@apollo/client 3.4.4 → 3.4.8

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 (66) hide show
  1. package/apollo-client.cjs.js +971 -76
  2. package/apollo-client.cjs.js.map +1 -1
  3. package/apollo-core.cjs.js +5768 -0
  4. package/apollo-core.cjs.js.map +1 -0
  5. package/apollo-core.cjs.min.js +1 -0
  6. package/cache/cache.cjs.js +12 -9
  7. package/cache/cache.cjs.js.map +1 -1
  8. package/cache/inmemory/policies.js +9 -6
  9. package/cache/inmemory/policies.js.map +1 -1
  10. package/cache/inmemory/writeToStore.d.ts +1 -1
  11. package/cache/inmemory/writeToStore.d.ts.map +1 -1
  12. package/cache/inmemory/writeToStore.js +3 -3
  13. package/cache/inmemory/writeToStore.js.map +1 -1
  14. package/core/ObservableQuery.d.ts +9 -6
  15. package/core/ObservableQuery.d.ts.map +1 -1
  16. package/core/ObservableQuery.js +70 -45
  17. package/core/ObservableQuery.js.map +1 -1
  18. package/core/QueryInfo.d.ts +5 -3
  19. package/core/QueryInfo.d.ts.map +1 -1
  20. package/core/QueryInfo.js +4 -2
  21. package/core/QueryInfo.js.map +1 -1
  22. package/core/QueryManager.d.ts +3 -3
  23. package/core/QueryManager.d.ts.map +1 -1
  24. package/core/QueryManager.js +15 -13
  25. package/core/QueryManager.js.map +1 -1
  26. package/core/core.cjs.js +89 -59
  27. package/core/core.cjs.js.map +1 -1
  28. package/core/watchQueryOptions.d.ts +2 -1
  29. package/core/watchQueryOptions.d.ts.map +1 -1
  30. package/core/watchQueryOptions.js.map +1 -1
  31. package/invariantErrorCodes.js +4 -2
  32. package/link/http/createHttpLink.d.ts.map +1 -1
  33. package/link/http/createHttpLink.js +7 -5
  34. package/link/http/createHttpLink.js.map +1 -1
  35. package/link/http/http.cjs.js +6 -5
  36. package/link/http/http.cjs.js.map +1 -1
  37. package/package.json +8 -8
  38. package/react/data/QueryData.d.ts +2 -1
  39. package/react/data/QueryData.d.ts.map +1 -1
  40. package/react/data/QueryData.js +8 -8
  41. package/react/data/QueryData.js.map +1 -1
  42. package/react/data/data.cjs.js +8 -8
  43. package/react/data/data.cjs.js.map +1 -1
  44. package/react/hooks/hooks.cjs.js +1 -0
  45. package/react/hooks/hooks.cjs.js.map +1 -1
  46. package/react/hooks/utils/useBaseQuery.d.ts.map +1 -1
  47. package/react/hooks/utils/useBaseQuery.js +1 -0
  48. package/react/hooks/utils/useBaseQuery.js.map +1 -1
  49. package/testing/testing.cjs.js +0 -5
  50. package/testing/testing.cjs.js.map +1 -1
  51. package/utilities/index.d.ts +1 -0
  52. package/utilities/index.d.ts.map +1 -1
  53. package/utilities/index.js +1 -0
  54. package/utilities/index.js.map +1 -1
  55. package/utilities/testing/index.d.ts +0 -1
  56. package/utilities/testing/index.d.ts.map +1 -1
  57. package/utilities/testing/index.js +0 -1
  58. package/utilities/testing/index.js.map +1 -1
  59. package/utilities/utilities.cjs.js +1 -0
  60. package/utilities/utilities.cjs.js.map +1 -1
  61. package/version.js +1 -1
  62. package/apollo-client.cjs.min.js +0 -1
  63. package/utilities/testing/stripSymbols.d.ts +0 -2
  64. package/utilities/testing/stripSymbols.d.ts.map +0 -1
  65. package/utilities/testing/stripSymbols.js +0 -4
  66. package/utilities/testing/stripSymbols.js.map +0 -1
package/core/core.cjs.js CHANGED
@@ -14,7 +14,7 @@ var graphql = require('graphql');
14
14
  var utils = require('../link/utils');
15
15
  var graphqlTag = require('graphql-tag');
16
16
 
17
- var version = '3.4.4';
17
+ var version = '3.4.8';
18
18
 
19
19
  exports.NetworkStatus = void 0;
