Skip to content

Commit

Permalink
Ensure useFragment handles top-level null/undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 28, 2024
1 parent 5547c4c commit 00d1df1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/core/__tests__/masking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,39 @@ describe("maskOperation", () => {
});

describe("maskFragment", () => {
test("returns null when data is null", () => {
const fragment = gql`
fragment Foo on Query {
foo
...QueryFields
}
fragment QueryFields on Query {
bar
}
`;

const data = maskFragment(null, fragment, new InMemoryCache(), "Foo");

expect(data).toBe(null);
});

test("returns undefined when data is undefined", () => {
const fragment = gql`
fragment Foo on Query {
foo
...QueryFields
}
fragment QueryFields on Query {
bar
}
`;

const data = maskFragment(undefined, fragment, new InMemoryCache(), "Foo");

expect(data).toBe(undefined);
});
test("masks named fragments in fragment documents", () => {
const fragment = gql`
fragment UserFields on User {
Expand Down
5 changes: 5 additions & 0 deletions src/core/masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export function maskFragment<TData = unknown>(
fragmentName
);

if (data == null) {
// Maintain the original `null` or `undefined` value
return data;
}

const context: MaskingContext = {
operationType: "fragment",
operationName: fragment.name.value,
Expand Down

0 comments on commit 00d1df1

Please sign in to comment.