1 | import { __assign } from "tslib";
|
2 | import { useCallback, useEffect, useRef, useState } from 'react';
|
3 | import { mergeOptions, } from "../../core/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 | export function useMutation(mutation, options) {
|
9 | var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
10 | verifyDocumentType(mutation, DocumentType.Mutation);
|
11 | var _a = useState({
|
12 | called: false,
|
13 | loading: false,
|
14 | client: client,
|
15 | }), result = _a[0], setResult = _a[1];
|
16 | var ref = useRef({
|
17 | result: result,
|
18 | mutationId: 0,
|
19 | isMounted: true,
|
20 | client: client,
|
21 | mutation: mutation,
|
22 | options: options,
|
23 | });
|
24 | {
|
25 | Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
26 | }
|
27 | var execute = useCallback(function (executeOptions) {
|
28 | if (executeOptions === void 0) { executeOptions = {}; }
|
29 | var _a = ref.current, client = _a.client, options = _a.options, mutation = _a.mutation;
|
30 | var baseOptions = __assign(__assign({}, options), { mutation: mutation });
|
31 | if (!ref.current.result.loading && !baseOptions.ignoreResults) {
|
32 | setResult(ref.current.result = {
|
33 | loading: true,
|
34 | error: void 0,
|
35 | data: void 0,
|
36 | called: true,
|
37 | client: client,
|
38 | });
|
39 | }
|
40 | var mutationId = ++ref.current.mutationId;
|
41 | var clientOptions = mergeOptions(baseOptions, executeOptions);
|
42 | return client.mutate(clientOptions).then(function (response) {
|
43 | var _a, _b, _c;
|
44 | var data = response.data, errors = response.errors;
|
45 | var error = errors && errors.length > 0
|
46 | ? new ApolloError({ graphQLErrors: errors })
|
47 | : void 0;
|
48 | if (mutationId === ref.current.mutationId &&
|
49 | !clientOptions.ignoreResults) {
|
50 | var result_1 = {
|
51 | called: true,
|
52 | loading: false,
|
53 | data: data,
|
54 | error: error,
|
55 | client: client,
|
56 | };
|
57 | if (ref.current.isMounted && !equal(ref.current.result, result_1)) {
|
58 | setResult(ref.current.result = result_1);
|
59 | }
|
60 | }
|
61 | (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted) === null || _b === void 0 ? void 0 : _b.call(_a, response.data);
|
62 | (_c = executeOptions.onCompleted) === null || _c === void 0 ? void 0 : _c.call(executeOptions, response.data);
|
63 | return response;
|
64 | }).catch(function (error) {
|
65 | var _a, _b, _c, _d;
|
66 | if (mutationId === ref.current.mutationId &&
|
67 | ref.current.isMounted) {
|
68 | var result_2 = {
|
69 | loading: false,
|
70 | error: error,
|
71 | data: void 0,
|
72 | called: true,
|
73 | client: client,
|
74 | };
|
75 | if (!equal(ref.current.result, result_2)) {
|
76 | setResult(ref.current.result = result_2);
|
77 | }
|
78 | }
|
79 | if (((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) || clientOptions.onError) {
|
80 | (_c = (_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onError) === null || _c === void 0 ? void 0 : _c.call(_b, error);
|
81 | (_d = executeOptions.onError) === null || _d === void 0 ? void 0 : _d.call(executeOptions, error);
|
82 | return { data: void 0, errors: error };
|
83 | }
|
84 | throw error;
|
85 | });
|
86 | }, []);
|
87 | var reset = useCallback(function () {
|
88 | setResult({ called: false, loading: false, client: client });
|
89 | }, []);
|
90 | useEffect(function () {
|
91 | ref.current.isMounted = true;
|
92 | return function () {
|
93 | ref.current.isMounted = false;
|
94 | };
|
95 | }, []);
|
96 | return [execute, __assign({ reset: reset }, result)];
|
97 | }
|
98 |
|
\ | No newline at end of file |