Skip to content

Commit

Permalink
[Data masking] Fix issue with @unmask(mode: "migrate") with nested …
Browse files Browse the repository at this point in the history
…partial objects from parent query (#12134)
  • Loading branch information
jerelmiller authored Nov 15, 2024
1 parent a6ece37 commit cfaf4ef
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 151 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-zebras-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix issue where data went missing when an unmasked fragment in migrate mode selected fields that the parent did not.
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 41601,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34359
"dist/apollo-client.min.cjs": 41638,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34394
}
122 changes: 122 additions & 0 deletions src/core/__tests__/masking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,128 @@ describe("maskOperation", () => {
});
});

test('handles overlapping types when subtype has accessor warnings with @unmask(mode: "migrate")', async () => {
using consoleSpy = spyOnConsole("warn");
const query = gql`
query PlaylistQuery {
playlist {
...PlaylistFragment @unmask(mode: "migrate")
id
name
album {
id
tracks {
id
__typename
}
__typename
}
artist {
id
topTracks {
id
__typename
}
__typename
}
__typename
...PlaylistTitleCell @unmask(mode: "migrate")
}
}
fragment PlaylistFragment on Playlist {
album {
id
images {
url
__typename
}
tracks {
id
name
__typename
}
__typename
}
}
fragment PlaylistTitleCell on Playlist {
artist {
id
images {
url
__typename
}
topTracks {
id
name
__typename
}
__typename
}
}
`;

const data = maskOperation(
{
playlist: {
id: "1",
name: "Playlist",
album: {
id: "2RSIoPew2TOy41ASHpzOx3",
__typename: "Album",
images: [{ url: "https://i.scdn.co/image/1", __typename: "Image" }],
tracks: [{ id: "1", name: "Track 1", __typename: "Track" }],
},
artist: {
id: "2",
__typename: "Artist",
images: [{ url: "https://i.scdn.co/image/1", __typename: "Image" }],
topTracks: [{ id: "2", name: "Track 2", __typename: "Track" }],
},
},
},
query,
new InMemoryCache()
);

expect(consoleSpy.warn).not.toHaveBeenCalled();

consoleSpy.warn.mockClear();

data.playlist.album;
data.playlist.album.id;
data.playlist.album.__typename;
data.playlist.artist;
data.playlist.artist.id;
data.playlist.artist.__typename;
expect(console.warn).not.toHaveBeenCalled();

data.playlist.album.images;
data.playlist.artist.images;
expect(console.warn).toHaveBeenCalledTimes(2);

expect(data).toEqual({
playlist: {
id: "1",
name: "Playlist",
album: {
id: "2RSIoPew2TOy41ASHpzOx3",
__typename: "Album",
images: [{ url: "https://i.scdn.co/image/1", __typename: "Image" }],
tracks: [{ id: "1", name: "Track 1", __typename: "Track" }],
},
artist: {
id: "2",
__typename: "Artist",
images: [{ url: "https://i.scdn.co/image/1", __typename: "Image" }],
topTracks: [{ id: "2", name: "Track 2", __typename: "Track" }],
},
},
});
});

test("masks fragments in subscription documents", () => {
const subscription = gql`
subscription {
Expand Down
Loading

0 comments on commit cfaf4ef

Please sign in to comment.