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

Commit 66d1e18

Browse files
committed
Pass result of mutation to the graphql HOC wrapped component
Based on #3008.
1 parent 45767f6 commit 66d1e18

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

packages/hoc/src/__tests__/mutations/index.test.tsx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,60 @@ describe('graphql(mutation)', () => {
4949
});
5050

5151
it('binds a mutation to props', () => {
52-
const ContainerWithData = graphql(query)(({ mutate }) => {
52+
const ContainerWithData = graphql(query)(({ mutate, result }) => {
5353
expect(mutate).toBeTruthy();
54+
expect(result).toBeTruthy();
5455
expect(typeof mutate).toBe('function');
56+
expect(typeof result).toBe('object');
57+
return null;
58+
});
59+
60+
render(
61+
<ApolloProvider client={client}>
62+
<ContainerWithData />
63+
</ApolloProvider>
64+
);
65+
});
66+
67+
it('binds a mutation result to props', () => {
68+
type InjectedProps = {
69+
result: any;
70+
};
71+
72+
const ContainerWithData = graphql<{}, Data, Variables, InjectedProps>(
73+
query
74+
)(({ result }) => {
75+
const { loading, error } = result;
76+
expect(result).toBeTruthy();
77+
expect(typeof loading).toBe('boolean');
78+
expect(error).toBeFalsy();
79+
80+
return null;
81+
});
82+
83+
render(
84+
<ApolloProvider client={client}>
85+
<ContainerWithData />
86+
</ApolloProvider>
87+
);
88+
});
89+
90+
it('binds a mutation to props with a custom name', () => {
91+
interface Props {}
92+
93+
type InjectedProps = {
94+
customMutation: any;
95+
customMutationResult: any;
96+
};
97+
98+
const ContainerWithData = graphql<Props, Data, Variables, InjectedProps>(
99+
query,
100+
{ name: 'customMutation' }
101+
)(({ customMutation, customMutationResult }) => {
102+
expect(customMutation).toBeTruthy();
103+
expect(customMutationResult).toBeTruthy();
104+
expect(typeof customMutation).toBe('function');
105+
expect(typeof customMutationResult).toBe('object');
55106
return null;
56107
});
57108

packages/hoc/src/mutation-hoc.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React from 'react';
22
import { DocumentNode } from 'graphql';
33
import hoistNonReactStatics from 'hoist-non-react-statics';
4-
import { parser, BaseMutationOptions } from '@apollo/react-common';
4+
import {
5+
parser,
6+
BaseMutationOptions,
7+
MutationFunction,
8+
MutationResult
9+
} from '@apollo/react-common';
510
import { Mutation } from '@apollo/react-components';
611

712
import {
@@ -62,16 +67,31 @@ export function withMutation<
6267

6368
return (
6469
<Mutation {...opts} mutation={document} ignoreResults>
65-
{(mutate: any, _result: any) => {
70+
{(
71+
mutate: MutationFunction<TData, TGraphQLVariables>,
72+
{ data, ...r }: MutationResult<TData>
73+
) => {
74+
// the HOC's historically hoisted the data from the execution result
75+
// up onto the result since it was passed as a nested prop
76+
// we massage the Mutation component's shape here to replicate that
77+
// this matches the query HoC
78+
const result = Object.assign(r, data || {});
6679
const name = operationOptions.name || 'mutate';
67-
let childProps = { [name]: mutate };
80+
const resultName = operationOptions.name
81+
? `${name}Result`
82+
: 'result';
83+
let childProps = {
84+
[name]: mutate,
85+
[resultName]: result
86+
};
6887
if (operationOptions.props) {
6988
const newResult: OptionProps<
7089
TProps,
7190
TData,
7291
TGraphQLVariables
7392
> = {
7493
[name]: mutate,
94+
[resultName]: result,
7595
ownProps: props
7696
};
7797
childProps = operationOptions.props(newResult) as any;

packages/hoc/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
OperationVariables,
1111
MutationFunction,
1212
BaseQueryOptions,
13-
BaseMutationOptions
13+
BaseMutationOptions,
14+
MutationResult
1415
} from '@apollo/react-common';
1516

1617
export interface QueryControls<
@@ -50,6 +51,7 @@ export interface MutateProps<
5051
TGraphQLVariables = OperationVariables
5152
> {
5253
mutate: MutationFunction<TData, TGraphQLVariables>;
54+
result: MutationResult<TData>;
5355
}
5456

5557
export type ChildProps<

0 commit comments

Comments
 (0)