Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(graphcache): Fix partial and info usage in default directives (#3338)

authored by

Phil Pluckthun and committed by
GitHub
51f72c2c abf77f47

+27 -11
+5
.changeset/heavy-shirts-smash.md
··· 1 + --- 2 + '@urql/exchange-graphcache': patch 3 + --- 4 + 5 + Fix `@_optional` directive not setting `info.partial = true` on cache miss and fix usage of `info.parentKey` and `info.parentFieldKey` usage in default directives.
+21
exchanges/graphcache/src/helpers/defaultDirectives.ts
··· 1 + import type { Resolver, DirectivesConfig } from '../types'; 2 + 3 + const optional: Resolver = (_parent, _args, cache, info) => { 4 + const result = cache.resolve(info.parentKey, info.parentFieldKey); 5 + if (result === undefined) { 6 + info.partial = true; 7 + return null; 8 + } else { 9 + return result; 10 + } 11 + }; 12 + 13 + const required: Resolver = (_parent, _args, cache, info) => { 14 + const result = cache.resolve(info.parentKey, info.parentFieldKey); 15 + return result == null ? undefined : result; 16 + }; 17 + 18 + export const defaultDirectives: DirectivesConfig = { 19 + optional: () => optional, 20 + required: () => required, 21 + };
+1 -11
exchanges/graphcache/src/store/store.ts
··· 19 19 } from '../types'; 20 20 21 21 import { invariant } from '../helpers/help'; 22 + import { defaultDirectives } from '../helpers/defaultDirectives'; 22 23 import { contextRef, ensureLink } from '../operations/shared'; 23 24 import { _query, _queryFragment } from '../operations/query'; 24 25 import { _write, _writeFragment } from '../operations/write'; ··· 37 38 38 39 type DocumentNode = TypedDocumentNode<any, any>; 39 40 type RootField = 'query' | 'mutation' | 'subscription'; 40 - 41 - const defaultDirectives: DirectivesConfig = { 42 - optional: () => (_parent, args, cache, info) => { 43 - const result = cache.resolve(info.parentFieldKey, info.fieldName, args); 44 - return result === undefined ? null : result; 45 - }, 46 - required: () => (_parent, args, cache, info) => { 47 - const result = cache.resolve(info.parentFieldKey, info.fieldName, args); 48 - return result === null ? undefined : result; 49 - }, 50 - }; 51 41 52 42 /** Implementation of the {@link Cache} interface as created internally by the {@link cacheExchange}. 53 43 * @internal