Skip to content

Commit

Permalink
Always retain __typename when present
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 21, 2024
1 parent bc5326e commit 0098ced
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/core/__tests__/masking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,45 @@ describe("maskOperation", () => {
expect(data).toEqual({ user: { __typename: "User", id: 1 } });
});

test("retains __typename in the result", () => {
const query = gql`
query {
user {
id
profile {
id
}
...UserFields
}
}
fragment UserFields on Query {
age
}
`;

const data = maskOperation(
deepFreeze({
user: {
__typename: "User",
id: 1,
age: 30,
profile: { __typename: "Profile", id: 2 },
},
}),
query,
createFragmentMatcher(new InMemoryCache())
);

expect(data).toEqual({
user: {
__typename: "User",
id: 1,
profile: { __typename: "Profile", id: 2 },
},
});
});

test("deep freezes the masked result if the original data is frozen", () => {
const query = gql`
query {
Expand Down
8 changes: 7 additions & 1 deletion src/core/masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function maskSelectionSet(
return [changed ? masked : data, changed];
}

return selectionSet.selections.reduce<[any, boolean]>(
const result = selectionSet.selections.reduce<[any, boolean]>(
([memo, changed], selection) => {
switch (selection.kind) {
case Kind.FIELD: {
Expand Down Expand Up @@ -223,6 +223,12 @@ function maskSelectionSet(
},
[Object.create(null), false]
);

if ("__typename" in data && !("__typename" in result[0])) {
result[0].__typename = data.__typename;
}

return result;
}

function addFieldAccessorWarnings(
Expand Down

0 comments on commit 0098ced

Please sign in to comment.