@apollo/client 3.6.1 → 3.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/apollo-client.cjs +133 -78
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/cache/cache.cjs.native.js +2289 -0
- package/core/ApolloClient.d.ts.map +1 -1
- package/core/ApolloClient.js.map +1 -1
- package/core/ObservableQuery.d.ts.map +1 -1
- package/core/ObservableQuery.js +3 -1
- package/core/ObservableQuery.js.map +1 -1
- package/core/core.cjs +4 -2
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +2141 -0
- package/errors/errors.cjs.native.js +48 -0
- package/invariantErrorCodes.js +1 -1
- package/link/batch/batch.cjs.native.js +161 -0
- package/link/batch-http/batch-http.cjs.native.js +127 -0
- package/link/context/context.cjs.native.js +38 -0
- package/link/core/core.cjs.native.js +121 -0
- package/link/error/error.cjs.native.js +90 -0
- package/link/http/http.cjs.native.js +320 -0
- package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
- package/link/http/selectHttpOptionsAndBody.js.map +1 -1
- package/link/persisted-queries/index.d.ts +2 -1
- package/link/persisted-queries/index.d.ts.map +1 -1
- package/link/persisted-queries/index.js +26 -13
- package/link/persisted-queries/index.js.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs +25 -12
- package/link/persisted-queries/persisted-queries.cjs.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs.native.js +174 -0
- package/link/retry/retry.cjs.native.js +170 -0
- package/link/schema/schema.cjs.native.js +56 -0
- package/link/subscriptions/subscriptions.cjs.native.js +45 -0
- package/link/utils/utils.cjs.native.js +115 -0
- package/link/ws/ws.cjs.native.js +28 -0
- package/main.cjs.native.js +16 -0
- package/package.json +17 -18
- package/react/components/components.cjs.native.js +79 -0
- package/react/context/ApolloConsumer.js +2 -2
- package/react/context/ApolloProvider.js +2 -2
- package/react/context/context.cjs +4 -4
- package/react/context/context.cjs.map +1 -1
- package/react/context/context.cjs.native.js +67 -0
- package/react/hoc/hoc.cjs.native.js +325 -0
- package/react/hooks/hooks.cjs +151 -90
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +604 -0
- package/react/hooks/useLazyQuery.d.ts.map +1 -1
- package/react/hooks/useLazyQuery.js +7 -22
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useQuery.d.ts +5 -1
- package/react/hooks/useQuery.d.ts.map +1 -1
- package/react/hooks/useQuery.js +46 -39
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useSubscription.d.ts.map +1 -1
- package/react/hooks/useSubscription.js +17 -7
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/hooks/useSyncExternalStore.d.ts +4 -0
- package/react/hooks/useSyncExternalStore.d.ts.map +1 -0
- package/react/hooks/useSyncExternalStore.js +48 -0
- package/react/hooks/useSyncExternalStore.js.map +1 -0
- package/react/parser/parser.cjs.native.js +103 -0
- package/react/react.cjs.native.js +22 -0
- package/react/ssr/ssr.cjs.native.js +150 -0
- package/testing/core/core.cjs.native.js +288 -0
- package/testing/core/mocking/mockQueryManager.js +1 -1
- package/testing/core/mocking/mockWatchQuery.js +1 -1
- package/testing/core/wrap.js +1 -1
- package/testing/testing.cjs.native.js +58 -0
- package/utilities/common/canUse.d.ts +2 -0
- package/utilities/common/canUse.d.ts.map +1 -1
- package/utilities/common/canUse.js +6 -2
- package/utilities/common/canUse.js.map +1 -1
- package/utilities/common/mergeOptions.d.ts +1 -1
- package/utilities/common/mergeOptions.d.ts.map +1 -1
- package/utilities/common/mergeOptions.js +1 -1
- package/utilities/common/mergeOptions.js.map +1 -1
- package/utilities/globals/global.js +1 -2
- package/utilities/globals/globals.cjs.native.js +56 -0
- package/utilities/observables/Concast.d.ts.map +1 -1
- package/utilities/observables/Concast.js +5 -2
- package/utilities/observables/Concast.js.map +1 -1
- package/utilities/policies/pagination.d.ts.map +1 -1
- package/utilities/policies/pagination.js +9 -7
- package/utilities/policies/pagination.js.map +1 -1
- package/utilities/utilities.cjs +22 -12
- package/utilities/utilities.cjs.map +1 -1
- package/utilities/utilities.cjs.native.js +1284 -0
- package/version.js +1 -1
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var globals = require('../../utilities/globals');
|
|
6
|
+
var React = require('react');
|
|
7
|
+
var context = require('../context');
|
|
8
|
+
var tslib = require('tslib');
|
|
9
|
+
var utilities = require('../../utilities');
|
|
10
|
+
var equality = require('@wry/equality');
|
|
11
|
+
var core = require('../../core');
|
|
12
|
+
var errors = require('../../errors');
|
|
13
|
+
var parser = require('../parser');
|
|
14
|
+
|
|
15
|
+
function _interopNamespace(e) {
|
|
16
|
+
if (e && e.__esModule) return e;
|
|
17
|
+
var n = Object.create(null);
|
|
18
|
+
if (e) {
|
|
19
|
+
for (var k in e) {
|
|
20
|
+
n[k] = e[k];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
n["default"] = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
28
|
+
|
|
29
|
+
function useApolloClient(override) {
|
|
30
|
+
var context$1 = React.useContext(context.getApolloContext());
|
|
31
|
+
var client = override || context$1.client;
|
|
32
|
+
__DEV__ ? globals.invariant(!!client, 'Could not find "client" in the context or passed in as an option. ' +
|
|
33
|
+
'Wrap the root component in an <ApolloProvider>, or pass an ApolloClient ' +
|
|
34
|
+
'instance in via options.') : globals.invariant(!!client, 29);
|
|
35
|
+
return client;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var didWarnUncachedGetSnapshot = false;
|
|
39
|
+
var uSESKey = "useSyncExternalStore";
|
|
40
|
+
var realHook = React__namespace[uSESKey];
|
|
41
|
+
var useSyncExternalStore = realHook || (function (subscribe, getSnapshot, getServerSnapshot) {
|
|
42
|
+
var value = getSnapshot();
|
|
43
|
+
if (__DEV__ &&
|
|
44
|
+
!didWarnUncachedGetSnapshot &&
|
|
45
|
+
value !== getSnapshot()) {
|
|
46
|
+
didWarnUncachedGetSnapshot = true;
|
|
47
|
+
__DEV__ && globals.invariant.error('The result of getSnapshot should be cached to avoid an infinite loop');
|
|
48
|
+
}
|
|
49
|
+
var _a = React__namespace.useState({ inst: { value: value, getSnapshot: getSnapshot } }), inst = _a[0].inst, forceUpdate = _a[1];
|
|
50
|
+
if (utilities.canUseLayoutEffect) {
|
|
51
|
+
React__namespace.useLayoutEffect(function () {
|
|
52
|
+
Object.assign(inst, { value: value, getSnapshot: getSnapshot });
|
|
53
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
54
|
+
forceUpdate({ inst: inst });
|
|
55
|
+
}
|
|
56
|
+
}, [subscribe, value, getSnapshot]);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
Object.assign(inst, { value: value, getSnapshot: getSnapshot });
|
|
60
|
+
}
|
|
61
|
+
React__namespace.useEffect(function () {
|
|
62
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
63
|
+
forceUpdate({ inst: inst });
|
|
64
|
+
}
|
|
65
|
+
return subscribe(function handleStoreChange() {
|
|
66
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
67
|
+
forceUpdate({ inst: inst });
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}, [subscribe]);
|
|
71
|
+
return value;
|
|
72
|
+
});
|
|
73
|
+
function checkIfSnapshotChanged(_a) {
|
|
74
|
+
var value = _a.value, getSnapshot = _a.getSnapshot;
|
|
75
|
+
try {
|
|
76
|
+
return value !== getSnapshot();
|
|
77
|
+
}
|
|
78
|
+
catch (_b) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
84
|
+
function useQuery(query, options) {
|
|
85
|
+
if (options === void 0) { options = Object.create(null); }
|
|
86
|
+
return useInternalState(useApolloClient(options.client), query).useQuery(options);
|
|
87
|
+
}
|
|
88
|
+
function useInternalState(client, query) {
|
|
89
|
+
var stateRef = React.useRef();
|
|
90
|
+
if (!stateRef.current ||
|
|
91
|
+
client !== stateRef.current.client ||
|
|
92
|
+
query !== stateRef.current.query) {
|
|
93
|
+
stateRef.current = new InternalState(client, query);
|
|
94
|
+
}
|
|
95
|
+
var state = stateRef.current;
|
|
96
|
+
var _a = React.useState(0); _a[0]; var setTick = _a[1];
|
|
97
|
+
state.forceUpdate = function () {
|
|
98
|
+
setTick(function (tick) { return tick + 1; });
|
|
99
|
+
};
|
|
100
|
+
return state;
|
|
101
|
+
}
|
|
102
|
+
var InternalState = (function () {
|
|
103
|
+
function InternalState(client, query) {
|
|
104
|
+
this.client = client;
|
|
105
|
+
this.query = query;
|
|
106
|
+
this.asyncResolveFns = new Set();
|
|
107
|
+
this.optionsToIgnoreOnce = new (utilities.canUseWeakSet ? WeakSet : Set)();
|
|
108
|
+
this.ssrDisabledResult = utilities.maybeDeepFreeze({
|
|
109
|
+
loading: true,
|
|
110
|
+
data: void 0,
|
|
111
|
+
error: void 0,
|
|
112
|
+
networkStatus: core.NetworkStatus.loading,
|
|
113
|
+
});
|
|
114
|
+
this.skipStandbyResult = utilities.maybeDeepFreeze({
|
|
115
|
+
loading: false,
|
|
116
|
+
data: void 0,
|
|
117
|
+
error: void 0,
|
|
118
|
+
networkStatus: core.NetworkStatus.ready,
|
|
119
|
+
});
|
|
120
|
+
this.toQueryResultCache = new (utilities.canUseWeakMap ? WeakMap : Map)();
|
|
121
|
+
parser.verifyDocumentType(query, parser.DocumentType.Query);
|
|
122
|
+
}
|
|
123
|
+
InternalState.prototype.forceUpdate = function () {
|
|
124
|
+
__DEV__ && globals.invariant.warn("Calling default no-op implementation of InternalState#forceUpdate");
|
|
125
|
+
};
|
|
126
|
+
InternalState.prototype.asyncUpdate = function () {
|
|
127
|
+
var _this = this;
|
|
128
|
+
return new Promise(function (resolve) {
|
|
129
|
+
_this.asyncResolveFns.add(resolve);
|
|
130
|
+
_this.optionsToIgnoreOnce.add(_this.watchQueryOptions);
|
|
131
|
+
_this.forceUpdate();
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
InternalState.prototype.useQuery = function (options) {
|
|
135
|
+
var _this = this;
|
|
136
|
+
this.renderPromises = React.useContext(context.getApolloContext()).renderPromises;
|
|
137
|
+
this.useOptions(options);
|
|
138
|
+
var obsQuery = this.useObservableQuery();
|
|
139
|
+
var result = useSyncExternalStore(React.useCallback(function () {
|
|
140
|
+
if (_this.renderPromises) {
|
|
141
|
+
return function () { };
|
|
142
|
+
}
|
|
143
|
+
var onNext = function () {
|
|
144
|
+
var previousResult = _this.result;
|
|
145
|
+
var result = obsQuery.getCurrentResult();
|
|
146
|
+
if (previousResult &&
|
|
147
|
+
previousResult.loading === result.loading &&
|
|
148
|
+
previousResult.networkStatus === result.networkStatus &&
|
|
149
|
+
equality.equal(previousResult.data, result.data)) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
_this.setResult(result);
|
|
153
|
+
};
|
|
154
|
+
var onError = function (error) {
|
|
155
|
+
var last = obsQuery["last"];
|
|
156
|
+
subscription.unsubscribe();
|
|
157
|
+
try {
|
|
158
|
+
obsQuery.resetLastResults();
|
|
159
|
+
subscription = obsQuery.subscribe(onNext, onError);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
obsQuery["last"] = last;
|
|
163
|
+
}
|
|
164
|
+
if (!hasOwnProperty.call(error, 'graphQLErrors')) {
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
var previousResult = _this.result;
|
|
168
|
+
if (!previousResult ||
|
|
169
|
+
(previousResult && previousResult.loading) ||
|
|
170
|
+
!equality.equal(error, previousResult.error)) {
|
|
171
|
+
_this.setResult({
|
|
172
|
+
data: (previousResult && previousResult.data),
|
|
173
|
+
error: error,
|
|
174
|
+
loading: false,
|
|
175
|
+
networkStatus: core.NetworkStatus.error,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
var subscription = obsQuery.subscribe(onNext, onError);
|
|
180
|
+
return function () { return subscription.unsubscribe(); };
|
|
181
|
+
}, [
|
|
182
|
+
obsQuery,
|
|
183
|
+
this.renderPromises,
|
|
184
|
+
this.client.disableNetworkFetches,
|
|
185
|
+
]), function () { return _this.getCurrentResult(); }, function () { return _this.getCurrentResult(); });
|
|
186
|
+
this.unsafeHandlePartialRefetch(result);
|
|
187
|
+
var queryResult = this.toQueryResult(result);
|
|
188
|
+
if (!queryResult.loading && this.asyncResolveFns.size) {
|
|
189
|
+
this.asyncResolveFns.forEach(function (resolve) { return resolve(queryResult); });
|
|
190
|
+
this.asyncResolveFns.clear();
|
|
191
|
+
}
|
|
192
|
+
return queryResult;
|
|
193
|
+
};
|
|
194
|
+
InternalState.prototype.useOptions = function (options) {
|
|
195
|
+
var _a;
|
|
196
|
+
var watchQueryOptions = this.createWatchQueryOptions(this.queryHookOptions = options);
|
|
197
|
+
var currentWatchQueryOptions = this.watchQueryOptions;
|
|
198
|
+
if (this.optionsToIgnoreOnce.has(currentWatchQueryOptions) ||
|
|
199
|
+
!equality.equal(watchQueryOptions, currentWatchQueryOptions)) {
|
|
200
|
+
this.watchQueryOptions = watchQueryOptions;
|
|
201
|
+
if (currentWatchQueryOptions && this.observable) {
|
|
202
|
+
this.optionsToIgnoreOnce.delete(currentWatchQueryOptions);
|
|
203
|
+
this.observable.reobserve(watchQueryOptions);
|
|
204
|
+
this.previousData = ((_a = this.result) === null || _a === void 0 ? void 0 : _a.data) || this.previousData;
|
|
205
|
+
this.result = void 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
this.onCompleted = options.onCompleted || InternalState.prototype.onCompleted;
|
|
209
|
+
this.onError = options.onError || InternalState.prototype.onError;
|
|
210
|
+
if ((this.renderPromises || this.client.disableNetworkFetches) &&
|
|
211
|
+
this.queryHookOptions.ssr === false &&
|
|
212
|
+
!this.queryHookOptions.skip) {
|
|
213
|
+
this.result = this.ssrDisabledResult;
|
|
214
|
+
}
|
|
215
|
+
else if (this.queryHookOptions.skip ||
|
|
216
|
+
this.watchQueryOptions.fetchPolicy === 'standby') {
|
|
217
|
+
this.result = this.skipStandbyResult;
|
|
218
|
+
}
|
|
219
|
+
else if (this.result === this.ssrDisabledResult ||
|
|
220
|
+
this.result === this.skipStandbyResult) {
|
|
221
|
+
this.result = void 0;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
InternalState.prototype.createWatchQueryOptions = function (_a) {
|
|
225
|
+
var _b;
|
|
226
|
+
if (_a === void 0) { _a = {}; }
|
|
227
|
+
var skip = _a.skip; _a.ssr; _a.onCompleted; _a.onError; _a.displayName; _a.defaultOptions; var otherOptions = tslib.__rest(_a, ["skip", "ssr", "onCompleted", "onError", "displayName", "defaultOptions"]);
|
|
228
|
+
var watchQueryOptions = Object.assign(otherOptions, { query: this.query });
|
|
229
|
+
if (this.renderPromises &&
|
|
230
|
+
(watchQueryOptions.fetchPolicy === 'network-only' ||
|
|
231
|
+
watchQueryOptions.fetchPolicy === 'cache-and-network')) {
|
|
232
|
+
watchQueryOptions.fetchPolicy = 'cache-first';
|
|
233
|
+
}
|
|
234
|
+
if (!watchQueryOptions.variables) {
|
|
235
|
+
watchQueryOptions.variables = {};
|
|
236
|
+
}
|
|
237
|
+
if (skip) {
|
|
238
|
+
var _c = watchQueryOptions.fetchPolicy, fetchPolicy = _c === void 0 ? this.getDefaultFetchPolicy() : _c, _d = watchQueryOptions.initialFetchPolicy, initialFetchPolicy = _d === void 0 ? fetchPolicy : _d;
|
|
239
|
+
Object.assign(watchQueryOptions, {
|
|
240
|
+
initialFetchPolicy: initialFetchPolicy,
|
|
241
|
+
fetchPolicy: 'standby',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
else if (!watchQueryOptions.fetchPolicy) {
|
|
245
|
+
watchQueryOptions.fetchPolicy =
|
|
246
|
+
((_b = this.observable) === null || _b === void 0 ? void 0 : _b.options.initialFetchPolicy) ||
|
|
247
|
+
this.getDefaultFetchPolicy();
|
|
248
|
+
}
|
|
249
|
+
return watchQueryOptions;
|
|
250
|
+
};
|
|
251
|
+
InternalState.prototype.getDefaultFetchPolicy = function () {
|
|
252
|
+
var _a, _b;
|
|
253
|
+
return (((_a = this.queryHookOptions.defaultOptions) === null || _a === void 0 ? void 0 : _a.fetchPolicy) ||
|
|
254
|
+
((_b = this.client.defaultOptions.watchQuery) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
|
|
255
|
+
"cache-first");
|
|
256
|
+
};
|
|
257
|
+
InternalState.prototype.onCompleted = function (data) { };
|
|
258
|
+
InternalState.prototype.onError = function (error) { };
|
|
259
|
+
InternalState.prototype.useObservableQuery = function () {
|
|
260
|
+
var obsQuery = this.observable =
|
|
261
|
+
this.renderPromises
|
|
262
|
+
&& this.renderPromises.getSSRObservable(this.watchQueryOptions)
|
|
263
|
+
|| this.observable
|
|
264
|
+
|| this.client.watchQuery(core.mergeOptions(this.queryHookOptions.defaultOptions, this.watchQueryOptions));
|
|
265
|
+
this.obsQueryFields = React.useMemo(function () { return ({
|
|
266
|
+
refetch: obsQuery.refetch.bind(obsQuery),
|
|
267
|
+
reobserve: obsQuery.reobserve.bind(obsQuery),
|
|
268
|
+
fetchMore: obsQuery.fetchMore.bind(obsQuery),
|
|
269
|
+
updateQuery: obsQuery.updateQuery.bind(obsQuery),
|
|
270
|
+
startPolling: obsQuery.startPolling.bind(obsQuery),
|
|
271
|
+
stopPolling: obsQuery.stopPolling.bind(obsQuery),
|
|
272
|
+
subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
|
|
273
|
+
}); }, [obsQuery]);
|
|
274
|
+
var ssrAllowed = !(this.queryHookOptions.ssr === false ||
|
|
275
|
+
this.queryHookOptions.skip);
|
|
276
|
+
if (this.renderPromises && ssrAllowed) {
|
|
277
|
+
this.renderPromises.registerSSRObservable(obsQuery);
|
|
278
|
+
if (obsQuery.getCurrentResult().loading) {
|
|
279
|
+
this.renderPromises.addObservableQueryPromise(obsQuery);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return obsQuery;
|
|
283
|
+
};
|
|
284
|
+
InternalState.prototype.setResult = function (nextResult) {
|
|
285
|
+
var previousResult = this.result;
|
|
286
|
+
if (previousResult && previousResult.data) {
|
|
287
|
+
this.previousData = previousResult.data;
|
|
288
|
+
}
|
|
289
|
+
this.result = nextResult;
|
|
290
|
+
this.forceUpdate();
|
|
291
|
+
this.handleErrorOrCompleted(nextResult);
|
|
292
|
+
};
|
|
293
|
+
InternalState.prototype.handleErrorOrCompleted = function (result) {
|
|
294
|
+
if (!result.loading) {
|
|
295
|
+
if (result.error) {
|
|
296
|
+
this.onError(result.error);
|
|
297
|
+
}
|
|
298
|
+
else if (result.data) {
|
|
299
|
+
this.onCompleted(result.data);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
InternalState.prototype.getCurrentResult = function () {
|
|
304
|
+
if (!this.result) {
|
|
305
|
+
this.handleErrorOrCompleted(this.result = this.observable.getCurrentResult());
|
|
306
|
+
}
|
|
307
|
+
return this.result;
|
|
308
|
+
};
|
|
309
|
+
InternalState.prototype.toQueryResult = function (result) {
|
|
310
|
+
var queryResult = this.toQueryResultCache.get(result);
|
|
311
|
+
if (queryResult)
|
|
312
|
+
return queryResult;
|
|
313
|
+
var data = result.data; result.partial; var resultWithoutPartial = tslib.__rest(result, ["data", "partial"]);
|
|
314
|
+
this.toQueryResultCache.set(result, queryResult = tslib.__assign(tslib.__assign(tslib.__assign({ data: data }, resultWithoutPartial), this.obsQueryFields), { client: this.client, observable: this.observable, variables: this.observable.variables, called: true, previousData: this.previousData }));
|
|
315
|
+
if (!queryResult.error && utilities.isNonEmptyArray(result.errors)) {
|
|
316
|
+
queryResult.error = new errors.ApolloError({ graphQLErrors: result.errors });
|
|
317
|
+
}
|
|
318
|
+
return queryResult;
|
|
319
|
+
};
|
|
320
|
+
InternalState.prototype.unsafeHandlePartialRefetch = function (result) {
|
|
321
|
+
if (result.partial &&
|
|
322
|
+
this.queryHookOptions.partialRefetch &&
|
|
323
|
+
!result.loading &&
|
|
324
|
+
(!result.data || Object.keys(result.data).length === 0) &&
|
|
325
|
+
this.observable.options.fetchPolicy !== 'cache-only') {
|
|
326
|
+
Object.assign(result, {
|
|
327
|
+
loading: true,
|
|
328
|
+
networkStatus: core.NetworkStatus.refetch,
|
|
329
|
+
});
|
|
330
|
+
this.observable.refetch();
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
return InternalState;
|
|
334
|
+
}());
|
|
335
|
+
|
|
336
|
+
var EAGER_METHODS = [
|
|
337
|
+
'refetch',
|
|
338
|
+
'reobserve',
|
|
339
|
+
'fetchMore',
|
|
340
|
+
'updateQuery',
|
|
341
|
+
'startPolling',
|
|
342
|
+
'subscribeToMore',
|
|
343
|
+
];
|
|
344
|
+
function useLazyQuery(query, options) {
|
|
345
|
+
var internalState = useInternalState(useApolloClient(options && options.client), query);
|
|
346
|
+
var execOptionsRef = React.useRef();
|
|
347
|
+
var useQueryResult = internalState.useQuery(tslib.__assign(tslib.__assign(tslib.__assign({}, options), execOptionsRef.current), { skip: !execOptionsRef.current }));
|
|
348
|
+
var initialFetchPolicy = useQueryResult.observable.options.initialFetchPolicy ||
|
|
349
|
+
internalState.getDefaultFetchPolicy();
|
|
350
|
+
var result = Object.assign(useQueryResult, {
|
|
351
|
+
called: !!execOptionsRef.current,
|
|
352
|
+
});
|
|
353
|
+
var eagerMethods = React.useMemo(function () {
|
|
354
|
+
var eagerMethods = {};
|
|
355
|
+
var _loop_1 = function (key) {
|
|
356
|
+
var method = result[key];
|
|
357
|
+
eagerMethods[key] = function () {
|
|
358
|
+
if (!execOptionsRef.current) {
|
|
359
|
+
execOptionsRef.current = Object.create(null);
|
|
360
|
+
internalState.forceUpdate();
|
|
361
|
+
}
|
|
362
|
+
return method.apply(this, arguments);
|
|
363
|
+
};
|
|
364
|
+
};
|
|
365
|
+
for (var _i = 0, EAGER_METHODS_1 = EAGER_METHODS; _i < EAGER_METHODS_1.length; _i++) {
|
|
366
|
+
var key = EAGER_METHODS_1[_i];
|
|
367
|
+
_loop_1(key);
|
|
368
|
+
}
|
|
369
|
+
return eagerMethods;
|
|
370
|
+
}, []);
|
|
371
|
+
Object.assign(result, eagerMethods);
|
|
372
|
+
var execute = React.useCallback(function (executeOptions) {
|
|
373
|
+
execOptionsRef.current = executeOptions ? tslib.__assign(tslib.__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
|
374
|
+
fetchPolicy: initialFetchPolicy,
|
|
375
|
+
};
|
|
376
|
+
var promise = internalState
|
|
377
|
+
.asyncUpdate()
|
|
378
|
+
.then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
|
379
|
+
promise.catch(function () { });
|
|
380
|
+
return promise;
|
|
381
|
+
}, []);
|
|
382
|
+
return [execute, result];
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function useMutation(mutation, options) {
|
|
386
|
+
var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
|
387
|
+
parser.verifyDocumentType(mutation, parser.DocumentType.Mutation);
|
|
388
|
+
var _a = React.useState({
|
|
389
|
+
called: false,
|
|
390
|
+
loading: false,
|
|
391
|
+
client: client,
|
|
392
|
+
}), result = _a[0], setResult = _a[1];
|
|
393
|
+
var ref = React.useRef({
|
|
394
|
+
result: result,
|
|
395
|
+
mutationId: 0,
|
|
396
|
+
isMounted: true,
|
|
397
|
+
client: client,
|
|
398
|
+
mutation: mutation,
|
|
399
|
+
options: options,
|
|
400
|
+
});
|
|
401
|
+
{
|
|
402
|
+
Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
|
403
|
+
}
|
|
404
|
+
var execute = React.useCallback(function (executeOptions) {
|
|
405
|
+
if (executeOptions === void 0) { executeOptions = {}; }
|
|
406
|
+
var _a = ref.current, client = _a.client, options = _a.options, mutation = _a.mutation;
|
|
407
|
+
var baseOptions = tslib.__assign(tslib.__assign({}, options), { mutation: mutation });
|
|
408
|
+
if (!ref.current.result.loading && !baseOptions.ignoreResults) {
|
|
409
|
+
setResult(ref.current.result = {
|
|
410
|
+
loading: true,
|
|
411
|
+
error: void 0,
|
|
412
|
+
data: void 0,
|
|
413
|
+
called: true,
|
|
414
|
+
client: client,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
var mutationId = ++ref.current.mutationId;
|
|
418
|
+
var clientOptions = core.mergeOptions(baseOptions, executeOptions);
|
|
419
|
+
return client.mutate(clientOptions).then(function (response) {
|
|
420
|
+
var _a, _b;
|
|
421
|
+
var data = response.data, errors$1 = response.errors;
|
|
422
|
+
var error = errors$1 && errors$1.length > 0
|
|
423
|
+
? new errors.ApolloError({ graphQLErrors: errors$1 })
|
|
424
|
+
: void 0;
|
|
425
|
+
if (mutationId === ref.current.mutationId &&
|
|
426
|
+
!clientOptions.ignoreResults) {
|
|
427
|
+
var result_1 = {
|
|
428
|
+
called: true,
|
|
429
|
+
loading: false,
|
|
430
|
+
data: data,
|
|
431
|
+
error: error,
|
|
432
|
+
client: client,
|
|
433
|
+
};
|
|
434
|
+
if (ref.current.isMounted && !equality.equal(ref.current.result, result_1)) {
|
|
435
|
+
setResult(ref.current.result = result_1);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
(_a = baseOptions.onCompleted) === null || _a === void 0 ? void 0 : _a.call(baseOptions, response.data);
|
|
439
|
+
(_b = executeOptions.onCompleted) === null || _b === void 0 ? void 0 : _b.call(executeOptions, response.data);
|
|
440
|
+
return response;
|
|
441
|
+
}).catch(function (error) {
|
|
442
|
+
var _a, _b;
|
|
443
|
+
if (mutationId === ref.current.mutationId &&
|
|
444
|
+
ref.current.isMounted) {
|
|
445
|
+
var result_2 = {
|
|
446
|
+
loading: false,
|
|
447
|
+
error: error,
|
|
448
|
+
data: void 0,
|
|
449
|
+
called: true,
|
|
450
|
+
client: client,
|
|
451
|
+
};
|
|
452
|
+
if (!equality.equal(ref.current.result, result_2)) {
|
|
453
|
+
setResult(ref.current.result = result_2);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (baseOptions.onError || clientOptions.onError) {
|
|
457
|
+
(_a = baseOptions.onError) === null || _a === void 0 ? void 0 : _a.call(baseOptions, error);
|
|
458
|
+
(_b = executeOptions.onError) === null || _b === void 0 ? void 0 : _b.call(executeOptions, error);
|
|
459
|
+
return { data: void 0, errors: error };
|
|
460
|
+
}
|
|
461
|
+
throw error;
|
|
462
|
+
});
|
|
463
|
+
}, []);
|
|
464
|
+
var reset = React.useCallback(function () {
|
|
465
|
+
setResult({ called: false, loading: false, client: client });
|
|
466
|
+
}, []);
|
|
467
|
+
React.useEffect(function () {
|
|
468
|
+
ref.current.isMounted = true;
|
|
469
|
+
return function () {
|
|
470
|
+
ref.current.isMounted = false;
|
|
471
|
+
};
|
|
472
|
+
}, []);
|
|
473
|
+
return [execute, tslib.__assign({ reset: reset }, result)];
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function useSubscription(subscription, options) {
|
|
477
|
+
var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
|
478
|
+
parser.verifyDocumentType(subscription, parser.DocumentType.Subscription);
|
|
479
|
+
var _a = React.useState({
|
|
480
|
+
loading: !(options === null || options === void 0 ? void 0 : options.skip),
|
|
481
|
+
error: void 0,
|
|
482
|
+
data: void 0,
|
|
483
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
484
|
+
}), result = _a[0], setResult = _a[1];
|
|
485
|
+
var _b = React.useState(function () {
|
|
486
|
+
if (options === null || options === void 0 ? void 0 : options.skip) {
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
return client.subscribe({
|
|
490
|
+
query: subscription,
|
|
491
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
492
|
+
fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
|
493
|
+
context: options === null || options === void 0 ? void 0 : options.context,
|
|
494
|
+
});
|
|
495
|
+
}), observable = _b[0], setObservable = _b[1];
|
|
496
|
+
var canResetObservableRef = React.useRef(false);
|
|
497
|
+
React.useEffect(function () {
|
|
498
|
+
return function () {
|
|
499
|
+
canResetObservableRef.current = true;
|
|
500
|
+
};
|
|
501
|
+
}, []);
|
|
502
|
+
var ref = React.useRef({ client: client, subscription: subscription, options: options });
|
|
503
|
+
React.useEffect(function () {
|
|
504
|
+
var _a, _b, _c, _d;
|
|
505
|
+
var shouldResubscribe = options === null || options === void 0 ? void 0 : options.shouldResubscribe;
|
|
506
|
+
if (typeof shouldResubscribe === 'function') {
|
|
507
|
+
shouldResubscribe = !!shouldResubscribe(options);
|
|
508
|
+
}
|
|
509
|
+
if (options === null || options === void 0 ? void 0 : options.skip) {
|
|
510
|
+
if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) || canResetObservableRef.current) {
|
|
511
|
+
setResult({
|
|
512
|
+
loading: false,
|
|
513
|
+
data: void 0,
|
|
514
|
+
error: void 0,
|
|
515
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
516
|
+
});
|
|
517
|
+
setObservable(null);
|
|
518
|
+
canResetObservableRef.current = false;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
else if ((shouldResubscribe !== false &&
|
|
522
|
+
(client !== ref.current.client ||
|
|
523
|
+
subscription !== ref.current.subscription ||
|
|
524
|
+
(options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
|
|
525
|
+
!(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
|
|
526
|
+
!equality.equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) ||
|
|
527
|
+
canResetObservableRef.current) {
|
|
528
|
+
setResult({
|
|
529
|
+
loading: true,
|
|
530
|
+
data: void 0,
|
|
531
|
+
error: void 0,
|
|
532
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
533
|
+
});
|
|
534
|
+
setObservable(client.subscribe({
|
|
535
|
+
query: subscription,
|
|
536
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
537
|
+
fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
|
|
538
|
+
context: options === null || options === void 0 ? void 0 : options.context,
|
|
539
|
+
}));
|
|
540
|
+
canResetObservableRef.current = false;
|
|
541
|
+
}
|
|
542
|
+
Object.assign(ref.current, { client: client, subscription: subscription, options: options });
|
|
543
|
+
}, [client, subscription, options, canResetObservableRef.current]);
|
|
544
|
+
React.useEffect(function () {
|
|
545
|
+
if (!observable) {
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
var subscription = observable.subscribe({
|
|
549
|
+
next: function (fetchResult) {
|
|
550
|
+
var _a, _b;
|
|
551
|
+
var result = {
|
|
552
|
+
loading: false,
|
|
553
|
+
data: fetchResult.data,
|
|
554
|
+
error: void 0,
|
|
555
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
556
|
+
};
|
|
557
|
+
setResult(result);
|
|
558
|
+
(_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onSubscriptionData) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
559
|
+
client: client,
|
|
560
|
+
subscriptionData: result
|
|
561
|
+
});
|
|
562
|
+
},
|
|
563
|
+
error: function (error) {
|
|
564
|
+
setResult({
|
|
565
|
+
loading: false,
|
|
566
|
+
data: void 0,
|
|
567
|
+
error: error,
|
|
568
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
569
|
+
});
|
|
570
|
+
},
|
|
571
|
+
complete: function () {
|
|
572
|
+
var _a, _b;
|
|
573
|
+
(_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onSubscriptionComplete) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
return function () {
|
|
577
|
+
subscription.unsubscribe();
|
|
578
|
+
};
|
|
579
|
+
}, [observable]);
|
|
580
|
+
return result;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function useReactiveVar(rv) {
|
|
584
|
+
var value = rv();
|
|
585
|
+
var setValue = React.useState(value)[1];
|
|
586
|
+
React.useEffect(function () {
|
|
587
|
+
var probablySameValue = rv();
|
|
588
|
+
if (value !== probablySameValue) {
|
|
589
|
+
setValue(probablySameValue);
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
return rv.onNextChange(setValue);
|
|
593
|
+
}
|
|
594
|
+
}, [value]);
|
|
595
|
+
return value;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
exports.useApolloClient = useApolloClient;
|
|
599
|
+
exports.useLazyQuery = useLazyQuery;
|
|
600
|
+
exports.useMutation = useMutation;
|
|
601
|
+
exports.useQuery = useQuery;
|
|
602
|
+
exports.useReactiveVar = useReactiveVar;
|
|
603
|
+
exports.useSubscription = useSubscription;
|
|
604
|
+
//# sourceMappingURL=hooks.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLazyQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAGtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"useLazyQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAGtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EAErB,MAAM,gBAAgB,CAAC;AAexB,wBAAgB,YAAY,CAAC,KAAK,GAAG,GAAG,EAAE,UAAU,GAAG,kBAAkB,EACvE,KAAK,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC1D,OAAO,CAAC,EAAE,oBAAoB,CAAC,KAAK,EAAE,UAAU,CAAC,GAChD,oBAAoB,CAAC,KAAK,EAAE,UAAU,CAAC,CAiEzC"}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
2
|
import { useCallback, useMemo, useRef } from 'react';
|
|
3
|
-
import { ApolloError } from "../../errors/index.js";
|
|
4
3
|
import { useInternalState } from "./useQuery.js";
|
|
5
4
|
import { useApolloClient } from "./useApolloClient.js";
|
|
6
|
-
import { isNonEmptyArray } from "../../utilities/index.js";
|
|
7
5
|
var EAGER_METHODS = [
|
|
8
6
|
'refetch',
|
|
9
7
|
'reobserve',
|
|
@@ -15,12 +13,9 @@ var EAGER_METHODS = [
|
|
|
15
13
|
export function useLazyQuery(query, options) {
|
|
16
14
|
var internalState = useInternalState(useApolloClient(options && options.client), query);
|
|
17
15
|
var execOptionsRef = useRef();
|
|
18
|
-
var defaultOptions = internalState.client.defaultOptions.watchQuery;
|
|
19
|
-
var initialFetchPolicy = (options && options.fetchPolicy) ||
|
|
20
|
-
(execOptionsRef.current && execOptionsRef.current.fetchPolicy) ||
|
|
21
|
-
(defaultOptions && defaultOptions.fetchPolicy) ||
|
|
22
|
-
"cache-first";
|
|
23
16
|
var useQueryResult = internalState.useQuery(__assign(__assign(__assign({}, options), execOptionsRef.current), { skip: !execOptionsRef.current }));
|
|
17
|
+
var initialFetchPolicy = useQueryResult.observable.options.initialFetchPolicy ||
|
|
18
|
+
internalState.getDefaultFetchPolicy();
|
|
24
19
|
var result = Object.assign(useQueryResult, {
|
|
25
20
|
called: !!execOptionsRef.current,
|
|
26
21
|
});
|
|
@@ -44,22 +39,12 @@ export function useLazyQuery(query, options) {
|
|
|
44
39
|
}, []);
|
|
45
40
|
Object.assign(result, eagerMethods);
|
|
46
41
|
var execute = useCallback(function (executeOptions) {
|
|
47
|
-
|
|
42
|
+
execOptionsRef.current = executeOptions ? __assign(__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
|
48
43
|
fetchPolicy: initialFetchPolicy,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var _a = result.observable.options.errorPolicy, errorPolicy = _a === void 0 ? "none" : _a;
|
|
54
|
-
if (errorPolicy === "none") {
|
|
55
|
-
throw apolloQueryResult.error || new ApolloError({
|
|
56
|
-
graphQLErrors: apolloQueryResult.errors,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return internalState.toQueryResult(apolloQueryResult);
|
|
61
|
-
}).then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
|
62
|
-
internalState.forceUpdate();
|
|
44
|
+
};
|
|
45
|
+
var promise = internalState
|
|
46
|
+
.asyncUpdate()
|
|
47
|
+
.then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
|
63
48
|
promise.catch(function () { });
|
|
64
49
|
return promise;
|
|
65
50
|
}, []);
|