Skip to content

Commit

Permalink
Ensure mutations warn with no-cache fetch policy and data masking
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Nov 12, 2024
1 parent fb49a69 commit 1765ee8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/__tests__/dataMasking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutation, never> = 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<unknown> {
Expand Down
1 change: 1 addition & 0 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export class QueryManager<TStore> {
data: self.maskOperation({
document: mutation,
data: storeResult.data,
fetchPolicy,
}) as any,
});
}
Expand Down

0 comments on commit 1765ee8

Please sign in to comment.