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

Fix: Support useMutation client param passed to execute function #9438

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
699cb8e
Fix: Support useMutation client param passed to execute function
dciesielkiewicz Feb 17, 2022
295145c
Write tests for useMutation client passed as a param to execute function
dciesielkiewicz Feb 17, 2022
c9fdb18
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Feb 21, 2022
f026e58
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Feb 25, 2022
eb8c9b9
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Feb 28, 2022
d9e1619
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Mar 10, 2022
ce49bee
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Mar 15, 2022
a8770ea
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Mar 23, 2022
1451e48
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Apr 13, 2022
999da23
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Apr 25, 2022
a4bcac2
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz May 4, 2022
efa7a1b
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Jun 10, 2022
c50373c
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Jun 11, 2022
f4e5495
Merge branch 'main' into fix-support-use-mutation-client-param
dciesielkiewicz Jun 30, 2022
765f825
Merge branch 'main' into fix-support-use-mutation-client-param
jerelmiller Feb 20, 2023
e1fa276
Update documentation to explicitly list `client` as an override option.
jerelmiller Feb 20, 2023
4341208
Add changeset for #9438
jerelmiller Feb 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-countries-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Ensure the `client` option passed to `useMutation`'s execute function is used when provided. Previously this option was ignored.
1 change: 1 addition & 0 deletions docs/shared/mutation-result.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ A function to trigger the mutation from your UI. You can optionally pass this fu
* `refetchQueries`
* `update`
* `variables`
* `client`

Any option you pass here overrides any existing value for that option that you passed to `useMutation`.

Expand Down
31 changes: 31 additions & 0 deletions src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,37 @@ describe('useMutation Hook', () => {
expect(result.current[1].client).toBeInstanceOf(ApolloClient);
});

it ('should call client passed to execute function', async () => {
const { result } = renderHook(
() => useMutation(CREATE_TODO_MUTATION),
{ wrapper: ({ children }) => (
<MockedProvider>
{children}
</MockedProvider>
)},
);

const link = mockSingleLink();
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link
});

const mutateSpy = jest.spyOn(client, 'mutate').mockImplementation(
() => new Promise((resolve) => {
resolve({ data: CREATE_TODO_RESULT })
})
);

const createTodo = result.current[0];
await act(async () => {
await createTodo({ client });
});

expect(mutateSpy).toHaveBeenCalledTimes(1);
});

it('should merge provided variables', async () => {
const CREATE_TODO_DATA = {
createTodo: {
Expand Down
4 changes: 3 additions & 1 deletion src/react/hooks/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ export function useMutation<
TCache
> = {}
) => {
const {client, options, mutation} = ref.current;
const {options, mutation} = ref.current;
const baseOptions = { ...options, mutation };
const client = executeOptions.client || ref.current.client;

if (!ref.current.result.loading && !baseOptions.ignoreResults && ref.current.isMounted) {
setResult(ref.current.result = {
loading: true,
Expand Down