@apollo/client 3.6.0-beta.7 → 3.6.0-rc.0

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 (60) hide show
  1. package/apollo-client.cjs +294 -199
  2. package/apollo-client.cjs.map +1 -1
  3. package/apollo-client.min.cjs +1 -1
  4. package/core/ApolloClient.d.ts +1 -1
  5. package/core/ApolloClient.d.ts.map +1 -1
  6. package/core/ApolloClient.js.map +1 -1
  7. package/core/ObservableQuery.d.ts +8 -3
  8. package/core/ObservableQuery.d.ts.map +1 -1
  9. package/core/ObservableQuery.js +84 -40
  10. package/core/ObservableQuery.js.map +1 -1
  11. package/core/QueryInfo.d.ts.map +1 -1
  12. package/core/QueryInfo.js +4 -2
  13. package/core/QueryInfo.js.map +1 -1
  14. package/core/QueryManager.d.ts.map +1 -1
  15. package/core/QueryManager.js +27 -8
  16. package/core/QueryManager.js.map +1 -1
  17. package/core/core.cjs +112 -50
  18. package/core/core.cjs.map +1 -1
  19. package/core/index.d.ts +1 -1
  20. package/core/index.d.ts.map +1 -1
  21. package/core/index.js +1 -1
  22. package/core/index.js.map +1 -1
  23. package/core/watchQueryOptions.d.ts +10 -1
  24. package/core/watchQueryOptions.d.ts.map +1 -1
  25. package/core/watchQueryOptions.js.map +1 -1
  26. package/invariantErrorCodes.js +1 -1
  27. package/package.json +15 -13
  28. package/react/hooks/hooks.cjs +181 -148
  29. package/react/hooks/hooks.cjs.map +1 -1
  30. package/react/hooks/index.d.ts +1 -1
  31. package/react/hooks/index.d.ts.map +1 -1
  32. package/react/hooks/index.js +1 -1
  33. package/react/hooks/index.js.map +1 -1
  34. package/react/hooks/useLazyQuery.d.ts +2 -2
  35. package/react/hooks/useLazyQuery.d.ts.map +1 -1
  36. package/react/hooks/useLazyQuery.js +37 -21
  37. package/react/hooks/useLazyQuery.js.map +1 -1
  38. package/react/hooks/useMutation.js.map +1 -1
  39. package/react/hooks/useQuery.d.ts +32 -1
  40. package/react/hooks/useQuery.d.ts.map +1 -1
  41. package/react/hooks/useQuery.js +152 -132
  42. package/react/hooks/useQuery.js.map +1 -1
  43. package/react/ssr/RenderPromises.d.ts +2 -1
  44. package/react/ssr/RenderPromises.d.ts.map +1 -1
  45. package/react/ssr/RenderPromises.js +23 -1
  46. package/react/ssr/RenderPromises.js.map +1 -1
  47. package/react/ssr/ssr.cjs +23 -1
  48. package/react/ssr/ssr.cjs.map +1 -1
  49. package/react/types/types.d.ts +9 -11
  50. package/react/types/types.d.ts.map +1 -1
  51. package/react/types/types.js.map +1 -1
  52. package/testing/core/mocking/mockFetch.js +1 -1
  53. package/testing/core/mocking/mockFetch.js.map +1 -1
  54. package/utilities/graphql/transform.d.ts +2 -2
  55. package/utilities/graphql/transform.d.ts.map +1 -1
  56. package/utilities/graphql/transform.js +1 -1
  57. package/utilities/graphql/transform.js.map +1 -1
  58. package/utilities/utilities.cjs +1 -1
  59. package/utilities/utilities.cjs.map +1 -1
  60. package/version.js +1 -1
package/core/core.cjs CHANGED
@@ -15,7 +15,7 @@ var utils = require('../link/utils');
15
15
  var tsInvariant = require('ts-invariant');
16
16
  var graphqlTag = require('graphql-tag');
17
17
 
18
- var version = '3.6.0-beta.7';
18
+ var version = '3.6.0-rc.0';
19
19
 
