Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo-server-types: move info.cacheControl 'declare module' here #5512

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 #5512](https://github.com/apollographql/apollo-server/pull/5512)

## 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;
}
}