Skip to content

Commit

Permalink
Avoid redundant refetchQueries call for mutation with no-cache policy (
Browse files Browse the repository at this point in the history
…#11515)

Co-authored-by: Lenz Weber-Tronic <mail@lenzw.de>
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
  • Loading branch information
3 people committed Jan 31, 2024
1 parent 85fb665 commit c9bf93b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-zebras-end.md
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Avoid redundant refetchQueries call for mutation with no-cache policy (fixes #10238)
4 changes: 2 additions & 2 deletions .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
}
29 changes: 29 additions & 0 deletions src/__tests__/client.ts
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/core/QueryManager.ts
Expand Up @@ -492,7 +492,7 @@ export class QueryManager<TStore> {

if (
cacheWrites.length > 0 ||
mutation.refetchQueries ||
(mutation.refetchQueries || "").length > 0 ||
mutation.update ||
mutation.onQueryUpdated ||
mutation.removeOptimistic
Expand Down

0 comments on commit c9bf93b

Please sign in to comment.