From 1cdce156de8175b50d8c6c4a2716bd60ceeba7ae Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Thu, 14 Nov 2024 11:39:58 -0700 Subject: [PATCH 1/2] Ensure interface types are handled when adding accessor warnings --- src/core/__tests__/masking.test.ts | 66 ++++++++++++++++++++++++++++++ src/core/masking.ts | 7 ++++ 2 files changed, 73 insertions(+) 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, From 03a61711a2ab48379cff4e62d00a07e05f83755e Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Thu, 14 Nov 2024 11:41:15 -0700 Subject: [PATCH 2/2] Add changeset --- .changeset/nice-countries-share.md | 5 +++++ 1 file changed, 5 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.