Skip to content

Commit 051a8ba

Browse files
docs: update example of ApolloServerPluginCacheControl's cacheControl (#7848)
## Background / Context Fixes #7845 ## Goals - Ensure documentation of accessing the extended `cacheControl` information on GraphQL's info object does not contain any TypeScript errors ## Changes - Added import statements to the code examples for `import { cacheControlFromInfo } from '@apollo/cache-control-types'` - Leverage `cacheControlFromInfo` in code examples - Updated description of what provides `info.cacheControl` to reference the documentation for the cache control plugin
1 parent 29c391c commit 051a8ba

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

docs/source/performance/caching.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type Comment {
120120

121121
### In your resolvers (dynamic)
122122

123-
You can decide how to cache a particular field's result _while_ you're resolving it. To support this, Apollo Server provides a `cacheControl` object in the [`info` parameter](../data/resolvers/#resolver-arguments) that's passed to every resolver.
123+
You can decide how to cache a particular field's result _while_ you're resolving it. To support this, Apollo Server's [cache control plugin](../api/plugin/cache-control) provides a `cacheControl` object in the [`info` parameter](../data/resolvers/#resolver-arguments) that's passed to every resolver.
124124

125125
> If you set a field's cache hint in its resolver, it **overrides** any cache hint you [provided in your schema](#in-your-schema-static).
126126

@@ -129,11 +129,15 @@ You can decide how to cache a particular field's result _while_ you're resolving
129129
The `cacheControl` object includes a `setCacheHint` method, which you call like so:
130130

131131
```ts
132+
import { cacheControlFromInfo } from '@apollo/cache-control-types';
133+
132134
const resolvers = {
133135
Query: {
134136
post: (_, { id }, _, info) => {
137+
// Access ApolloServerPluginCacheControl's extension of the GraphQLResolveInfo object
138+
const cacheControl = cacheControlFromInfo(info)
135139
// highlight-start
136-
info.cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
140+
cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
137141
// highlight-end
138142
return find(posts, { id });
139143
},
@@ -151,11 +155,16 @@ This object represents the field's current cache hint. Its fields include the fo
151155
- A `restrict` method, which is similar to `setCacheHint` but it can't _relax_ existing hint settings:
152156

153157
```ts
158+
import { cacheControlFromInfo } from '@apollo/cache-control-types';
159+
160+
// Access ApolloServerPluginCacheControl's extension of the GraphQLResolveInfo object
161+
const cacheControl = cacheControlFromInfo(info)
162+
154163
// If we call this first...
155-
info.cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
164+
cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
156165

157166
// ...then this changes maxAge (more restrictive) but NOT scope (less restrictive)
158-
info.cacheControl.cacheHint.restrict({ maxAge: 30, scope: 'PUBLIC' });
167+
cacheControl.cacheHint.restrict({ maxAge: 30, scope: 'PUBLIC' });
159168
```
160169

161170
#### `cacheControl.cacheHintFromType`

0 commit comments

Comments
 (0)