-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathMutationData.ts
164 lines (143 loc) · 4.21 KB
/
MutationData.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { equal } from '@wry/equality';
import { DocumentType } from '../parser';
import { ApolloError } from '../../errors';
import {
MutationDataOptions,
MutationTuple,
MutationFunctionOptions,
MutationResult,
} from '../types/types';
import { OperationData } from './OperationData';
import { OperationVariables, MutationOptions, mergeOptions } from '../../core';
import { FetchResult } from '../../link/core';
type MutationResultWithoutClient<TData = any> = Omit<MutationResult<TData>, 'client'>;
export class MutationData<
TData = any,
TVariables = OperationVariables
> extends OperationData {
private mostRecentMutationId: number;
private result: MutationResultWithoutClient<TData>;
private previousResult?: MutationResultWithoutClient<TData>;
private setResult: (result: MutationResultWithoutClient<TData>) => any;
constructor({
options,
context,
result,
setResult
}: {
options: MutationDataOptions<TData, TVariables>;
context: any;
result: MutationResultWithoutClient<TData>;
setResult: (result: MutationResultWithoutClient<TData>) => any;
}) {
super(options, context);
this.verifyDocumentType(options.mutation, DocumentType.Mutation);
this.result = result;
this.setResult = setResult;
this.mostRecentMutationId = 0;
}
public execute(result: MutationResultWithoutClient<TData>): MutationTuple<TData, TVariables> {
this.isMounted = true;
this.verifyDocumentType(this.getOptions().mutation, DocumentType.Mutation);
return [
this.runMutation,
{ ...result, client: this.refreshClient().client }
] as MutationTuple<TData, TVariables>;
}
public afterExecute() {
this.isMounted = true;
return this.unmount.bind(this);
}
public cleanup() {
// No cleanup required.
}
private runMutation = (
mutationFunctionOptions: MutationFunctionOptions<
TData,
TVariables
> = {} as MutationFunctionOptions<TData, TVariables>
) => {
this.onMutationStart();
const mutationId = this.generateNewMutationId();
return this.mutate(mutationFunctionOptions)
.then((response: FetchResult<TData>) => {
this.onMutationCompleted(response, mutationId);
return response;
})
.catch((error: ApolloError) => {
this.onMutationError(error, mutationId);
if (!this.getOptions().onError) throw error;
});
};
private mutate(
options: MutationFunctionOptions<TData, TVariables>
) {
return this.refreshClient().client.mutate(
mergeOptions(
this.getOptions(),
options as MutationOptions<TData, TVariables>,
),
);
}
private onMutationStart() {
if (!this.result.loading && !this.getOptions().ignoreResults) {
this.updateResult({
loading: true,
error: undefined,
data: undefined,
called: true
});
}
}
private onMutationCompleted(
response: FetchResult<TData>,
mutationId: number
) {
const { onCompleted, ignoreResults } = this.getOptions();
const { data, errors } = response;
const error =
errors && errors.length > 0
? new ApolloError({ graphQLErrors: errors })
: undefined;
const callOncomplete = () =>
onCompleted ? onCompleted(data as TData) : null;
if (this.isMostRecentMutation(mutationId) && !ignoreResults) {
this.updateResult({
called: true,
loading: false,
data,
error
});
}
callOncomplete();
}
private onMutationError(error: ApolloError, mutationId: number) {
const { onError } = this.getOptions();
if (this.isMostRecentMutation(mutationId)) {
this.updateResult({
loading: false,
error,
data: undefined,
called: true
});
}
if (onError) {
onError(error);
}
}
private generateNewMutationId(): number {
return ++this.mostRecentMutationId;
}
private isMostRecentMutation(mutationId: number) {
return this.mostRecentMutationId === mutationId;
}
private updateResult(result: MutationResultWithoutClient<TData>) {
if (
this.isMounted &&
(!this.previousResult || !equal(this.previousResult, result))
) {
this.setResult(result);
this.previousResult = result;
}
}
}