Skip to content

Commit 79cb3d3

Browse files
committed
add documentation
1 parent 7c3d8de commit 79cb3d3

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

website/src/pages/docs/features/response-caching.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,47 @@ useResponseCache({
9595
})
9696
```
9797

98+
### Enforce session based caching
99+
100+
In some cases, a type or a field should only be cached if their is a session. For this, you can use
101+
the `scope` to indicate that the cache should only be used if a session is present.
102+
103+
```ts filename="Response Cache configuration with scope"
104+
useResponseCache({
105+
// cache based on the authentication header
106+
session: request => request.headers.get('authentication')
107+
108+
// You can use configuration object to define the scope
109+
scopePerSchemaCoordinate: {
110+
'Query.me': 'PRIVATE', // on a field
111+
'User: 'PRIVATE', // or a type
112+
}
113+
})
114+
```
115+
116+
It is also possible to use the `@cacheControl` directive to define the scope.
117+
118+
```ts filename="Response Cache configuration with scope using @cacheControl directive"
119+
import { cacheControlDirective } from '@graphql-yoga/plugin-response-cache'
120+
121+
const typeDefs = /* GraphQL */ `
122+
# the directive needs to be defined in the schema
123+
${cacheControlDirective}
124+
125+
type Query {
126+
# cache operations selecting Query.lazy for 10 seconds
127+
me: User @cacheControl(scope: PRIVATE)
128+
}
129+
130+
type User @cacheControl(scope: PRIVATE) {
131+
#...
132+
}
133+
`
134+
```
135+
136+
Any query containing a type or a field with the scope `PRIVATE` will only be cached if a session is
137+
present.
138+
98139
## Time to Live (TTL)
99140
100141
It is possible to give cached operations a time to live. Either globally, based on

0 commit comments

Comments
 (0)