From 80288a42fbc0c5e5197d8274c418d336c852954f Mon Sep 17 00:00:00 2001 From: Aron Woost Date: Thu, 14 Jul 2022 09:26:37 +0200 Subject: [PATCH 1/4] Add test --- src/__tests__/cache-redirect-id-array-case.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/__tests__/cache-redirect-id-array-case.ts diff --git a/src/__tests__/cache-redirect-id-array-case.ts b/src/__tests__/cache-redirect-id-array-case.ts new file mode 100644 index 00000000000..38108842f1f --- /dev/null +++ b/src/__tests__/cache-redirect-id-array-case.ts @@ -0,0 +1,60 @@ +import gql from "graphql-tag"; +import { ApolloClient, ApolloLink } from "../core"; +import { InMemoryCache } from "../cache"; +import { itAsync } from "../testing"; + +const query = gql` + query GetProductsByIds($ids: [String]!) { + getProductsByIds(ids: $ids) { + id + name + } + } +`; + +const cache = new InMemoryCache({ + typePolicies: { + Query: { + fields: { + getProductsByIds: { + read(_, { args, toReference }) { + // ℹ️ uncommenting the following line will make it work + // return false; + return args?.ids.map((id: string) => toReference(`Product:${id}`)); + }, + }, + }, + }, + }, +}); + +itAsync( + "does network request when ids are not found in cache", + (resolve, reject) => { + const client = new ApolloClient({ + link: ApolloLink.from([ + (operation, forward) => { + // indicates, that an outgoing query attempted is being done + expect(operation.operationName).toBe("GetProductsByIds"); + expect(operation.variables).toEqual({ ids: ["1", "2", "3"] }); + + resolve(); + + return forward(operation); + }, + ]), + cache, + }); + + client + .watchQuery({ + query, + // fetchPolicy: "cache-first", // ℹ️ change to "cache-and-network" will make it work + fetchPolicy: "cache-and-network", + variables: { + ids: ["1", "2", "3"], + }, + }) + .subscribe({ error: reject }); + } +); From a4e01e9475d54d51a1ee19240e7332be5d0a4d11 Mon Sep 17 00:00:00 2001 From: Aron Woost Date: Thu, 14 Jul 2022 09:46:08 +0200 Subject: [PATCH 2/4] Make it fail --- src/__tests__/cache-redirect-id-array-case.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/__tests__/cache-redirect-id-array-case.ts b/src/__tests__/cache-redirect-id-array-case.ts index 38108842f1f..22dc4e59da0 100644 --- a/src/__tests__/cache-redirect-id-array-case.ts +++ b/src/__tests__/cache-redirect-id-array-case.ts @@ -49,8 +49,7 @@ itAsync( client .watchQuery({ query, - // fetchPolicy: "cache-first", // ℹ️ change to "cache-and-network" will make it work - fetchPolicy: "cache-and-network", + fetchPolicy: "cache-first", // ℹ️ change to "cache-and-network" will make it work variables: { ids: ["1", "2", "3"], }, From 710655517b84baa68088615ab4b19ec50c72ea2c Mon Sep 17 00:00:00 2001 From: Aron Woost Date: Thu, 14 Jul 2022 14:37:49 +0200 Subject: [PATCH 3/4] Simplify --- src/__tests__/cache-redirect-id-array-case.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/__tests__/cache-redirect-id-array-case.ts b/src/__tests__/cache-redirect-id-array-case.ts index 22dc4e59da0..2c17b8af326 100644 --- a/src/__tests__/cache-redirect-id-array-case.ts +++ b/src/__tests__/cache-redirect-id-array-case.ts @@ -20,6 +20,7 @@ const cache = new InMemoryCache({ read(_, { args, toReference }) { // ℹ️ uncommenting the following line will make it work // return false; + return args?.ids.map((id: string) => toReference(`Product:${id}`)); }, }, @@ -49,7 +50,7 @@ itAsync( client .watchQuery({ query, - fetchPolicy: "cache-first", // ℹ️ change to "cache-and-network" will make it work + fetchPolicy: "cache-first", variables: { ids: ["1", "2", "3"], }, From c3cec8dea85c710870e963b25bb585bbcd575a6c Mon Sep 17 00:00:00 2001 From: Aron Woost Date: Thu, 14 Jul 2022 14:45:20 +0200 Subject: [PATCH 4/4] Simplify --- src/__tests__/cache-redirect-id-array-case.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/__tests__/cache-redirect-id-array-case.ts b/src/__tests__/cache-redirect-id-array-case.ts index 2c17b8af326..a693fe628f4 100644 --- a/src/__tests__/cache-redirect-id-array-case.ts +++ b/src/__tests__/cache-redirect-id-array-case.ts @@ -31,8 +31,8 @@ const cache = new InMemoryCache({ itAsync( "does network request when ids are not found in cache", - (resolve, reject) => { - const client = new ApolloClient({ + (resolve, reject) => + new ApolloClient({ link: ApolloLink.from([ (operation, forward) => { // indicates, that an outgoing query attempted is being done @@ -45,9 +45,7 @@ itAsync( }, ]), cache, - }); - - client + }) .watchQuery({ query, fetchPolicy: "cache-first", @@ -55,6 +53,5 @@ itAsync( ids: ["1", "2", "3"], }, }) - .subscribe({ error: reject }); - } + .subscribe({ error: reject }) );