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): Switch fragment heuristic to always be truthy on writes (#2455)

Previously we assumed that if an unknown fragment was being applied it'd
be an exceptional case and would not have to apply if the cache doesn't
already know all its fields.

We can now (theoretically) safely switch to always return `true` on writes.
When we encounter a field that isn't in the data, as returned by the API
then we can eagerly write anyway and let it fail.

This is important for cases where the fragment applies to an interface
of a node rather than to a concrete type.

authored by

Phil Pluckthun and committed by
GitHub
cabbcd7e 72882d30

+16 -6
+5
.changeset/thin-worms-unite.md
··· 1 + --- 2 + '@urql/exchange-graphcache': patch 3 + --- 4 + 5 + Switch `isFragmentHeuristicallyMatching()` to always return `true` for writes, so that we give every fragment a chance to be applied and to write to the cache.
+9 -6
exchanges/graphcache/src/operations/shared.ts
··· 17 17 } from '../ast'; 18 18 19 19 import { warn, pushDebugNode, popDebugNode } from '../helpers/help'; 20 - import { hasField } from '../store/data'; 20 + import { hasField, isWriting } from '../store/data'; 21 21 import { Store, keyOfField } from '../store'; 22 22 23 23 import { getFieldArguments, shouldInclude, isInterfaceOfType } from '../ast'; ··· 141 141 16 142 142 ); 143 143 144 - return !getSelectionSet(node).some(node => { 145 - if (!isFieldNode(node)) return false; 146 - const fieldKey = keyOfField(getName(node), getFieldArguments(node, vars)); 147 - return !hasField(entityKey, fieldKey); 148 - }); 144 + return ( 145 + isWriting() || 146 + !getSelectionSet(node).some(node => { 147 + if (!isFieldNode(node)) return false; 148 + const fieldKey = keyOfField(getName(node), getFieldArguments(node, vars)); 149 + return !hasField(entityKey, fieldKey); 150 + }) 151 + ); 149 152 }; 150 153 151 154 interface SelectionIterator {
+2
exchanges/graphcache/src/store/data.ts
··· 85 85 return newData; 86 86 }; 87 87 88 + export const isWriting = (): boolean => currentOperation === 'write'; 89 + 88 90 export const ownsData = (data?: Data): boolean => 89 91 !!data && currentOwnership!.has(data); 90 92