UNPKG

6.04 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import * as React from "rehackt";
3import { mergeOptions } from "../../utilities/index.js";
4import { equal } from "@wry/equality";
5import { DocumentType, verifyDocumentType } from "../parser/index.js";
6import { ApolloError } from "../../errors/index.js";
7import { useApolloClient } from "./useApolloClient.js";
8import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
9/**
10 *
11 *
12 * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.
13 *
14 * @example
15 * ```jsx
16 * import { gql, useMutation } from '@apollo/client';
17 *
18 * const ADD_TODO = gql`
19 * mutation AddTodo($type: String!) {
20 * addTodo(type: $type) {
21 * id
22 * type
23 * }
24 * }
25 * `;
26 *
27 * function AddTodo() {
28 * let input;
29 * const [addTodo, { data }] = useMutation(ADD_TODO);
30 *
31 * return (
32 * <div>
33 * <form
34 * onSubmit={e => {
35 * e.preventDefault();
36 * addTodo({ variables: { type: input.value } });
37 * input.value = '';
38 * }}
39 * >
40 * <input
41 * ref={node => {
42 * input = node;
43 * }}
44 * />
45 * <button type="submit">Add Todo</button>
46 * </form>
47 * </div>
48 * );
49 * }
50 * ```
51 * @since 3.0.0
52 * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.
53 * @param options - Options to control how the mutation is executed.
54 * @returns A tuple in the form of `[mutate, result]`
55 */
56export 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 // TODO(brian): why are we returning this here???
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//# sourceMappingURL=useMutation.js.map
\No newline at end of file