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

Commit 6adc799

Browse files
committed
Adds mutation result to mutation hoc
1 parent e5f272b commit 6adc799

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/mutation-hoc.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ export function withMutation<
5656

5757
return (
5858
<Mutation {...opts} mutation={document} ignoreResults>
59-
{(mutate, _result) => {
59+
{(mutate, { data, ...r }) => {
60+
// the HOC's historically hoisted the data from the execution result
61+
// up onto the result since it was passed as a nested prop
62+
// we massage the Mutation component's shape here to replicate that
63+
// this matches the query HoC
64+
const result = Object.assign(r, data || {});
6065
const name = operationOptions.name || 'mutate';
61-
let childProps = { [name]: mutate };
66+
const resultName = operationOptions.name ? `${name}Result` : 'result';
67+
let childProps = { [name]: mutate, [resultName]: result };
6268
if (operationOptions.props) {
6369
const newResult: OptionProps<TProps, TData, TGraphQLVariables> = {
6470
[name]: mutate,
71+
[resultName]: result,
6572
ownProps: props,
6673
};
6774
childProps = operationOptions.props(newResult) as any;
@@ -70,7 +77,7 @@ export function withMutation<
7077
return (
7178
<WrappedComponent
7279
{...props as TProps}
73-
{...childProps as any}
80+
{...childProps as TChildProps}
7481
/>
7582
);
7683
}}

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import ApolloClient, {
1010
PureQueryOptions,
1111
MutationUpdaterFn,
1212
} from 'apollo-client';
13-
import { MutationFn } from './Mutation';
13+
import { MutationFn, MutationResult } from './Mutation';
1414

1515
export interface Context {
1616
[key: string]: any;
@@ -94,6 +94,7 @@ export interface DataProps<TData, TGraphQLVariables = OperationVariables> {
9494
// export to allow usage individually for simple components
9595
export interface MutateProps<TData = any, TGraphQLVariables = OperationVariables> {
9696
mutate: MutationFn<TData, TGraphQLVariables>;
97+
result: MutationResult<TData>
9798
}
9899

99100
export type ChildProps<TProps = {}, TData = {}, TGraphQLVariables = OperationVariables> = TProps &

test/client/graphql/mutations/index.test.tsx

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,55 @@ describe('graphql(mutation)', () => {
4545
});
4646

4747
it('binds a mutation to props', () => {
48-
const ContainerWithData = graphql(query)(({ mutate }) => {
48+
const ContainerWithData = graphql(query)(({ mutate, result }) => {
4949
expect(mutate).toBeTruthy();
50+
expect(result).toBeTruthy();
5051
expect(typeof mutate).toBe('function');
52+
expect(typeof result).toBe('object');
53+
return null;
54+
});
55+
56+
renderer.create(
57+
<ApolloProvider client={client}>
58+
<ContainerWithData />
59+
</ApolloProvider>,
60+
);
61+
});
62+
63+
it('binds a mutation result to props', () => {
64+
type InjectedProps = {
65+
result: any;
66+
};
67+
68+
const ContainerWithData = graphql<{}, Data, Variables, InjectedProps>(query)(({ result }) => {
69+
const { loading, error } = result;
70+
expect(result).toBeTruthy();
71+
expect(typeof loading).toBe('boolean');
72+
expect(error).toBeFalsy();
73+
74+
return null;
75+
});
76+
77+
renderer.create(
78+
<ApolloProvider client={client}>
79+
<ContainerWithData />
80+
</ApolloProvider>,
81+
);
82+
});
83+
84+
it('binds a mutation to props with a custom name', () => {
85+
interface Props {};
86+
87+
type InjectedProps = {
88+
customMutation: any;
89+
customMutationResult: any;
90+
};
91+
92+
const ContainerWithData = graphql<Props, Data, Variables, InjectedProps>(query, { name: 'customMutation' })(({ customMutation, customMutationResult }) => {
93+
expect(customMutation).toBeTruthy();
94+
expect(customMutationResult).toBeTruthy();
95+
expect(typeof customMutation).toBe('function');
96+
expect(typeof customMutationResult).toBe('object');
5197
return null;
5298
});
5399

0 commit comments

Comments
 (0)