This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 790
/
mutation-hoc.tsx
110 lines (101 loc) · 3.51 KB
/
mutation-hoc.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React from 'react';
import { DocumentNode } from 'graphql';
import hoistNonReactStatics from 'hoist-non-react-statics';
import {
parser,
BaseMutationOptions,
MutationFunction,
MutationResult
} from '@apollo/react-common';
import { Mutation } from '@apollo/react-components';
import {
defaultMapPropsToOptions,
getDisplayName,
calculateVariablesFromProps,
GraphQLBase
} from './hoc-utils';
import { OperationOption, OptionProps, MutateProps } from './types';
export function withMutation<
TProps extends TGraphQLVariables | {} = {},
TData = {},
TGraphQLVariables = {},
TChildProps = MutateProps<TData, TGraphQLVariables>
>(
document: DocumentNode,
operationOptions: OperationOption<
TProps,
TData,
TGraphQLVariables,
TChildProps
> = {}
) {
// this is memoized so if coming from `graphql` there is nearly no extra cost
const operation = parser(document);
// extract options
const {
options = defaultMapPropsToOptions,
alias = 'Apollo'
} = operationOptions;
let mapPropsToOptions = options as (props: any) => BaseMutationOptions;
if (typeof mapPropsToOptions !== 'function')
mapPropsToOptions = () => options as BaseMutationOptions;
return (
WrappedComponent: React.ComponentType<TProps & TChildProps>
): React.ComponentClass<TProps> => {
const graphQLDisplayName = `${alias}(${getDisplayName(WrappedComponent)})`;
class GraphQL extends GraphQLBase<TProps, TChildProps> {
static displayName = graphQLDisplayName;
static WrappedComponent = WrappedComponent;
render() {
let props = this.props as TProps;
const opts = mapPropsToOptions(props);
if (operationOptions.withRef) {
this.withRef = true;
props = Object.assign({}, props, {
ref: this.setWrappedInstance
});
}
if (!opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (
<Mutation ignoreResults {...opts} mutation={document}>
{(
mutate: MutationFunction<TData, TGraphQLVariables>,
{ data, ...r }: MutationResult<TData>
) => {
// the HOC's historically hoisted the data from the execution result
// up onto the result since it was passed as a nested prop
// we massage the Mutation component's shape here to replicate that
// this matches the query HoC
const result = Object.assign(r, data || {});
const name = operationOptions.name || 'mutate';
const resultName = operationOptions.name
? `${name}Result`
: 'result';
let childProps = ({
[name]: mutate,
[resultName]: result
} as any) as TChildProps;
if (operationOptions.props) {
const newResult: OptionProps<
TProps,
TData,
TGraphQLVariables
> = {
[name]: mutate,
[resultName]: result,
ownProps: props
};
childProps = operationOptions.props(newResult) as any;
}
return <WrappedComponent {...props} {...childProps} />;
}}
</Mutation>
);
}
}
// Make sure we preserve any custom statics on the original component.
return hoistNonReactStatics(GraphQL, WrappedComponent, {});
};
}