Repository: FormidableLabs/urql
Pull request #694 (2020-04-12 19:40:28)

(fix) - invalidate null link
## Summary

There was an issue in invalidation when a link was null, this lead to the cache invalidating a record instead of the link since we were checking for a truthy value rather than just undefined.

Fixes: https://github.com/FormidableLabs/urql/issues/693

## Set of changes

- Change link check to `!== undefined` so we account for `null`


changeset-bot[bot]: ###  🦋  Changeset is good to go

Latest commit: c3e230abd7c01b036498f1220900f2d47b4e33fd

**We got this.**

<details><summary>This PR includes changesets to release 1 package</summary>

  | Name                      | Type  |
| ------------------------- | ----- |
| @urql/exchange-graphcache | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/FormidableLabs/urql/new/fix-null-invalidation?filename=.changeset/tasty-chicken-complain.md&value=---%0A%22%40urql%2Fexchange-graphcache%22%3A%20patch%0A---%0A%0A(fix)%20-%20invalidate%20null%20link%0A)



Patch:
diff --git a/.changeset/thirty-boxes-divide.md b/.changeset/thirty-boxes-divide.md
new file mode 100644
index 00000000..18429b35
--- /dev/null
+++ b/.changeset/thirty-boxes-divide.md
@@ -0,0 +1,5 @@
+---
+'@urql/exchange-graphcache': patch
+---
+
+Change invalidation to check for undefined links since null is a valid value in graphql
diff --git a/exchanges/graphcache/src/operations/invalidate.ts b/exchanges/graphcache/src/operations/invalidate.ts
index b88d9c43..2e6ccfe3 100644
--- a/exchanges/graphcache/src/operations/invalidate.ts
+++ b/exchanges/graphcache/src/operations/invalidate.ts
@@ -18,7 +18,7 @@ export const invalidateEntity = (
 
   for (let i = 0, l = fields.length; i < l; i++) {
     const { fieldKey } = fields[i];
-    if (InMemoryData.readLink(entityKey, fieldKey)) {
+    if (InMemoryData.readLink(entityKey, fieldKey) !== undefined) {
       InMemoryData.writeLink(entityKey, fieldKey, undefined);
     } else {
       InMemoryData.writeRecord(entityKey, fieldKey, undefined);


Test patch:
diff --git a/exchanges/graphcache/src/store/store.test.ts b/exchanges/graphcache/src/store/store.test.ts
index 0810beb1..f95ada5c 100644
--- a/exchanges/graphcache/src/store/store.test.ts
+++ b/exchanges/graphcache/src/store/store.test.ts
@@ -182,6 +182,41 @@ describe('Store with OptimisticMutationConfig', () => {
     expect(InMemoryData.readRecord('Todo:0', 'text')).toBe(undefined);
   });
 
+  it('should invalidate null keys correctly', () => {
+    const connection = gql`
+      query test {
+        exercisesConnection(page: { after: null, first: 10 }) {
+          id
+        }
+      }
+    `;
+
+    write(
+      store,
+      {
+        query: connection,
+      },
+      // @ts-ignore
+      {
+        exercisesConnection: null,
+      }
+    );
+    let { data } = query(store, { query: connection });
+
+    InMemoryData.initDataState(store.data, null);
+    expect((data as any).exercisesConnection).toEqual(null);
+    const fields = store.inspectFields({ __typename: 'Query' });
+    fields.forEach(({ fieldName, arguments: args }) => {
+      if (fieldName === 'exercisesConnection') {
+        store.invalidate('Query', fieldName, args);
+      }
+    });
+    InMemoryData.clearDataState();
+
+    ({ data } = query(store, { query: connection }));
+    expect(data).toBe(null);
+  });
+
   it('should be able to invalidate data with arguments', () => {
     write(
       store,