From 59a5ff8a05772d5462e409c836e2dfbfb4a98816 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 23 Jan 2024 15:01:15 +0100 Subject: [PATCH 1/6] Add failing test --- src/__tests__/client.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/__tests__/client.ts b/src/__tests__/client.ts index 2fd8c29f187..cf8df19c268 100644 --- a/src/__tests__/client.ts +++ b/src/__tests__/client.ts @@ -2686,6 +2686,35 @@ describe("client", () => { spy.mockRestore(); }); + // See https://github.com/apollographql/apollo-client/issues/10238 + it("does not call QueryManager.refetchQueries for mutations with no-cache policy", async () => { + const mutation = gql` + mutation { + noop + } + `; + const link = mockSingleLink({ + request: { query: mutation }, + result: { data: { noop: false } }, + }); + + const client = new ApolloClient({ + link, + cache: new InMemoryCache(), + }); + + const spy = jest.spyOn(client["queryManager"], "refetchQueries"); + spy.mockImplementation(() => new Map()); + + await client.mutate({ + mutation, + fetchPolicy: "no-cache", + }); + + expect(spy).not.toHaveBeenCalled(); + spy.mockRestore(); + }); + it("has a getObservableQueries method which calls QueryManager", async () => { const client = new ApolloClient({ link: ApolloLink.empty(), From 1143b0bea77cd50c2fe3b17bb4853823949a193f Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 23 Jan 2024 17:03:17 +0100 Subject: [PATCH 2/6] Avoid redundant refetchQueries call for mutation with no-cache policy --- src/core/QueryManager.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index 62909a7f7b9..d712cd1ea70 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -470,10 +470,14 @@ export class QueryManager { }); } } + const shouldRefetchQueries = + Array.isArray(mutation.refetchQueries) ? + mutation.refetchQueries.length > 0 + : Boolean(mutation.refetchQueries); if ( cacheWrites.length > 0 || - mutation.refetchQueries || + shouldRefetchQueries || mutation.update || mutation.onQueryUpdated || mutation.removeOptimistic From 815678c819161fed682927430a6972d51aaaaef8 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 23 Jan 2024 17:07:47 +0100 Subject: [PATCH 3/6] add changeset --- .changeset/ninety-zebras-end.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/ninety-zebras-end.md diff --git a/.changeset/ninety-zebras-end.md b/.changeset/ninety-zebras-end.md new file mode 100644 index 00000000000..7097b30b2b7 --- /dev/null +++ b/.changeset/ninety-zebras-end.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Avoid redundant refetchQueries call for mutation with no-cache policy (fixes #10238) From e3d4b4d8858f998c4526649f32a3698646368043 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Wed, 24 Jan 2024 14:01:05 +0100 Subject: [PATCH 4/6] Update src/core/QueryManager.ts Co-authored-by: Lenz Weber-Tronic --- src/core/QueryManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index d712cd1ea70..0614a28f9ff 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -477,7 +477,7 @@ export class QueryManager { if ( cacheWrites.length > 0 || - shouldRefetchQueries || + (mutation.refetchQueries || "").length > 0 || mutation.update || mutation.onQueryUpdated || mutation.removeOptimistic From 21f163901576ab0d8e73b4a8ad1edf0e78b1650d Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Wed, 24 Jan 2024 14:01:11 +0100 Subject: [PATCH 5/6] Update src/core/QueryManager.ts Co-authored-by: Lenz Weber-Tronic --- src/core/QueryManager.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index 0614a28f9ff..d30cbe0c9dc 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -470,10 +470,6 @@ export class QueryManager { }); } } - const shouldRefetchQueries = - Array.isArray(mutation.refetchQueries) ? - mutation.refetchQueries.length > 0 - : Boolean(mutation.refetchQueries); if ( cacheWrites.length > 0 || From 35e9f3658823ce6dce0dbb8e443b14d87ad40a69 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 31 Jan 2024 09:07:50 -0700 Subject: [PATCH 6/6] Bump size limit --- .size-limits.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.size-limits.json b/.size-limits.json index ca5d4c24594..44d275b37a4 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 39149, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32647 + "dist/apollo-client.min.cjs": 39153, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32652 }