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

Improve defaultMaxAge setting #4162

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/source/performance/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ const server = new ApolloServer({
}));
```

If you elect not to have a defaultMaxAge of 0 set for all resolvers and types you must explicitly set it to `null`. For example:

```javascript

const typeDefs = `
type Foo {
id: ID!
name: String!
}

type User {
id: ID!
name: String!
status: String!
foo: Foo!
}

type Query {
user: User @cacheControl(maxAge: 0, scope: PRIVATE)
userFoo: User @cacheControl(maxAge: 100)
}
`;

const server = new ApolloServer({
// ...
cacheControl: {
defaultMaxAge: null,
},
}));
```

This is useful if you have multiple queries of identical types like in the example shown, `query user` will have a maxAge of 0 while `query userFoo` will have a maxAge of `100`.

### The overall cache policy

Apollo Server's cache API lets you declare fine-grained cache hints on specific resolvers. Apollo Server then combines these hints into an overall cache policy for the response. The `maxAge` of this policy is the minimum `maxAge` across all fields in your request. As [described above](#setting-a-default-maxage), the default `maxAge` of all root fields and non-scalar fields is 0, so the overall cache policy for a response will have `maxAge` 0 (ie, uncacheable) unless all root and non-scalar fields in the response have cache hints (or if `defaultMaxAge` is specified).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
import { buildSchemaWithCacheControlSupport } from './cacheControlSupport';
import {
buildSchemaWithCacheControlSupport,
makeExecutableSchemaWithCacheControlSupport
} from './cacheControlSupport';

import { CacheScope } from '../';
import { collectCacheControlHints } from './collectCacheControlHints';

describe('@cacheControl directives', () => {
it('it should not apply the defaultMaxAge for types when defaultMaxAge is null and no cacheControl directive is set', async () => {
const typeDefs = `
type Foo {
id: ID!
name: String!
}

type User {
id: ID!
name: String!
status: String!
foo: Foo!
}

type Query {
fooUser: User @cacheControl(maxAge: 100)
}
`;

const schema = makeExecutableSchemaWithCacheControlSupport({
typeDefs,
});

const hints = await collectCacheControlHints(
schema,
`
query {
fooUser {
id
name
status
foo {
id
name
}
}
}
`, {
defaultMaxAge: null
}
);

expect(hints).not.toContainEqual({ path: ['fooUser', 'foo'], maxAge: 0 });
expect(hints).toContainEqual({ path: ['fooUser'], maxAge: 100 });
});


it('should set maxAge: 0 and no scope for a field without cache hints', async () => {
const schema = buildSchemaWithCacheControlSupport(`
type Query {
Expand Down
10 changes: 6 additions & 4 deletions packages/apollo-cache-control/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export enum CacheScope {
}

export interface CacheControlExtensionOptions {
defaultMaxAge?: number;
defaultMaxAge?: number | null;
// FIXME: We should replace these with
// more appropriately named options.
calculateHttpHeaders?: boolean;
Expand All @@ -51,10 +51,11 @@ declare module 'apollo-server-types' {

export class CacheControlExtension<TContext = any>
implements GraphQLExtension<TContext> {
private defaultMaxAge: number;
private defaultMaxAge: number | null;

constructor(public options: CacheControlExtensionOptions = {}) {
this.defaultMaxAge = options.defaultMaxAge || 0;
this.defaultMaxAge =
options.defaultMaxAge !== null ? options.defaultMaxAge || 0 : null;
}

private hints: Map<ResponsePath, CacheHint> = new Map();
Expand Down Expand Up @@ -104,7 +105,8 @@ export class CacheControlExtension<TContext = any>
(targetType instanceof GraphQLObjectType ||
targetType instanceof GraphQLInterfaceType ||
!info.path.prev) &&
hint.maxAge === undefined
hint.maxAge === undefined &&
this.defaultMaxAge !== null
) {
hint.maxAge = this.defaultMaxAge;
}
Expand Down