Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

add client to the mutation result #3417

Merged
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -12,6 +12,8 @@
[@hwillson](https://github.com/hwillson) in [#3422](https://github.com/apollographql/react-apollo/pull/3422)
- Fixed problematic re-renders that were caused by using `fetchMore.updateQuery` with `notifyOnNetworkStatusChange` set to true. When `notifyOnNetworkStatusChange` is true, re-renders will now wait until `updateQuery` has completed, to make sure the updated data is used during the render. <br/>
[@hwillson](https://github.com/hwillson) in [#3433](https://github.com/apollographql/react-apollo/pull/3433)
- Add `client` to the `useMutation` result. <br/>
[@joshalling](https://github.com/joshalling) in [#3417](https://github.com/apollographql/react-apollo/pull/3417)
- Documentation fixes. <br/>
[@SeanRoberts](https://github.com/SeanRoberts) in [#3380](https://github.com/apollographql/react-apollo/pull/3380)

Expand Down
19 changes: 18 additions & 1 deletion packages/hooks/src/__tests__/useMutation.test.tsx
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect } from 'react';
import { DocumentNode } from 'graphql';
import gql from 'graphql-tag';
import { MockedProvider, mockSingleLink } from '@apollo/react-testing';
import { render, cleanup } from '@testing-library/react';
import { render, cleanup, wait } from '@testing-library/react';
import { ApolloProvider, useMutation } from '@apollo/react-hooks';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
Expand Down Expand Up @@ -236,6 +236,23 @@ describe('useMutation Hook', () => {
</MockedProvider>
);
});

it('should return the current client instance in the result object', async () => {
const Component = () => {
const [, { client }] = useMutation(CREATE_TODO_MUTATION);
expect(client).toBeDefined();
expect(client instanceof ApolloClient).toBeTruthy();
return null;
};

render(
<MockedProvider>
<Component />
</MockedProvider>
);

await wait();
});
});

describe('Optimistic response', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/hooks/src/data/MutationData.ts
Expand Up @@ -42,6 +42,7 @@ export class MutationData<
public execute(result: MutationResult<TData>) {
this.isMounted = true;
this.verifyDocumentType(this.getOptions().mutation, DocumentType.Mutation);
result.client = this.refreshClient().client;
return [this.runMutation, result] as MutationTuple<TData, TVariables>;
}

Expand Down