@apollo/client 3.6.2 → 3.6.5

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