Skip to content

Commit

Permalink
apollo-server-types: move info.cacheControl 'declare module' here
Browse files Browse the repository at this point in the history
This makes `info.cacheControl` available to TypeScript packages that
depend on `apollo-server-types` rather than only being declared deep
inside `apollo-server-core`. Additionally, it gives a name to type used
for `info.cacheControl`.

Intended for use cases like
apollographql/federation#870

We've run into tricky issues with `declare module` before so if this
ends up causing more problems than it's worth, we may revert it.
  • Loading branch information
glasser committed Jul 19, 2021
1 parent bfdb18e commit 1d04bf5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The version headers in this history reflect the versions of Apollo Server itself

## vNEXT

- `apollo-server-types`: TypeScript typings for `info.cacheControl` are now added to `GraphQLResolveInfo` as part of `apollo-server-types` rather than a nested file in `apollo-server-core`, and the field now has a named type, `ResolveInfoCacheControl`. [PR #FIXME](https://github.com/apollographql/apollo-server/pull/FIXME)

## v3.0.1

- `apollo-server-core`: The default `maxAge` (which defaults to 0) for a field should only be applied if no dynamic cache control hint is set. Specifically, if you call the (new in 3.0.0) function `info.cacheControl.cacheHint.restrict({ maxAge: 60 })`, it should set `maxAge` to 60 even if the default max age is lower. (This bug fix is the behavior that was intended for 3.0.0, and primarily affects the behavior of functions added in Apollo Server 3. This does mean that checking `info.cacheControl.cacheHint` now only shows explicitly-set `maxAge` and not the default, but this seems like it will be helpful since it lets you differentiate between the two similar circumstances.) [PR #5492](https://github.com/apollographql/apollo-server/pull/5492)
Expand Down
18 changes: 1 addition & 17 deletions packages/apollo-server-core/src/plugin/cacheControl/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
CacheAnnotation,
CacheHint,
CachePolicy,
} from 'apollo-server-types';
import type { CacheAnnotation, CacheHint } from 'apollo-server-types';
import { CacheScope } from 'apollo-server-types';
import {
DirectiveNode,
Expand Down Expand Up @@ -37,18 +33,6 @@ export interface ApolloServerPluginCacheControlOptions {
__testing__cacheHints?: Map<string, CacheHint>;
}

declare module 'graphql/type/definition' {
interface GraphQLResolveInfo {
cacheControl: {
cacheHint: CachePolicy;
// Shorthand for `cacheHint.replace(hint)`; also for compatibility with
// the Apollo Server 2.x API.
setCacheHint(hint: CacheHint): void;
cacheHintFromType(t: GraphQLCompositeType): CacheHint | undefined;
};
}
}

export function ApolloServerPluginCacheControl(
options: ApolloServerPluginCacheControlOptions = Object.create(null),
): InternalApolloServerPlugin {
Expand Down
27 changes: 23 additions & 4 deletions packages/apollo-server-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'apollo-server-env';
import {
import type { Request, Response } from 'apollo-server-env';
import type {
GraphQLSchema,
ValidationContext,
ASTVisitor,
Expand All @@ -8,11 +8,12 @@ import {
DocumentNode,
GraphQLError,
GraphQLResolveInfo,
GraphQLCompositeType,
} from 'graphql';

// This seems like it could live in this package too.
import { KeyValueCache } from 'apollo-server-caching';
import { Trace } from 'apollo-reporting-protobuf';
import type { KeyValueCache } from 'apollo-server-caching';
import type { Trace } from 'apollo-reporting-protobuf';

export type BaseContext = Record<string, any>;

Expand Down Expand Up @@ -276,3 +277,21 @@ export interface CachePolicy extends CacheHint {
*/
policyIfCacheable(): Required<CacheHint> | null;
}

/**
* When using Apollo Server with the cache control plugin (on by default), an
* object of this kind is available to resolvers on `info.cacheControl`.
*/
export interface ResolveInfoCacheControl {
cacheHint: CachePolicy;
// Shorthand for `cacheHint.replace(hint)`; also for compatibility with
// the Apollo Server 2.x API.
setCacheHint(hint: CacheHint): void;
cacheHintFromType(t: GraphQLCompositeType): CacheHint | undefined;
}

declare module 'graphql/type/definition' {
interface GraphQLResolveInfo {
cacheControl: ResolveInfoCacheControl;
}
}

0 comments on commit 1d04bf5

Please sign in to comment.