urql-graphql / urql

The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
https://urql.dev/goto/docs
MIT License
8.6k stars 448 forks source link

feat(graphcache): local directives #3306

Closed JoviDeCroock closed 1 year ago

JoviDeCroock commented 1 year ago

Resolves #3191

See https://github.com/urql-graphql/urql/pull/3317 for more information on client-only directives.

Summary

This introduces a new property to the Graphcache config named directives, this will function as a more selective way to apply resolvers. i.e. you can do @_relayPagination only when you want it.

The directives config is populated by object entries that define client-only/local directives. A directive is a function that accepts an object of arguments passed to directives and must return a resolver.

If you add an entry relayPagination: (directiveArgs) => yourResolver then @_relayPagination will become available. All directives with a leading underscore are stripped from queries sent to the API after #3317.

This means that you can selectively implement local/client-only directives that only change what resolver is used in Graphcache!

Additionally, this introduces two default directives @_optional and @_required as a means to facilitate optional query selections without the client-controlled nullability spec being shipped 😅 this bridges the gap between schemaless and schema-aware graphcache.

You can add @_optional to allow Graphcache to default any field to a null value. And, you can add @_required to tell Graphcache that a field is required, even if it's cached as null.