Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data masking] Fix issue applying accessor warnings to interface types #12130

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-countries-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix error thrown when applying unmask migrate mode warnings on interface types with selection sets that contain inline fragment conditions.
66 changes: 66 additions & 0 deletions src/core/__tests__/masking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,72 @@ describe("maskOperation", () => {
expect(console.warn).not.toHaveBeenCalled();
});

// https://github.com/apollographql/apollo-client/issues/12127
test('handles interface types when using @unmask(mode: "migrate")', async () => {
using _ = spyOnConsole("warn");
const query = gql`
query PlaybackStateSubscriberQuery {
playbackState {
__typename
...PlaybackStateFragment @unmask(mode: "migrate")
}
}

fragment PlaybackStateFragment on PlaybackState {
item {
__typename
id

... on Track {
album {
__typename
id
}
}

... on Episode {
show {
__typename
id
}
}
}
}
`;

const data = maskOperation(
{
playbackState: {
__typename: "PlaybackState",
item: {
__typename: "Track",
id: "1",
album: {
__typename: "Album",
id: "2",
},
},
},
},
query,
new InMemoryCache()
);

expect(data).toEqual({
playbackState: {
__typename: "PlaybackState",
item: {
__typename: "Track",
id: "1",
album: {
__typename: "Album",
id: "2",
},
},
},
});
});

test("masks fragments in subscription documents", () => {
const subscription = gql`
subscription {
Expand Down
7 changes: 7 additions & 0 deletions src/core/masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ function addFieldAccessorWarnings(
return memo;
}
case Kind.INLINE_FRAGMENT: {
if (
selection.typeCondition &&
!context.cache.fragmentMatches!(selection, (data as any).__typename)
) {
return memo;
}

return addFieldAccessorWarnings(
memo,
data,
Expand Down