From b8c7c34a466d6ec1999c118b5592cc0dc8c994f1 Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Thu, 8 Jan 2026 02:32:05 +0530 Subject: [PATCH 1/4] Strip __fragments field for non-query operations from fat/broad query --- .../src/formatModule.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts b/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts index 2045204b4..8870255fc 100644 --- a/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts +++ b/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts @@ -68,14 +68,14 @@ export async function formatModuleFactory( const originalDocument = parse(docText, { noLocation: true }); const optimizedDocument = optimizeDocumentNode(originalDocument); + if (!moduleName.endsWith("WatchNodeQuery.graphql")) { + exports.executionQueryDocument = + stripFragmentReferenceFieldSelectionTransform(optimizedDocument); + } + if (!emitNarrowObservables) { exports.executionQueryDocument = optimizedDocument; } else { - if (!moduleName.endsWith("WatchNodeQuery.graphql")) { - exports.executionQueryDocument = - stripFragmentReferenceFieldSelectionTransform(optimizedDocument); - } - invariant(schema, "Expected a schema instance"); exports.watchQueryDocument = reduceNodeWatchQueryTransform( schema, From 72eb582a0e8be1ca58c30cdc1c067438bf70f89e Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Thu, 8 Jan 2026 03:00:42 +0530 Subject: [PATCH 2/4] Skip override of executionQuery by optimizedDocument --- .../src/formatModule.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts b/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts index 8870255fc..59eb2116d 100644 --- a/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts +++ b/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts @@ -72,10 +72,7 @@ export async function formatModuleFactory( exports.executionQueryDocument = stripFragmentReferenceFieldSelectionTransform(optimizedDocument); } - - if (!emitNarrowObservables) { - exports.executionQueryDocument = optimizedDocument; - } else { + if (emitNarrowObservables) { invariant(schema, "Expected a schema instance"); exports.watchQueryDocument = reduceNodeWatchQueryTransform( schema, From e19f8f337c85fe7a465f5214a971e81979cd6640 Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Thu, 8 Jan 2026 03:16:38 +0530 Subject: [PATCH 3/4] Change files --- ...tape-compiler-2c268599-dadd-40ed-a1bf-1d6f76eacc46.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/@graphitation-apollo-react-relay-duct-tape-compiler-2c268599-dadd-40ed-a1bf-1d6f76eacc46.json diff --git a/change/@graphitation-apollo-react-relay-duct-tape-compiler-2c268599-dadd-40ed-a1bf-1d6f76eacc46.json b/change/@graphitation-apollo-react-relay-duct-tape-compiler-2c268599-dadd-40ed-a1bf-1d6f76eacc46.json new file mode 100644 index 000000000..15536e466 --- /dev/null +++ b/change/@graphitation-apollo-react-relay-duct-tape-compiler-2c268599-dadd-40ed-a1bf-1d6f76eacc46.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "Strip __fragments field for non-query operations from fat/broad query", + "packageName": "@graphitation/apollo-react-relay-duct-tape-compiler", + "email": "ikjotdhody@microsoft.com", + "dependentChangeType": "patch" +} From 08860ceb2819033f7ec003a8fd6978661b87b9d6 Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Thu, 8 Jan 2026 03:40:59 +0530 Subject: [PATCH 4/4] Tests to ensure __fragments are stripped from the execution documents in subscription/mutation operations. --- .../src/__tests__/formatModule.test.ts | 62 +++++++++++++++++++ ...ntReferenceFieldSelectionTransform.test.ts | 44 +++++++++++++ 2 files changed, 106 insertions(+) diff --git a/packages/apollo-react-relay-duct-tape-compiler/src/__tests__/formatModule.test.ts b/packages/apollo-react-relay-duct-tape-compiler/src/__tests__/formatModule.test.ts index fca1fc043..2da6f083b 100644 --- a/packages/apollo-react-relay-duct-tape-compiler/src/__tests__/formatModule.test.ts +++ b/packages/apollo-react-relay-duct-tape-compiler/src/__tests__/formatModule.test.ts @@ -780,6 +780,68 @@ describe("formatModule", () => { `); }); + it("strips __fragments from subscription execution documents", async () => { + const result = await formatModule( + { + emitDocuments: true, + emitSupermassiveDocuments: false, + emitNarrowObservables: true, + }, + { + definition: { + kind: "Request", + root: { + kind: "Root", + operation: "subscription", + }, + } as Request, + typeText: `export type TestSubscription = {};`, + docText: ` + subscription TestSubscription { + userUpdated { + id + ... on Node { + __fragments @client + } + } + } + `, + }, + ); + expect(result).not.toContain("__fragments"); + }); + + it("strips __fragments from mutation execution documents", async () => { + const result = await formatModule( + { + emitDocuments: true, + emitSupermassiveDocuments: false, + emitNarrowObservables: true, + }, + { + definition: { + kind: "Request", + root: { + kind: "Root", + operation: "mutation", + }, + } as Request, + typeText: `export type TestMutation = {};`, + docText: ` + mutation TestMutation($id: ID!, $name: String!) { + updateUser(id: $id, name: $name) { + id + ... on Node { + __fragments @client + } + } + } + `, + }, + ); + expect(result).not.toContain("__fragments"); + }); + describe("--unstable_emitExecutionDocumentText=true", () => { it("adds document text with --emitDocuments=true", async () => { expect( diff --git a/packages/apollo-react-relay-duct-tape-compiler/src/formatModuleTransforms/__tests__/stripFragmentReferenceFieldSelectionTransform.test.ts b/packages/apollo-react-relay-duct-tape-compiler/src/formatModuleTransforms/__tests__/stripFragmentReferenceFieldSelectionTransform.test.ts index 4949bba31..cbf6a60d9 100644 --- a/packages/apollo-react-relay-duct-tape-compiler/src/formatModuleTransforms/__tests__/stripFragmentReferenceFieldSelectionTransform.test.ts +++ b/packages/apollo-react-relay-duct-tape-compiler/src/formatModuleTransforms/__tests__/stripFragmentReferenceFieldSelectionTransform.test.ts @@ -48,4 +48,48 @@ describe(stripFragmentReferenceFieldSelectionTransform, () => { `), ); }); + + it("removes __fragments from subscription documents", () => { + const result = stripFragmentReferenceFieldSelectionTransform(graphql` + subscription UserUpdated { + userUpdated { + id + ... on Node { + __fragments @client + } + } + } + `); + expect(print(result)).toEqual( + print(graphql` + subscription UserUpdated { + userUpdated { + id + } + } + `), + ); + }); + + it("removes __fragments from mutation documents", () => { + const result = stripFragmentReferenceFieldSelectionTransform(graphql` + mutation UpdateUser($id: ID!, $name: String!) { + updateUser(id: $id, name: $name) { + id + ... on Node { + __fragments @client + } + } + } + `); + expect(print(result)).toEqual( + print(graphql` + mutation UpdateUser($id: ID!, $name: String!) { + updateUser(id: $id, name: $name) { + id + } + } + `), + ); + }); });