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

fix(graphcache): Mutable links on InMemoryData store (#3516)

Changed files
+10 -5
.changeset
exchanges
graphcache
src
store
+5
.changeset/wicked-kids-joke.md
···
+
---
+
'@urql/exchange-graphcache': patch
+
---
+
+
Fix `store.resolve()` returning the exact link array that’s used by the cache. This can lead to subtle bugs when a user mutates the result returned by `cache.resolve()`, since this directly mutates what’s in the cache at that layer.
+5 -5
exchanges/graphcache/src/store/store.ts
···
field: string,
args?: FieldArgs
): DataField | undefined {
-
let fieldValue: DataField | undefined = null;
const entityKey = this.keyOfEntity(entity);
if (entityKey) {
const fieldKey = keyOfField(field, args);
-
fieldValue = InMemoryData.readRecord(entityKey, fieldKey);
-
if (fieldValue === undefined)
-
fieldValue = InMemoryData.readLink(entityKey, fieldKey);
+
const fieldValue = InMemoryData.readRecord(entityKey, fieldKey);
+
if (fieldValue !== undefined) return fieldValue;
+
let fieldLink = InMemoryData.readLink(entityKey, fieldKey);
+
if (fieldLink !== undefined) fieldLink = ensureLink(this, fieldLink);
+
return fieldLink;
}
-
return fieldValue;
}
resolveFieldByKey(entity: Entity, field: string, args?: FieldArgs) {