20
20
  exports.NetworkStatus = void 0;
21
21
  (function (NetworkStatus) {
@@ -32,7 +32,6 @@ function isNetworkRequestInFlight(networkStatus) {
32
32
  }
33
33
 
34
34
  var assign = Object.assign, hasOwnProperty$1 = Object.hasOwnProperty;
35
- var warnedAboutUpdateQuery = false;
36
35
  var ObservableQuery = (function (_super) {
37
36
  tslib.__extends(ObservableQuery, _super);
38
37
  function ObservableQuery(_a) {
@@ -66,11 +65,10 @@ var ObservableQuery = (function (_super) {
66
65
  _this.observers = new Set();
67
66
  _this.subscriptions = new Set();
68
67
  _this.isTornDown = false;
69
- _this.options = options;
68
+ _this.options = tslib.__assign({ initialFetchPolicy: options.fetchPolicy || "cache-first" }, options);
70
69
  _this.queryId = queryInfo.queryId || queryManager.generateQueryId();
71
70
  var opDef = utilities.getOperationDefinition(options.query);
72
71
  _this.queryName = opDef && opDef.name && opDef.name.value;
73
- _this.initialFetchPolicy = options.fetchPolicy || "cache-first";
74
72
  _this.queryManager = queryManager;
75
73
  _this.queryInfo = queryInfo;
76
74
  return _this;
@@ -204,35 +202,49 @@ var ObservableQuery = (function (_super) {
204
202
  var _this = this;
205
203
  var combinedOptions = tslib.__assign(tslib.__assign({}, (fetchMoreOptions.query ? fetchMoreOptions : tslib.__assign(tslib.__assign(tslib.__assign({}, this.options), fetchMoreOptions), { variables: tslib.__assign(tslib.__assign({}, this.options.variables), fetchMoreOptions.variables) }))), { fetchPolicy: "no-cache" });
206
204
  var qid = this.queryManager.generateQueryId();
205
+ var queryInfo = this.queryInfo;
206
+ var originalNetworkStatus = queryInfo.networkStatus;
207
+ queryInfo.networkStatus = exports.NetworkStatus.fetchMore;
207
208
  if (combinedOptions.notifyOnNetworkStatusChange) {
208
- this.queryInfo.networkStatus = exports.NetworkStatus.fetchMore;
209
209
  this.observe();
210
210
  }
211
+ var updatedQuerySet = new Set();
211
212
  return this.queryManager.fetchQuery(qid, combinedOptions, exports.NetworkStatus.fetchMore).then(function (fetchMoreResult) {
212
- var data = fetchMoreResult.data;
213
- var updateQuery = fetchMoreOptions.updateQuery;
214
- if (updateQuery) {
215
- if (__DEV__ &&
216
- !warnedAboutUpdateQuery) {
217
- __DEV__ && globals.invariant.warn("The updateQuery callback for fetchMore is deprecated, and will be removed\nin the next major version of Apollo Client.\n\nPlease convert updateQuery functions to field policies with appropriate\nread and merge functions, or use/adapt a helper function (such as\nconcatPagination, offsetLimitPagination, or relayStylePagination) from\n@apollo/client/utilities.\n\nThe field policy system handles pagination more effectively than a\nhand-written updateQuery function, and you only need to define the policy\nonce, rather than every time you call fetchMore.");
218
- warnedAboutUpdateQuery = true;
219
- }
220
- _this.updateQuery(function (previous) { return updateQuery(previous, {
221
- fetchMoreResult: data,
222
- variables: combinedOptions.variables,
223
- }); });
224
- }
225
- else {
226
- _this.queryManager.cache.writeQuery({
227
- query: combinedOptions.query,
228
- variables: combinedOptions.variables,
229
- data: data,
230
- });
213
+ _this.queryManager.removeQuery(qid);
214
+ if (queryInfo.networkStatus === exports.NetworkStatus.fetchMore) {
215
+ queryInfo.networkStatus = originalNetworkStatus;
231
216
  }
217
+ _this.queryManager.cache.batch({
218
+ update: function (cache) {
219
+ var updateQuery = fetchMoreOptions.updateQuery;
220
+ if (updateQuery) {
221
+ cache.updateQuery({
222
+ query: _this.options.query,
223
+ variables: _this.variables,
224
+ returnPartialData: true,
225
+ optimistic: false,
226
+ }, function (previous) { return updateQuery(previous, {
227
+ fetchMoreResult: fetchMoreResult.data,
228
+ variables: combinedOptions.variables,
229
+ }); });
230
+ }
231
+ else {
232
+ cache.writeQuery({
233
+ query: combinedOptions.query,
234
+ variables: combinedOptions.variables,
235
+ data: fetchMoreResult.data,
236
+ });
237
+ }
238
+ },
239
+ onWatchUpdated: function (watch) {
240
+ updatedQuerySet.add(watch.query);
241
+ },
242
+ });
232
243
  return fetchMoreResult;
233
244
  }).finally(function () {
234
- _this.queryManager.stopQuery(qid);
235
- _this.reobserve();
245
+ if (!updatedQuerySet.has(_this.options.query)) {
246
+ reobserveCacheFirst(_this);
247
+ }
236
248
  });
237
249
  };
238
250
  ObservableQuery.prototype.subscribeToMore = function (options) {
@@ -285,7 +297,7 @@ var ObservableQuery = (function (_super) {
285
297
  return Promise.resolve();
286
298
  }
287
299
  return this.reobserve({
288
- fetchPolicy: this.initialFetchPolicy,
300
+ fetchPolicy: this.options.initialFetchPolicy,
289
301
  variables: variables,
290
302
  }, exports.NetworkStatus.setVariables);
291
303
  };
@@ -317,6 +329,26 @@ var ObservableQuery = (function (_super) {
317
329
  this.options.pollInterval = 0;
318
330
  this.updatePolling();
319
331
  };
332
+ ObservableQuery.prototype.applyNextFetchPolicy = function (reason, options) {
333
+ if (options.nextFetchPolicy) {
334
+ var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a, _b = options.initialFetchPolicy, initialFetchPolicy = _b === void 0 ? fetchPolicy : _b;
335
+ if (typeof options.nextFetchPolicy === "function") {
336
+ options.fetchPolicy = options.nextFetchPolicy(fetchPolicy, {
337
+ reason: reason,
338
+ options: options,
339
+ observable: this,
340
+ initialFetchPolicy: initialFetchPolicy,
341
+ });
342
+ }
343
+ else if (reason === "variables-changed") {
344
+ options.fetchPolicy = initialFetchPolicy;
345
+ }
346
+ else {
347
+ options.fetchPolicy = options.nextFetchPolicy;
348
+ }
349
+ }
350
+ return options.fetchPolicy;
351
+ };
320
352
  ObservableQuery.prototype.fetch = function (options, newNetworkStatus) {
321
353
  this.queryManager.setObservableQuery(this);
322
354
  return this.queryManager.fetchQueryObservable(this.queryId, options, newNetworkStatus);
@@ -379,16 +411,18 @@ var ObservableQuery = (function (_super) {
379
411
  newNetworkStatus === exports.NetworkStatus.fetchMore ||
380
412
  newNetworkStatus === exports.NetworkStatus.poll;
381
413
  var oldVariables = this.options.variables;
414
+ var oldFetchPolicy = this.options.fetchPolicy;
415
+ var mergedOptions = mergeOptions(this.options, newOptions || {});
382
416
  var options = useDisposableConcast
383
- ? utilities.compact(this.options, newOptions)
384
- : assign(this.options, utilities.compact(newOptions));
417
+ ? mergedOptions
418
+ : assign(this.options, mergedOptions);
385
419
  if (!useDisposableConcast) {
386
420
  this.updatePolling();
387
421
  if (newOptions &&
388
422
  newOptions.variables &&
389
- !newOptions.fetchPolicy &&
390
- !equality.equal(newOptions.variables, oldVariables)) {
391
- options.fetchPolicy = this.initialFetchPolicy;
423
+ !equality.equal(newOptions.variables, oldVariables) &&
424
+ (!newOptions.fetchPolicy || newOptions.fetchPolicy === oldFetchPolicy)) {
425
+ this.applyNextFetchPolicy("variables-changed", options);
392
426
  if (newNetworkStatus === void 0) {
393
427
  newNetworkStatus = exports.NetworkStatus.setVariables;
394
428
  }
@@ -452,6 +486,23 @@ var ObservableQuery = (function (_super) {
452
486
  return ObservableQuery;
453
487
  }(utilities.Observable));
454
488
  utilities.fixObservableSubclass(ObservableQuery);
489
+ function reobserveCacheFirst(obsQuery) {
490
+ var _a = obsQuery.options, fetchPolicy = _a.fetchPolicy, nextFetchPolicy = _a.nextFetchPolicy;
491
+ if (fetchPolicy === "cache-and-network" ||
492
+ fetchPolicy === "network-only") {
493
+ return obsQuery.reobserve({
494
+ fetchPolicy: "cache-first",
495
+ nextFetchPolicy: function () {
496
+ this.nextFetchPolicy = nextFetchPolicy;
497
+ if (typeof nextFetchPolicy === "function") {
498
+ return nextFetchPolicy.apply(this, arguments);
499
+ }
500
+ return fetchPolicy;
501
+ },
502
+ });
503
+ }
504
+ return obsQuery.reobserve();
505
+ }
455
506
  function defaultSubscriptionObserverErrorCallback(error) {
456
507
  __DEV__ && globals.invariant.error('Unhandled error', error.message, error.stack);
457
508
  }
@@ -460,14 +511,6 @@ function logMissingFieldErrors(missing) {
460
511
  __DEV__ && globals.invariant.debug("Missing cache result fields: ".concat(JSON.stringify(missing)), missing);
461
512
  }
462
513
  }
463
- function applyNextFetchPolicy(options) {
464
- var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a, nextFetchPolicy = options.nextFetchPolicy;
465
- if (nextFetchPolicy) {
466
- options.fetchPolicy = typeof nextFetchPolicy === "function"
467
- ? nextFetchPolicy.call(options, fetchPolicy)
468
- : nextFetchPolicy;
469
- }
470
- }
471
514
 
472
515
  var LocalState = (function () {
473
516
  function LocalState(_a) {
@@ -850,11 +893,12 @@ var QueryInfo = (function () {
850
893
  if (oq) {
851
894
  oq["queryInfo"] = this;
852
895
  this.listeners.add(this.oqListener = function () {
853
- if (_this.getDiff().fromOptimisticTransaction) {
896
+ var diff = _this.getDiff();
897
+ if (diff.fromOptimisticTransaction) {
854
898
  oq["observe"]();
855
899
  }
856
900
  else {
857
- oq.reobserve();
901
+ reobserveCacheFirst(oq);
858
902
  }
859
903
  });
860
904
  }
@@ -1503,8 +1547,10 @@ var QueryManager = (function () {
1503
1547
  };
1504
1548
  QueryManager.prototype.removeQuery = function (queryId) {
1505
1549
  this.fetchCancelFns.delete(queryId);
1506
- this.getQuery(queryId).stop();
1507
- this.queries.delete(queryId);
1550
+ if (this.queries.has(queryId)) {
1551
+ this.getQuery(queryId).stop();
1552
+ this.queries.delete(queryId);
1553
+ }
1508
1554
  };
1509
1555
  QueryManager.prototype.broadcastQueries = function () {
1510
1556
  if (this.onBroadcast)
@@ -1588,10 +1634,11 @@ var QueryManager = (function () {
1588
1634
  var aqr = {
1589
1635
  data: result.data,
1590
1636
  loading: false,
1591
- networkStatus: queryInfo.networkStatus || exports.NetworkStatus.ready,
1637
+ networkStatus: exports.NetworkStatus.ready,
1592
1638
  };
1593
1639
  if (hasErrors && options.errorPolicy !== "ignore") {
1594
1640
  aqr.errors = result.errors;
1641
+ aqr.networkStatus = exports.NetworkStatus.error;
1595
1642
  }
1596
1643
  return aqr;
1597
1644
  }, function (networkError) {
@@ -1633,7 +1680,9 @@ var QueryManager = (function () {
1633
1680
  : fromVariables(normalized.variables));
1634
1681
  concast.cleanup(function () {
1635
1682
  _this.fetchCancelFns.delete(queryId);
1636
- applyNextFetchPolicy(options);
1683
+ if (queryInfo.observableQuery) {
1684
+ queryInfo.observableQuery["applyNextFetchPolicy"]("after-fetch", options);
1685
+ }
1637
1686
  });
1638
1687
  return concast;
1639
1688
  };
@@ -1707,7 +1756,7 @@ var QueryManager = (function () {
1707
1756
  };
1708
1757
  QueryManager.prototype.fetchQueryByPolicy = function (queryInfo, _a, networkStatus) {
1709
1758
  var _this = this;
1710
- var query = _a.query, variables = _a.variables, fetchPolicy = _a.fetchPolicy, refetchWritePolicy = _a.refetchWritePolicy, errorPolicy = _a.errorPolicy, returnPartialData = _a.returnPartialData, context = _a.context, notifyOnNetworkStatusChange = _a.notifyOnNetworkStatusChange;
1759
+ var query = _a.query, variables = _a.variables, fetchPolicy = _a.fetchPolicy, refetchWritePolicy = _a.refetchWritePolicy, errorPolicy = _a.errorPolicy, returnPartialData = _a.returnPartialData, context = _a.context, notifyOnNetworkStatusChange = _a.notifyOnNetworkStatusChange, fetchBlockingPromise = _a.fetchBlockingPromise;
1711
1760
  var oldNetworkStatus = queryInfo.networkStatus;
1712
1761
  queryInfo.init({
1713
1762
  document: query,
@@ -1740,12 +1789,26 @@ var QueryManager = (function () {
1740
1789
  refetchWritePolicy !== "merge") ? 1
1741
1790
  : 2;
1742
1791
  var resultsFromLink = function () {
1743
- return _this.getResultsFromLink(queryInfo, cacheWriteBehavior, {
1792
+ var get = function () { return _this.getResultsFromLink(queryInfo, cacheWriteBehavior, {
1744
1793
  variables: variables,
1745
1794
  context: context,
1746
1795
  fetchPolicy: fetchPolicy,
1747
1796
  errorPolicy: errorPolicy,
1748
- });
1797
+ }); };
1798
+ return fetchBlockingPromise ? fetchBlockingPromise.then(function (ok) { return ok ? get() : utilities.Observable.of(); }, function (error) {
1799
+ var apolloError = errors.isApolloError(error)
1800
+ ? error
1801
+ : new errors.ApolloError({ clientErrors: [error] });
1802
+ if (errorPolicy !== "ignore") {
1803
+ queryInfo.markError(apolloError);
1804
+ }
1805
+ return utilities.Observable.of({
1806
+ loading: false,
1807
+ networkStatus: exports.NetworkStatus.error,
1808
+ error: apolloError,
1809
+ data: readCache().result,
1810
+ });
1811
+ }) : get();
1749
1812
  };
1750
1813
  var shouldNotify = notifyOnNetworkStatusChange &&
1751
1814
  typeof oldNetworkStatus === "number" &&
@@ -2080,7 +2143,6 @@ exports.gql = graphqlTag.gql;
2080
2143
  exports.resetCaches = graphqlTag.resetCaches;
2081
2144
  exports.ApolloClient = ApolloClient;
2082
2145
  exports.ObservableQuery = ObservableQuery;
2083
- exports.applyNextFetchPolicy = applyNextFetchPolicy;
2084
2146
  exports.mergeOptions = mergeOptions;
2085
2147
  for (var k in core) {
2086
2148
  if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = core[k];