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" +} 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/formatModule.ts b/packages/apollo-react-relay-duct-tape-compiler/src/formatModule.ts index 2045204b4..59eb2116d 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,11 @@ export async function formatModuleFactory( const originalDocument = parse(docText, { noLocation: true }); const optimizedDocument = optimizeDocumentNode(originalDocument); - if (!emitNarrowObservables) { - exports.executionQueryDocument = optimizedDocument; - } else { - if (!moduleName.endsWith("WatchNodeQuery.graphql")) { - exports.executionQueryDocument = - stripFragmentReferenceFieldSelectionTransform(optimizedDocument); - } - + if (!moduleName.endsWith("WatchNodeQuery.graphql")) { + exports.executionQueryDocument = + stripFragmentReferenceFieldSelectionTransform(optimizedDocument); + } + if (emitNarrowObservables) { invariant(schema, "Expected a schema instance"); exports.watchQueryDocument = reduceNodeWatchQueryTransform( schema, 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 + } + } + `), + ); + }); });