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

Commit

Permalink
Fixed withMutation hoc allowing updates to result (#3431)
Browse files Browse the repository at this point in the history
* Fixed withMutation hoc allowing updates to result

Fixes #3250

* Allow internal `ignoreResults` setting to be overridden

In some cases it can be beneficial to receive result updates in
`withMutation`. This commit tweaks `withMutation` such that
developers can override the currently always on `ignoreResults`
setting.

* Changelog update
  • Loading branch information
tim-stasse authored and hwillson committed Sep 13, 2019
1 parent 9541f3c commit db0fd27
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -6,6 +6,8 @@

- Calling `startPolling` or `stopPolling` after a component has unmounted is now a no-op (instead of throwing an exception). Polling is automatically stopped when a component is unmounted, so it doesn't need to be called manually. <br/>
[@hwillson](https://github.com/hwillson) in [#3485](https://github.com/apollographql/react-apollo/pull/3485)
- Allow `ignoreResults` to be controlled through `graphql` and `withMutation` options. <br/>
[@tim-stasse](https://github.com/tim-stasse) in [#3431](https://github.com/apollographql/react-apollo/pull/3431)

### Bug Fixes

Expand Down
80 changes: 79 additions & 1 deletion packages/hoc/src/__tests__/mutations/queries.test.tsx
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '@testing-library/react';
import { render, cleanup, wait } from '@testing-library/react';
import gql from 'graphql-tag';
import ApolloClient, { MutationUpdaterFn } from 'apollo-client';
import { InMemoryCache as Cache } from 'apollo-cache-inmemory';
Expand Down Expand Up @@ -315,4 +315,82 @@ describe('graphql(mutation) query integration', () => {
</ApolloProvider>
);
});

it('should be able to override the internal `ignoreResults` setting', async () => {
const mutation: DocumentNode = gql`
mutation($signature: String!) {
mini: submitMiniCoverS3DirectUpload(signature: $signature) {
__typename
id
cover(maxWidth: 600, maxHeight: 400)
}
}
`;

const mutationData = {
mini: {
id: 1,
cover: 'image2',
__typename: 'Mini'
}
};

type MutationData = typeof mutationData;

interface MutationVariables {
signature: string;
}

const link = mockSingleLink({
request: { query: mutation, variables: { signature: '1233' } },
result: { data: mutationData }
});

const cache = new Cache({ addTypename: false });
const client = new ApolloClient({ link, cache });

let renderCount = 0;
const MutationContainer = graphql<MutationVariables, MutationData>(
mutation,
{ options: { ignoreResults: false } }
)(
class extends React.Component<
ChildProps<MutationVariables, MutationData>
> {
render() {
switch (renderCount) {
case 0:
expect(this.props.result!.loading).toBeFalsy();
setTimeout(() => {
this.props.mutate!().then(result => {
expect(stripSymbols(result && result.data)).toEqual(
mutationData
);
});
});
break;
case 1:
expect(this.props.result!.loading).toBeTruthy();
break;
case 2:
expect(this.props.result!.loading).toBeFalsy();
default: // Do nothing
}

renderCount += 1;
return null;
}
}
);

render(
<ApolloProvider client={client}>
<MutationContainer signature="1233" />
</ApolloProvider>
);

await wait(() => {
expect(renderCount).toBe(3);
});
});
});
2 changes: 1 addition & 1 deletion packages/hoc/src/mutation-hoc.tsx
Expand Up @@ -66,7 +66,7 @@ export function withMutation<
}

return (
<Mutation {...opts} mutation={document} ignoreResults>
<Mutation ignoreResults {...opts} mutation={document}>
{(
mutate: MutationFunction<TData, TGraphQLVariables>,
{ data, ...r }: MutationResult<TData>
Expand Down

0 comments on commit db0fd27

Please sign in to comment.