Skip to content

Commit

Permalink
Ensure operation.getContext and operation.setContext functions ar…
Browse files Browse the repository at this point in the history
…e callable for links after `removeTypenameFromVariables` (#10919)
  • Loading branch information
jerelmiller committed May 30, 2023
1 parent a445e44 commit f796ce1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .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.
21 changes: 21 additions & 0 deletions src/link/remove-typename/__tests__/removeTypenameFromVariables.ts
Expand Up @@ -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');
});
13 changes: 5 additions & 8 deletions src/link/remove-typename/removeTypenameFromVariables.ts
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit f796ce1

Please sign in to comment.