From 093a19af096c1b44f11468f3bfaeaa9be641f0e7 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 27 Aug 2024 16:08:03 -0600 Subject: [PATCH] Add test to ensure partial data returned for errors is masked --- src/__tests__/dataMasking.ts | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/__tests__/dataMasking.ts b/src/__tests__/dataMasking.ts index 111837447d2..eeddd834379 100644 --- a/src/__tests__/dataMasking.ts +++ b/src/__tests__/dataMasking.ts @@ -2564,6 +2564,57 @@ describe("client.query", () => { expect(errors).toEqual([{ message: "User not logged in" }]); }); + + test("masks fragment data in fields nulled by errors when using errorPolicy `all`", async () => { + const query = gql` + query MaskedQuery { + currentUser { + id + name + ...UserFields + } + } + + fragment UserFields on User { + age + } + `; + + const mocks = [ + { + request: { query }, + result: { + data: { + currentUser: { + __typename: "User", + id: 1, + name: "Test User", + age: null, + }, + }, + errors: [{ message: "Could not determine age" }], + }, + }, + ]; + + const client = new ApolloClient({ + dataMasking: true, + cache: new InMemoryCache(), + link: new MockLink(mocks), + }); + + const { data, errors } = await client.query({ query, errorPolicy: "all" }); + + expect(data).toEqual({ + currentUser: { + __typename: "User", + id: 1, + name: "Test User", + }, + }); + + expect(errors).toEqual([{ message: "Could not determine age" }]); + }); }); class TestCache extends ApolloCache {