1 | import { __assign } from "tslib";
|
2 | import * as React from "rehackt";
|
3 | import { mergeOptions } from "../../utilities/index.js";
|
4 | import { equal } from "@wry/equality";
|
5 | import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
6 | import { ApolloError } from "../../errors/index.js";
|
7 | import { useApolloClient } from "./useApolloClient.js";
|
8 | import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
|
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 | export function useMutation(mutation, options) {
|
57 | var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
58 | verifyDocumentType(mutation, DocumentType.Mutation);
|
59 | var _a = React.useState({
|
60 | called: false,
|
61 | loading: false,
|
62 | client: client,
|
63 | }), result = _a[0], setResult = _a[1];
|
64 | var ref = React.useRef({
|
65 | result: result,
|
66 | mutationId: 0,
|
67 | isMounted: true,
|
68 | client: client,
|
69 | mutation: mutation,
|
70 | options: options,
|
71 | });
|
72 | useIsomorphicLayoutEffect(function () {
|
73 | Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
74 | });
|
75 | var execute = React.useCallback(function (executeOptions) {
|
76 | if (executeOptions === void 0) { executeOptions = {}; }
|
77 | var _a = ref.current, options = _a.options, mutation = _a.mutation;
|
78 | var baseOptions = __assign(__assign({}, options), { mutation: mutation });
|
79 | var client = executeOptions.client || ref.current.client;
|
80 | if (!ref.current.result.loading &&
|
81 | !baseOptions.ignoreResults &&
|
82 | ref.current.isMounted) {
|
83 | setResult((ref.current.result = {
|
84 | loading: true,
|
85 | error: void 0,
|
86 | data: void 0,
|
87 | called: true,
|
88 | client: client,
|
89 | }));
|
90 | }
|
91 | var mutationId = ++ref.current.mutationId;
|
92 | var clientOptions = mergeOptions(baseOptions, executeOptions);
|
93 | return client
|
94 | .mutate(clientOptions)
|
95 | .then(function (response) {
|
96 | var _a, _b;
|
97 | var data = response.data, errors = response.errors;
|
98 | var error = errors && errors.length > 0 ?
|
99 | new ApolloError({ graphQLErrors: errors })
|
100 | : void 0;
|
101 | var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);
|
102 | if (error && onError) {
|
103 | onError(error, clientOptions);
|
104 | }
|
105 | if (mutationId === ref.current.mutationId &&
|
106 | !clientOptions.ignoreResults) {
|
107 | var result_1 = {
|
108 | called: true,
|
109 | loading: false,
|
110 | data: data,
|
111 | error: error,
|
112 | client: client,
|
113 | };
|
114 | if (ref.current.isMounted && !equal(ref.current.result, result_1)) {
|
115 | setResult((ref.current.result = result_1));
|
116 | }
|
117 | }
|
118 | var onCompleted = executeOptions.onCompleted || ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onCompleted);
|
119 | if (!error) {
|
120 | onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response.data, clientOptions);
|
121 | }
|
122 | return response;
|
123 | })
|
124 | .catch(function (error) {
|
125 | var _a;
|
126 | if (mutationId === ref.current.mutationId && ref.current.isMounted) {
|
127 | var result_2 = {
|
128 | loading: false,
|
129 | error: error,
|
130 | data: void 0,
|
131 | called: true,
|
132 | client: client,
|
133 | };
|
134 | if (!equal(ref.current.result, result_2)) {
|
135 | setResult((ref.current.result = result_2));
|
136 | }
|
137 | }
|
138 | var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);
|
139 | if (onError) {
|
140 | onError(error, clientOptions);
|
141 |
|
142 | return { data: void 0, errors: error };
|
143 | }
|
144 | throw error;
|
145 | });
|
146 | }, []);
|
147 | var reset = React.useCallback(function () {
|
148 | if (ref.current.isMounted) {
|
149 | var result_3 = {
|
150 | called: false,
|
151 | loading: false,
|
152 | client: ref.current.client,
|
153 | };
|
154 | Object.assign(ref.current, { mutationId: 0, result: result_3 });
|
155 | setResult(result_3);
|
156 | }
|
157 | }, []);
|
158 | React.useEffect(function () {
|
159 | var current = ref.current;
|
160 | current.isMounted = true;
|
161 | return function () {
|
162 | current.isMounted = false;
|
163 | };
|
164 | }, []);
|
165 | return [execute, __assign({ reset: reset }, result)];
|
166 | }
|
167 |
|
\ | No newline at end of file |