1 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2 |
|
3 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
4 |
|
5 | import { ApolloError } from 'apollo-client';
|
6 | import React from 'react';
|
7 | import { useApolloClient } from './ApolloContext';
|
8 | import actHack from './internal/actHack';
|
9 | import { objToKey } from './utils';
|
10 |
|
11 | var getInitialState = function getInitialState() {
|
12 | return {
|
13 | called: false,
|
14 | data: undefined,
|
15 | error: undefined,
|
16 | hasError: false,
|
17 | loading: false
|
18 | };
|
19 | };
|
20 |
|
21 | export function useMutation(mutation, baseOptions) {
|
22 | if (baseOptions === void 0) {
|
23 | baseOptions = {};
|
24 | }
|
25 |
|
26 | var client = useApolloClient(baseOptions.client);
|
27 |
|
28 | var _React$useState = React.useState(getInitialState),
|
29 | result = _React$useState[0],
|
30 | setResult = _React$useState[1];
|
31 |
|
32 | var _baseOptions = baseOptions,
|
33 | _baseOptions$rethrow = _baseOptions.rethrow,
|
34 | rethrow = _baseOptions$rethrow === void 0 ? true : _baseOptions$rethrow,
|
35 | options = _objectWithoutPropertiesLoose(_baseOptions, ["rethrow"]);
|
36 |
|
37 | var mergeResult = function mergeResult(partialResult) {
|
38 |
|
39 | actHack(function () {
|
40 | setResult(function (prev) {
|
41 | return _extends({}, prev, {}, partialResult);
|
42 | });
|
43 | });
|
44 | };
|
45 |
|
46 |
|
47 | React.useEffect(function () {
|
48 | mergeResult(getInitialState());
|
49 | }, [client]);
|
50 |
|
51 | var _useMutationTracking = useMutationTracking(),
|
52 | generateNewMutationId = _useMutationTracking.generateNewMutationId,
|
53 | isMostRecentMutation = _useMutationTracking.isMostRecentMutation;
|
54 |
|
55 | var onMutationStart = function onMutationStart() {
|
56 | if (!result.loading) {
|
57 | mergeResult({
|
58 | called: true,
|
59 | data: undefined,
|
60 | error: undefined,
|
61 | hasError: false,
|
62 | loading: true
|
63 | });
|
64 | }
|
65 | };
|
66 |
|
67 | var onMutationError = function onMutationError(error, mutationId) {
|
68 | if (isMostRecentMutation(mutationId)) {
|
69 | mergeResult({
|
70 | error: error,
|
71 | hasError: true,
|
72 | loading: false
|
73 | });
|
74 | }
|
75 | };
|
76 |
|
77 | var onMutationCompleted = function onMutationCompleted(response, mutationId) {
|
78 | var data = response.data,
|
79 | errors = response.errors;
|
80 |
|
81 | if (errors && errors.length > 0) {
|
82 | onMutationError(new ApolloError({
|
83 | graphQLErrors: errors
|
84 | }), mutationId);
|
85 | return;
|
86 | }
|
87 |
|
88 | if (isMostRecentMutation(mutationId)) {
|
89 | mergeResult({
|
90 | data: data,
|
91 | loading: false
|
92 | });
|
93 | }
|
94 | };
|
95 |
|
96 | var runMutation = React.useCallback(function (mutateOptions) {
|
97 | if (mutateOptions === void 0) {
|
98 | mutateOptions = {};
|
99 | }
|
100 |
|
101 | return new Promise(function (resolve, reject) {
|
102 | onMutationStart();
|
103 | var mutationId = generateNewMutationId();
|
104 |
|
105 |
|
106 | var mutateVariables = options.variables ? _extends({}, mutateOptions.variables, {}, options.variables) : mutateOptions.variables;
|
107 | client.mutate(_extends({
|
108 | mutation: mutation
|
109 | }, options, {}, mutateOptions, {
|
110 | variables: mutateVariables
|
111 | })).then(function (response) {
|
112 | onMutationCompleted(response, mutationId);
|
113 | resolve(response);
|
114 | })["catch"](function (err) {
|
115 | onMutationError(err, mutationId);
|
116 |
|
117 | if (rethrow) {
|
118 | reject(err);
|
119 | return;
|
120 | }
|
121 |
|
122 | resolve({});
|
123 | });
|
124 | });
|
125 | }, [client, mutation, objToKey(baseOptions)]);
|
126 | return [runMutation, result];
|
127 | }
|
128 |
|
129 | function useMutationTracking() {
|
130 | var mostRecentMutationId = React.useRef(0);
|
131 |
|
132 | var generateNewMutationId = function generateNewMutationId() {
|
133 | mostRecentMutationId.current += 1;
|
134 | return mostRecentMutationId.current;
|
135 | };
|
136 |
|
137 | var isMostRecentMutation = function isMostRecentMutation(mutationId) {
|
138 | return mostRecentMutationId.current === mutationId;
|
139 | };
|
140 |
|
141 | return {
|
142 | generateNewMutationId: generateNewMutationId,
|
143 | isMostRecentMutation: isMostRecentMutation
|
144 | };
|
145 | } |
\ | No newline at end of file |