From 1e7d009e4a52949dab0065f3219dfe148837531e Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Fri, 15 Nov 2024 10:30:07 -0700 Subject: [PATCH] [Data masking] Fix issue applying accessor warnings to interface types (#12130) --- .changeset/nice-countries-share.md | 5 +++ src/core/__tests__/masking.test.ts | 66 ++++++++++++++++++++++++++++++ src/core/masking.ts | 7 ++++ 3 files changed, 78 insertions(+) create mode 100644 .changeset/nice-countries-share.md diff --git a/.changeset/nice-countries-share.md b/.changeset/nice-countries-share.md new file mode 100644 index 00000000000..0af3ec25287 --- /dev/null +++ b/.changeset/nice-countries-share.md @@ -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. diff --git a/src/core/__tests__/masking.test.ts b/src/core/__tests__/masking.test.ts index 84f5389e928..db44191ac2d 100644 --- a/src/core/__tests__/masking.test.ts +++ b/src/core/__tests__/masking.test.ts @@ -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 { diff --git a/src/core/masking.ts b/src/core/masking.ts index a6d288b9653..2ead4710af2 100644 --- a/src/core/masking.ts +++ b/src/core/masking.ts @@ -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,