From ad1ce1d3690421133e39c86b802691fe2a205dd8 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 17 Mar 2020 10:33:38 +0000 Subject: [PATCH 1/3] Fix store.inspectFields undefined error for commutative layers --- exchanges/graphcache/src/store/data.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exchanges/graphcache/src/store/data.ts b/exchanges/graphcache/src/store/data.ts index 3dea12d506..1c058c7b8a 100644 --- a/exchanges/graphcache/src/store/data.ts +++ b/exchanges/graphcache/src/store/data.ts @@ -312,7 +312,9 @@ const extractNodeMapFields = ( // Then extracts FieldInfo for the entity from the optimistic maps for (let i = 0, l = currentData!.optimisticOrder.length; i < l; i++) { const optimistic = map.optimistic[currentData!.optimisticOrder[i]]; - extractNodeFields(fieldInfos, seenFieldKeys, optimistic.get(entityKey)); + if (optimistic !== undefined) { + extractNodeFields(fieldInfos, seenFieldKeys, optimistic.get(entityKey)); + } } }; From 3ee819a4bcdf990dd69c217ed0daae03cf92aec6 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 17 Mar 2020 10:35:11 +0000 Subject: [PATCH 2/3] Add changeset --- .changeset/dull-zebras-burn.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dull-zebras-burn.md diff --git a/.changeset/dull-zebras-burn.md b/.changeset/dull-zebras-burn.md new file mode 100644 index 0000000000..9547c7ea7d --- /dev/null +++ b/.changeset/dull-zebras-burn.md @@ -0,0 +1,5 @@ +--- +'@urql/exchange-graphcache': patch +--- + +Fix cache.inspectFields causing an undefined error for uninitialised or cleared commutative layers. From a04a57c190e399625836a73b252fde11221f9364 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 17 Mar 2020 10:37:39 +0000 Subject: [PATCH 3/3] Add test to prevent regression --- exchanges/graphcache/src/store/data.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/exchanges/graphcache/src/store/data.test.ts b/exchanges/graphcache/src/store/data.test.ts index db75fbadb8..35ed1edb2a 100644 --- a/exchanges/graphcache/src/store/data.test.ts +++ b/exchanges/graphcache/src/store/data.test.ts @@ -406,4 +406,21 @@ describe('commutative changes', () => { InMemoryData.initDataState(data, null); expect(InMemoryData.readRecord('Query', 'index')).toBe(2); }); + + it('prevents inspectFields from failing for uninitialised layers', () => { + InMemoryData.initDataState(data, null); + InMemoryData.writeRecord('Query', 'test', true); + InMemoryData.clearDataState(); + + InMemoryData.reserveLayer(data, 1); + + InMemoryData.initDataState(data, null); + expect(InMemoryData.inspectFields('Query')).toEqual([ + { + arguments: null, + fieldKey: 'test', + fieldName: 'test', + }, + ]); + }); });