···2727| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
2828| `keys` | A mapping of key generator functions for types that are used to override the default key generation that _Graphcache_ uses to normalize data for given types. |
2929| `resolvers` | A nested mapping of resolvers, which are used to override the record or entity that _Graphcache_ resolves for a given field for a type. |
3030+| `directives` | A mapping of directives, which are functions accepting directive arguments and returning a resolver, which can be referenced by `@localDirective` or `@_localDirective` in queries. |
3031| `updates` | A nested mapping of updater functions for mutation and subscription fields, which may be used to add side-effects that update other parts of the cache when the given subscription or mutation field is written to the cache. |
3132| `optimistic` | A mapping of mutation fields to resolvers that may be used to provide _Graphcache_ with an optimistic result for a given mutation field that should be applied to the cached data temporarily. |
3233| `schema` | A serialized GraphQL schema that is used by _Graphcache_ to resolve partial data, interfaces, and enums. The schema also used to provide helpful warnings for [schema awareness](../graphcache/schema-awareness.md). |
+77-12
docs/graphcache/local-directives.md
···5566# Local Directives
7788-Graphcache supports adding directives to GraphQL Documents, when we prefix a
99-directive with an underscore (`_`) it will be stripped from the document and stored
1010-on the `_directives` property on the AST-node.
88+Previously, we've learned about local resolvers [on the "Normalized Caching"
99+page](./normalized-caching.md#manually-resolving-entities) and [the "Local Resolvers" page](./local-resolvers.md).
1010+1111+Resolvers allow us to change the data that Graphcache resolvers for a given field on a given type.
1212+This, in turn, allows us to change which links and data are returned in a query’s result, which
1313+otherwise may not be cached or be returned in a different shape.
1414+1515+Resolvers are useful to globally change how a field behaves, for instance, to tell Graphcache that
1616+a `Query.item(id: $id)` field returns an item of type `Item` with the `id` field, or to transform
1717+a value before it’s used in the UI.
1818+1919+However, resolvers are limited to changing the behaviour globally, not to change a field’s behaviour
2020+per query. This is why **local directives** exist.
11211212-> Ensure you prefix directives with `_` if you only want to alter local behavior.
2222+## Adding client-only directives
13231414-By default graphcache will add two directives `@_optional` and `@_required` which
1515-allow you to mark fields as being optional or mandatory.
2424+Any directive in our GraphQL documents that’s prefixed with an underscore character (`_`) will be
2525+filtered by `@urql/core`. This means that our GraphQL API never sees it and it becomes
2626+a “client-only directive”.
16271717-If you want to add directives yourself you can do so by performing
2828+No matter whether we prefix a directive or not however, we can define local resolvers for directives
2929+in Graphcache’s configuration and make conditional local resolvers.
18301931```js
2032cacheExchange({
2133 directives: {
2222- // If you now add `@_pagination` to your document we will execute this
2323- pagination: directiveArguments => () => {
2424- /* Resolver */
3434+ pagination(directiveArgs) {
3535+ // This resolver is called for @_pagination directives
3636+ return (parent, args, cache, info) => {
3737+ return null;
3838+ };
2539 },
2640 },
2741});
2842```
29433030-The function signature of a directive is a function which receives the arguments the directive is called with in the document.
3131-That function should returns a [Resolver](./local-directives.md).
4444+Once we define a directive on the `directives` configuration object, we can reference it in our
4545+GraphQL queries.
4646+4747+As per the above example, if we now reference `@_pagination` in a query, the resolver that’s
4848+returned in the configuration will be applied to the field, just like a local resolver.
4949+5050+We can also reference the directive using `@pagination`, however, this will mean that it’s
5151+also sent to the API, so this usually isn’t what we want.
5252+5353+## Client-controlled Nullability
5454+5555+Graphcache comes with two directives built-in by default. The `optional` and `required` directives.
5656+These directives can be used as an alternative to [the Schema Awareness
5757+feature’s](./schema-awareness.md) ability to generate partial results.
5858+5959+If we were to write a query that contains `@_optional` on a field, then the field is always allowed to be
6060+nullable. In case it’s not cached, Graphcache will be able to replace it with a `null`
6161+value.
6262+6363+Similarly, if we annotate a field with `@_required`, the value is not optional and, even if the
6464+cache knows the value is set to `null`, it will become required and Graphcache will either cascade
6565+to the next higher parent field annotated with `@_optional`, or will mark a query as a cache-miss.
6666+6767+## Pagination
6868+6969+Previously, in [the “Local Resolvers” page’s Pagination section](./local-resolvers.md#pagination) we
7070+defined a local resolver to add infinite pagination to a given type’s field.
7171+7272+If we add the `simplePagination` or `relayPagination` helpers as directives instead, we can still
7373+use our schema’s pagination field as normal, and instead, only use infinite pagination as required.
7474+7575+```js
7676+import { simplePagination } from '@urql/exchange-graphcache/extras';
7777+import { relayPagination } from '@urql/exchange-graphcache/extras';
7878+7979+cacheExchange({
8080+ directives: {
8181+ simplePagination,
8282+ relayPagination,
8383+ },
8484+});
8585+```
8686+8787+Defining directives for our resolver factory functions means that we can now use them selectively.
8888+8989+```graphql
9090+{
9191+ todos(first: 10) @_relayPagination(mergeMode: "outwards") {
9292+ id
9393+ text
9494+ }
9595+}
9696+```
32973398### Reading on
3499