From 1765ee8929cc07b93b0b79d4a91e869c675f7152 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 12 Nov 2024 16:52:44 -0700 Subject: [PATCH] Ensure mutations warn with no-cache fetch policy and data masking --- src/__tests__/dataMasking.ts | 66 ++++++++++++++++++++++++++++++++++++ src/core/QueryManager.ts | 1 + 2 files changed, 67 insertions(+) diff --git a/src/__tests__/dataMasking.ts b/src/__tests__/dataMasking.ts index 09c7c524788..6bbe4dbcbf7 100644 --- a/src/__tests__/dataMasking.ts +++ b/src/__tests__/dataMasking.ts @@ -5341,6 +5341,72 @@ describe("client.mutate", () => { expect(errors).toEqual([{ message: "Could not determine age" }]); }); + + test("warns and returns masked result when used with no-cache fetch policy", async () => { + using _ = spyOnConsole("warn"); + type UserFieldsFragment = { + age: number; + } & { " $fragmentName"?: "UserFieldsFragment" }; + + interface Mutation { + updateUser: { + __typename: "User"; + id: number; + name: string; + } & { + " $fragmentRefs"?: { UserFieldsFragment: UserFieldsFragment }; + }; + } + + const mutation: MaskedDocumentNode = gql` + mutation MaskedMutation { + updateUser { + id + name + ...UserFields + } + } + + fragment UserFields on User { + age + } + `; + + const mocks = [ + { + request: { query: mutation }, + result: { + data: { + updateUser: { + __typename: "User", + id: 1, + name: "Test User", + age: 30, + }, + }, + }, + }, + ]; + + const client = new ApolloClient({ + dataMasking: true, + cache: new InMemoryCache(), + link: new MockLink(mocks), + }); + + const { data } = await client.mutate({ mutation, fetchPolicy: "no-cache" }); + + expect(data).toEqual({ + updateUser: { + __typename: "User", + id: 1, + name: "Test User", + }, + }); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(NO_CACHE_WARNING); + }); }); class TestCache extends ApolloCache { diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index d9009f662e1..cfdb82fec43 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -366,6 +366,7 @@ export class QueryManager { data: self.maskOperation({ document: mutation, data: storeResult.data, + fetchPolicy, }) as any, }); }