Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure operation.getContext and operation.setContext functions are callable for links after removeTypenameFromVariables #10919

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/popular-beers-move.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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