20
20
  (function (NetworkStatus) {
@@ -45,11 +45,12 @@ var ObservableQuery = (function (_super) {
45
45
  catch (_a) { }
46
46
  var first = !_this.observers.size;
47
47
  _this.observers.add(observer);
48
- if (_this.lastError) {
49
- observer.error && observer.error(_this.lastError);
48
+ var last = _this.last;
49
+ if (last && last.error) {
50
+ observer.error && observer.error(last.error);
50
51
  }
51
- else if (_this.lastResult) {
52
- observer.next && observer.next(_this.lastResult);
52
+ else if (last && last.result) {
53
+ observer.next && observer.next(last.result);
53
54
  }
54
55
  if (first) {
55
56
  _this.reobserve().catch(function () { });
@@ -62,21 +63,9 @@ var ObservableQuery = (function (_super) {
62
63
  }) || this;
63
64
  _this.observers = new Set();
64
65
  _this.subscriptions = new Set();
65
- _this.observer = {
66
- next: function (result) {
67
- if (_this.lastError || _this.isDifferentFromLastResult(result)) {
68
- _this.updateLastResult(result);
69
- utilities.iterateObserversSafely(_this.observers, 'next', result);
70
- }
71
- },
72
- error: function (error) {
73
- _this.updateLastResult(tslib.__assign(tslib.__assign({}, _this.lastResult), { error: error, errors: error.graphQLErrors, networkStatus: exports.NetworkStatus.error, loading: false }));
74
- utilities.iterateObserversSafely(_this.observers, 'error', _this.lastError = error);
75
- },
76
- };
77
66
  _this.isTornDown = false;
78
67
  _this.options = options;
79
- _this.queryId = queryManager.generateQueryId();
68
+ _this.queryId = queryInfo.queryId || queryManager.generateQueryId();
80
69
  var opDef = utilities.getOperationDefinition(options.query);
81
70
  _this.queryName = opDef && opDef.name && opDef.name.value;
82
71
  _this.initialFetchPolicy = options.fetchPolicy || "cache-first";
@@ -112,15 +101,20 @@ var ObservableQuery = (function (_super) {
112
101
  };
113
102
  ObservableQuery.prototype.getCurrentResult = function (saveAsLastResult) {
114
103
  if (saveAsLastResult === void 0) { saveAsLastResult = true; }
115
- var _a = this, lastResult = _a.lastResult, _b = _a.options.fetchPolicy, fetchPolicy = _b === void 0 ? "cache-first" : _b;
104
+ var lastResult = this.getLastResult(true);
116
105
  var networkStatus = this.queryInfo.networkStatus ||
117
106
  (lastResult && lastResult.networkStatus) ||
118
107
  exports.NetworkStatus.ready;
119
108
  var result = tslib.__assign(tslib.__assign({}, lastResult), { loading: isNetworkRequestInFlight(networkStatus), networkStatus: networkStatus });
120
109
  if (!this.queryManager.transform(this.options.query).hasForcedResolvers) {
121
110
  var diff = this.queryInfo.getDiff();
122
- result.data = (diff.complete ||
123
- this.options.returnPartialData) ? diff.result : void 0;
111
+ if (diff.complete || this.options.returnPartialData) {
112
+ result.data = diff.result;
113
+ }
114
+ if (equality.equal(result.data, {})) {
115
+ result.data = void 0;
116
+ }
117
+ var _a = this.options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a;
124
118
  if (diff.complete) {
125
119
  if (result.networkStatus === exports.NetworkStatus.loading &&
126
120
  (fetchPolicy === 'cache-first' ||
@@ -133,12 +127,13 @@ var ObservableQuery = (function (_super) {
133
127
  else if (fetchPolicy !== "no-cache") {
134
128
  result.partial = true;
135
129
  }
136
- if (!diff.complete &&
130
+ if (__DEV__ &&
131
+ !diff.complete &&
137
132
  !this.options.partialRefetch &&
138
133
  !result.loading &&
139
134
  !result.data &&
140
135
  !result.error) {
141
- result.error = new errors.ApolloError({ clientErrors: diff.missing });
136
+ logMissingFieldErrors(diff.missing);
142
137
  }
143
138
  }
144
139
  if (saveAsLastResult) {
@@ -147,18 +142,24 @@ var ObservableQuery = (function (_super) {
147
142
  return result;
148
143
  };
149
144
  ObservableQuery.prototype.isDifferentFromLastResult = function (newResult) {
150
- return !equality.equal(this.lastResultSnapshot, newResult);
145
+ return !this.last || !equality.equal(this.last.result, newResult);
146
+ };
147
+ ObservableQuery.prototype.getLast = function (key, variablesMustMatch) {
148
+ var last = this.last;
149
+ if (last &&
150
+ last[key] &&
151
+ (!variablesMustMatch || equality.equal(last.variables, this.variables))) {
152
+ return last[key];
153
+ }
151
154
  };
152
- ObservableQuery.prototype.getLastResult = function () {
153
- return this.lastResult;
155
+ ObservableQuery.prototype.getLastResult = function (variablesMustMatch) {
156
+ return this.getLast("result", variablesMustMatch);
154
157
  };
155
- ObservableQuery.prototype.getLastError = function () {
156
- return this.lastError;
158
+ ObservableQuery.prototype.getLastError = function (variablesMustMatch) {
159
+ return this.getLast("error", variablesMustMatch);
157
160
  };
158
161
  ObservableQuery.prototype.resetLastResults = function () {
159
- delete this.lastResult;
160
- delete this.lastResultSnapshot;
161
- delete this.lastError;
162
+ delete this.last;
162
163
  this.isTornDown = false;
163
164
  };
164
165
  ObservableQuery.prototype.resetQueryStoreErrors = function () {
@@ -271,12 +272,10 @@ var ObservableQuery = (function (_super) {
271
272
  }, exports.NetworkStatus.setVariables);
272
273
  };
273
274
  ObservableQuery.prototype.updateQuery = function (mapFn) {
274
- var _a;
275
275
  var queryManager = this.queryManager;
276
276
  var result = queryManager.cache.diff({
277
277
  query: this.options.query,
278
278
  variables: this.variables,
279
- previousResult: (_a = this.lastResult) === null || _a === void 0 ? void 0 : _a.data,
280
279
  returnPartialData: true,
281
280
  optimistic: false,
282
281
  }).result;
@@ -345,18 +344,18 @@ var ObservableQuery = (function (_super) {
345
344
  };
346
345
  poll();
347
346
  };
348
- ObservableQuery.prototype.updateLastResult = function (newResult) {
349
- var previousResult = this.lastResult;
350
- this.lastResult = newResult;
351
- this.lastResultSnapshot = this.queryManager.assumeImmutableResults
352
- ? newResult
353
- : utilities.cloneDeep(newResult);
347
+ ObservableQuery.prototype.updateLastResult = function (newResult, variables) {
348
+ if (variables === void 0) { variables = this.variables; }
349
+ this.last = tslib.__assign(tslib.__assign({}, this.last), { result: this.queryManager.assumeImmutableResults
350
+ ? newResult
351
+ : utilities.cloneDeep(newResult), variables: variables });
354
352
  if (!utilities.isNonEmptyArray(newResult.errors)) {
355
- delete this.lastError;
353
+ delete this.last.error;
356
354
  }
357
- return previousResult;
355
+ return this.last;
358
356
  };
359
357
  ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
358
+ var _this = this;
360
359
  this.isTornDown = false;
361
360
  var useDisposableConcast = newNetworkStatus === exports.NetworkStatus.refetch ||
362
361
  newNetworkStatus === exports.NetworkStatus.fetchMore ||
@@ -377,18 +376,39 @@ var ObservableQuery = (function (_super) {
377
376
  }
378
377
  }
379
378
  }
379
+ var variables = options.variables && tslib.__assign({}, options.variables);
380
380
  var concast = this.fetch(options, newNetworkStatus);
381
+ var observer = {
382
+ next: function (result) {
383
+ _this.reportResult(result, variables);
384
+ },
385
+ error: function (error) {
386
+ _this.reportError(error, variables);
387
+ },
388
+ };
381
389
  if (!useDisposableConcast) {
382
- if (this.concast) {
390
+ if (this.concast && this.observer) {
383
391
  this.concast.removeObserver(this.observer, true);
384
392
  }
385
393
  this.concast = concast;
394
+ this.observer = observer;
386
395
  }
387
- concast.addObserver(this.observer);
396
+ concast.addObserver(observer);
388
397
  return concast.promise;
389
398
  };
390
399
  ObservableQuery.prototype.observe = function () {
391
- this.observer.next(this.getCurrentResult(false));
400
+ this.reportResult(this.getCurrentResult(false), this.variables);
401
+ };
402
+ ObservableQuery.prototype.reportResult = function (result, variables) {
403
+ if (this.getLastError() || this.isDifferentFromLastResult(result)) {
404
+ this.updateLastResult(result, variables);
405
+ utilities.iterateObserversSafely(this.observers, 'next', result);
406
+ }
407
+ };
408
+ ObservableQuery.prototype.reportError = function (error, variables) {
409
+ var errorResult = tslib.__assign(tslib.__assign({}, this.getLastResult()), { error: error, errors: error.graphQLErrors, networkStatus: exports.NetworkStatus.error, loading: false });
410
+ this.updateLastResult(errorResult, variables);
411
+ utilities.iterateObserversSafely(this.observers, 'error', this.last.error = error);
392
412
  };
393
413
  ObservableQuery.prototype.hasObservers = function () {
394
414
  return this.observers.size > 0;
@@ -396,9 +416,10 @@ var ObservableQuery = (function (_super) {
396
416
  ObservableQuery.prototype.tearDownQuery = function () {
397
417
  if (this.isTornDown)
398
418
  return;
399
- if (this.concast) {
419
+ if (this.concast && this.observer) {
400
420
  this.concast.removeObserver(this.observer);
401
421
  delete this.concast;
422
+ delete this.observer;
402
423
  }
403
424
  this.stopPolling();
404
425
  this.subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
@@ -413,6 +434,11 @@ utilities.fixObservableSubclass(ObservableQuery);
413
434
  function defaultSubscriptionObserverErrorCallback(error) {
414
435
  __DEV__ && tsInvariant.invariant.error('Unhandled error', error.message, error.stack);
415
436
  }
437
+ function logMissingFieldErrors(missing) {
438
+ if (__DEV__ && utilities.isNonEmptyArray(missing)) {
439
+ __DEV__ && tsInvariant.invariant.debug("Missing cache result fields: " + missing.map(function (m) { return m.path.join('.'); }).join(', '), missing);
440
+ }
441
+ }
416
442
  function applyNextFetchPolicy(options) {
417
443
  var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a, nextFetchPolicy = options.nextFetchPolicy;
418
444
  if (nextFetchPolicy) {
@@ -700,8 +726,9 @@ function cancelNotifyTimeout(info) {
700
726
  }
701
727
  }
702
728
  var QueryInfo = (function () {
703
- function QueryInfo(cache) {
704
- this.cache = cache;
729
+ function QueryInfo(queryManager, queryId) {
730
+ if (queryId === void 0) { queryId = queryManager.generateQueryId(); }
731
+ this.queryId = queryId;
705
732
  this.listeners = new Set();
706
733
  this.document = null;
707
734
  this.lastRequestId = 1;
@@ -709,6 +736,7 @@ var QueryInfo = (function () {
709
736
  this.stopped = false;
710
737
  this.dirty = false;
711
738
  this.observableQuery = null;
739
+ var cache = this.cache = queryManager.cache;
712
740
  if (!destructiveMethodCounts.has(cache)) {
713
741
  destructiveMethodCounts.set(cache, 0);
714
742
  wrapDestructiveCacheMethod(cache, "evict");
@@ -982,22 +1010,24 @@ var QueryManager = (function () {
982
1010
  this.fetchCancelFns.clear();
983
1011
  };
984
1012
  QueryManager.prototype.mutate = function (_a) {
985
- var mutation = _a.mutation, variables = _a.variables, optimisticResponse = _a.optimisticResponse, updateQueries = _a.updateQueries, _b = _a.refetchQueries, refetchQueries = _b === void 0 ? [] : _b, _c = _a.awaitRefetchQueries, awaitRefetchQueries = _c === void 0 ? false : _c, updateWithProxyFn = _a.update, onQueryUpdated = _a.onQueryUpdated, _d = _a.errorPolicy, errorPolicy = _d === void 0 ? 'none' : _d, fetchPolicy = _a.fetchPolicy, keepRootFields = _a.keepRootFields, context = _a.context;
1013
+ var mutation = _a.mutation, variables = _a.variables, optimisticResponse = _a.optimisticResponse, updateQueries = _a.updateQueries, _b = _a.refetchQueries, refetchQueries = _b === void 0 ? [] : _b, _c = _a.awaitRefetchQueries, awaitRefetchQueries = _c === void 0 ? false : _c, updateWithProxyFn = _a.update, onQueryUpdated = _a.onQueryUpdated, _d = _a.errorPolicy, errorPolicy = _d === void 0 ? 'none' : _d, _e = _a.fetchPolicy, fetchPolicy = _e === void 0 ? 'network-only' : _e, keepRootFields = _a.keepRootFields, context = _a.context;
986
1014
  return tslib.__awaiter(this, void 0, void 0, function () {
987
1015
  var mutationId, mutationStoreValue, self;
988
- return tslib.__generator(this, function (_e) {
989
- switch (_e.label) {
1016
+ return tslib.__generator(this, function (_f) {
1017
+ switch (_f.label) {
990
1018
  case 0:
991
1019
  __DEV__ ? tsInvariant.invariant(mutation, 'mutation option is required. You must specify your GraphQL document in the mutation option.') : tsInvariant.invariant(mutation, 15);
992
- __DEV__ ? tsInvariant.invariant(!fetchPolicy || fetchPolicy === 'no-cache', "Mutations only support a 'no-cache' fetchPolicy. If you don't want to disable the cache, remove your fetchPolicy setting to proceed with the default mutation behavior.") : tsInvariant.invariant(!fetchPolicy || fetchPolicy === 'no-cache', 16);
1020
+ __DEV__ ? tsInvariant.invariant(fetchPolicy === 'network-only' ||
1021
+ fetchPolicy === 'no-cache', "Mutations support only 'network-only' or 'no-cache' fetchPolicy strings. The default `network-only` behavior automatically writes mutation results to the cache. Passing `no-cache` skips the cache write.") : tsInvariant.invariant(fetchPolicy === 'network-only' ||
1022
+ fetchPolicy === 'no-cache', 16);
993
1023
  mutationId = this.generateMutationId();
994
1024
  mutation = this.transform(mutation).document;
995
1025
  variables = this.getVariables(mutation, variables);
996
1026
  if (!this.transform(mutation).hasClientExports) return [3, 2];
997
1027
  return [4, this.localState.addExportedVariables(mutation, variables, context)];
998
1028
  case 1:
999
- variables = (_e.sent());
1000
- _e.label = 2;
1029
+ variables = (_f.sent());
1030
+ _f.label = 2;
1001
1031
  case 2:
1002
1032
  mutationStoreValue = this.mutationStore &&
1003
1033
  (this.mutationStore[mutationId] = {
@@ -1256,7 +1286,7 @@ var QueryManager = (function () {
1256
1286
  if (typeof options.notifyOnNetworkStatusChange === 'undefined') {
1257
1287
  options.notifyOnNetworkStatusChange = false;
1258
1288
  }
1259
- var queryInfo = new QueryInfo(this.cache);
1289
+ var queryInfo = new QueryInfo(this);
1260
1290
  var observable = new ObservableQuery({
1261
1291
  queryManager: this,
1262
1292
  queryInfo: queryInfo,
@@ -1372,6 +1402,7 @@ var QueryManager = (function () {
1372
1402
  queryInfo: queryInfo,
1373
1403
  options: tslib.__assign(tslib.__assign({}, options), { fetchPolicy: "network-only" }),
1374
1404
  });
1405
+ tsInvariant.invariant(oq.queryId === queryId);
1375
1406
  queryInfo.setObservableQuery(oq);
1376
1407
  queries.set(queryId, oq);
1377
1408
  });
@@ -1666,10 +1697,9 @@ var QueryManager = (function () {
1666
1697
  if (networkStatus === void 0) { networkStatus = queryInfo.networkStatus || exports.NetworkStatus.loading; }
1667
1698
  var data = diff.result;
1668
1699
  if (__DEV__ &&
1669
- utilities.isNonEmptyArray(diff.missing) &&
1670
- !equality.equal(data, {}) &&
1671
- !returnPartialData) {
1672
- __DEV__ && tsInvariant.invariant.debug("Missing cache result fields: " + diff.missing.map(function (m) { return m.path.join('.'); }).join(', '), diff.missing);
1700
+ !returnPartialData &&
1701
+ !equality.equal(data, {})) {
1702
+ logMissingFieldErrors(diff.missing);
1673
1703
  }
1674
1704
  var fromData = function (data) { return utilities.Observable.of(tslib.__assign({ data: data, loading: isNetworkRequestInFlight(networkStatus), networkStatus: networkStatus }, (diff.complete ? null : { partial: true }))); };
1675
1705
  if (data && _this.transform(query).hasForcedResolvers) {
@@ -1756,7 +1786,7 @@ var QueryManager = (function () {
1756
1786
  };
1757
1787
  QueryManager.prototype.getQuery = function (queryId) {
1758
1788
  if (queryId && !this.queries.has(queryId)) {
1759
- this.queries.set(queryId, new QueryInfo(this.cache));
1789
+ this.queries.set(queryId, new QueryInfo(this, queryId));
1760
1790
  }
1761
1791
  return this.queries.get(queryId);
1762
1792
  };