From f796ce1ac72f31a951a1d0f0b78d19dd039a6398 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 30 May 2023 12:20:38 -0600 Subject: [PATCH] Ensure `operation.getContext` and `operation.setContext` functions are callable for links after `removeTypenameFromVariables` (#10919) --- .changeset/popular-beers-move.md | 5 +++++ .../__tests__/removeTypenameFromVariables.ts | 21 +++++++++++++++++++ .../removeTypenameFromVariables.ts | 13 +++++------- 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 .changeset/popular-beers-move.md diff --git a/.changeset/popular-beers-move.md b/.changeset/popular-beers-move.md new file mode 100644 index 00000000000..6ee6221f03a --- /dev/null +++ b/.changeset/popular-beers-move.md @@ -0,0 +1,5 @@ +--- +'@apollo/client': patch +--- + +Fix an issue when using a link that relied on `operation.getContext` and `operation.setContext` would error out when it was declared after the `removeTypenameFromVariables` link. diff --git a/src/link/remove-typename/__tests__/removeTypenameFromVariables.ts b/src/link/remove-typename/__tests__/removeTypenameFromVariables.ts index 0d4140087c1..e7f7aedf14a 100644 --- a/src/link/remove-typename/__tests__/removeTypenameFromVariables.ts +++ b/src/link/remove-typename/__tests__/removeTypenameFromVariables.ts @@ -517,3 +517,24 @@ test('handles when declared variables are unused', async () => { }, }); }); + +test('ensures operation.getContext and operation.setContext functions are properly forwarded', async () => { + const query = gql` + query Test($foo: FooInput) { + someField(foo: $foo) + } + `; + + const link = removeTypenameFromVariables(); + + const operationWithoutVariables = await execute(link, { query }); + const operationWithVariables = await execute(link, { + query, + variables: { foo: { __typename: 'FooInput', bar: true } }, + }); + + expect(typeof operationWithoutVariables.getContext).toBe('function'); + expect(typeof operationWithoutVariables.setContext).toBe('function'); + expect(typeof operationWithVariables.getContext).toBe('function'); + expect(typeof operationWithVariables.setContext).toBe('function'); +}); diff --git a/src/link/remove-typename/removeTypenameFromVariables.ts b/src/link/remove-typename/removeTypenameFromVariables.ts index 52571d48af8..72bb8bc0b0f 100644 --- a/src/link/remove-typename/removeTypenameFromVariables.ts +++ b/src/link/remove-typename/removeTypenameFromVariables.ts @@ -22,16 +22,13 @@ export function removeTypenameFromVariables( const { except } = options; const { query, variables } = operation; - if (!variables) { - return forward(operation); + if (variables) { + operation.variables = except + ? maybeStripTypenameUsingConfig(query, variables, except) + : stripTypename(variables); } - return forward({ - ...operation, - variables: except - ? maybeStripTypenameUsingConfig(query, variables, except) - : stripTypename(variables), - }); + return forward(operation); }); }