@apollo/client 3.13.5 → 3.13.7

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/core/core.cjs CHANGED
@@ -10,6 +10,7 @@ var equal = require('@wry/equality');
10
10
  var utilities = require('../utilities');
11
11
  var cache = require('../cache');
12
12
  var errors = require('../errors');
13
+ var optimism = require('optimism');
13
14
  var trie = require('@wry/trie');
14
15
  var masking = require('../masking');
15
16
  var graphql = require('graphql');
@@ -21,7 +22,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
21
22
 
22
23
  var equal__default = /*#__PURE__*/_interopDefaultLegacy(equal);
23
24
 
24
- var version = "3.13.5";
25
+ var version = "3.13.7";
25
26
 
26
27
  function isNonNullObject(obj) {
27
28
  return obj !== null && typeof obj === "object";
@@ -199,7 +200,13 @@ var ObservableQuery = (function (_super) {
199
200
  tslib.__extends(ObservableQuery, _super);
200
201
  function ObservableQuery(_a) {
201
202
  var queryManager = _a.queryManager, queryInfo = _a.queryInfo, options = _a.options;
202
- var _this = _super.call(this, function (observer) {
203
+ var _this = this;
204
+ var startedInactive = ObservableQuery.inactiveOnCreation.getValue();
205
+ _this = _super.call(this, function (observer) {
206
+ if (startedInactive) {
207
+ queryManager["queries"].set(_this.queryId, queryInfo);
208
+ startedInactive = false;
209
+ }
203
210
  try {
204
211
  var subObserver = observer._subscription._observer;
205
212
  if (subObserver && !subObserver.error) {
@@ -227,6 +234,7 @@ var ObservableQuery = (function (_super) {
227
234
  }) || this;
228
235
  _this.observers = new Set();
229
236
  _this.subscriptions = new Set();
237
+ _this.dirty = false;
230
238
  _this.queryInfo = queryInfo;
231
239
  _this.queryManager = queryManager;
232
240
  _this.waitForOwnResult = skipCacheDataFor(options.fetchPolicy);
@@ -470,7 +478,7 @@ var ObservableQuery = (function (_super) {
470
478
  })
471
479
  .finally(function () {
472
480
  if (isCached && !updatedQuerySet.has(_this.query)) {
473
- reobserveCacheFirst(_this);
481
+ _this.reobserveCacheFirst();
474
482
  }
475
483
  });
476
484
  };
@@ -579,8 +587,9 @@ var ObservableQuery = (function (_super) {
579
587
  return options.fetchPolicy;
580
588
  };
581
589
  ObservableQuery.prototype.fetch = function (options, newNetworkStatus, query) {
582
- this.queryManager.setObservableQuery(this);
583
- return this.queryManager["fetchConcastWithInfo"](this.queryId, options, newNetworkStatus, query);
590
+ var queryInfo = this.queryManager.getOrCreateQuery(this.queryId);
591
+ queryInfo.setObservableQuery(this);
592
+ return this.queryManager["fetchConcastWithInfo"](queryInfo, options, newNetworkStatus, query);
584
593
  };
585
594
  ObservableQuery.prototype.updatePolling = function () {
586
595
  var _this = this;
@@ -762,25 +771,62 @@ var ObservableQuery = (function (_super) {
762
771
  id: this.queryId,
763
772
  }) }) : result;
764
773
  };
774
+ ObservableQuery.prototype.resetNotifications = function () {
775
+ this.cancelNotifyTimeout();
776
+ this.dirty = false;
777
+ };
778
+ ObservableQuery.prototype.cancelNotifyTimeout = function () {
779
+ if (this.notifyTimeout) {
780
+ clearTimeout(this.notifyTimeout);
781
+ this.notifyTimeout = void 0;
782
+ }
783
+ };
784
+ ObservableQuery.prototype.scheduleNotify = function () {
785
+ var _this = this;
786
+ if (this.dirty)
787
+ return;
788
+ this.dirty = true;
789
+ if (!this.notifyTimeout) {
790
+ this.notifyTimeout = setTimeout(function () { return _this.notify(); }, 0);
791
+ }
792
+ };
793
+ ObservableQuery.prototype.notify = function () {
794
+ this.cancelNotifyTimeout();
795
+ if (this.dirty) {
796
+ if (this.options.fetchPolicy == "cache-only" ||
797
+ this.options.fetchPolicy == "cache-and-network" ||
798
+ !isNetworkRequestInFlight(this.queryInfo.networkStatus)) {
799
+ var diff = this.queryInfo.getDiff();
800
+ if (diff.fromOptimisticTransaction) {
801
+ this.observe();
802
+ }
803
+ else {
804
+ this.reobserveCacheFirst();
805
+ }
806
+ }
807
+ }
808
+ this.dirty = false;
809
+ };
810
+ ObservableQuery.prototype.reobserveCacheFirst = function () {
811
+ var _a = this.options, fetchPolicy = _a.fetchPolicy, nextFetchPolicy = _a.nextFetchPolicy;
812
+ if (fetchPolicy === "cache-and-network" || fetchPolicy === "network-only") {
813
+ return this.reobserve({
814
+ fetchPolicy: "cache-first",
815
+ nextFetchPolicy: function (currentFetchPolicy, context) {
816
+ this.nextFetchPolicy = nextFetchPolicy;
817
+ if (typeof this.nextFetchPolicy === "function") {
818
+ return this.nextFetchPolicy(currentFetchPolicy, context);
819
+ }
820
+ return fetchPolicy;
821
+ },
822
+ });
823
+ }
824
+ return this.reobserve();
825
+ };
826
+ ObservableQuery.inactiveOnCreation = new optimism.Slot();
765
827
  return ObservableQuery;
766
828
  }(utilities.Observable));
767
829
  utilities.fixObservableSubclass(ObservableQuery);
768
- function reobserveCacheFirst(obsQuery) {
769
- var _a = obsQuery.options, fetchPolicy = _a.fetchPolicy, nextFetchPolicy = _a.nextFetchPolicy;
770
- if (fetchPolicy === "cache-and-network" || fetchPolicy === "network-only") {
771
- return obsQuery.reobserve({
772
- fetchPolicy: "cache-first",
773
- nextFetchPolicy: function (currentFetchPolicy, context) {
774
- this.nextFetchPolicy = nextFetchPolicy;
775
- if (typeof this.nextFetchPolicy === "function") {
776
- return this.nextFetchPolicy(currentFetchPolicy, context);
777
- }
778
- return fetchPolicy;
779
- },
780
- });
781
- }
782
- return obsQuery.reobserve();
783
- }
784
830
  function defaultSubscriptionObserverErrorCallback(error) {
785
831
  globalThis.__DEV__ !== false && globals.invariant.error(25, error.message, error.stack);
786
832
  }
@@ -806,21 +852,13 @@ function wrapDestructiveCacheMethod(cache, methodName) {
806
852
  };
807
853
  }
808
854
  }
809
- function cancelNotifyTimeout(info) {
810
- if (info["notifyTimeout"]) {
811
- clearTimeout(info["notifyTimeout"]);
812
- info["notifyTimeout"] = void 0;
813
- }
814
- }
815
855
  var QueryInfo = (function () {
816
856
  function QueryInfo(queryManager, queryId) {
817
857
  if (queryId === void 0) { queryId = queryManager.generateQueryId(); }
818
858
  this.queryId = queryId;
819
- this.listeners = new Set();
820
859
  this.document = null;
821
860
  this.lastRequestId = 1;
822
861
  this.stopped = false;
823
- this.dirty = false;
824
862
  this.observableQuery = null;
825
863
  var cache = (this.cache = queryManager.cache);
826
864
  if (!destructiveMethodCounts.has(cache)) {
@@ -856,10 +894,6 @@ var QueryInfo = (function () {
856
894
  }
857
895
  return this;
858
896
  };
859
- QueryInfo.prototype.reset = function () {
860
- cancelNotifyTimeout(this);
861
- this.dirty = false;
862
- };
863
897
  QueryInfo.prototype.resetDiff = function () {
864
898
  this.lastDiff = void 0;
865
899
  };
@@ -898,68 +932,29 @@ var QueryInfo = (function () {
898
932
  };
899
933
  };
900
934
  QueryInfo.prototype.setDiff = function (diff) {
901
- var _this = this;
902
- var _a;
935
+ var _a, _b;
903
936
  var oldDiff = this.lastDiff && this.lastDiff.diff;
904
937
  if (diff && !diff.complete && ((_a = this.observableQuery) === null || _a === void 0 ? void 0 : _a.getLastError())) {
905
938
  return;
906
939
  }
907
940
  this.updateLastDiff(diff);
908
- if (!this.dirty && !equal.equal(oldDiff && oldDiff.result, diff && diff.result)) {
909
- this.dirty = true;
910
- if (!this.notifyTimeout) {
911
- this.notifyTimeout = setTimeout(function () { return _this.notify(); }, 0);
912
- }
941
+ if (!equal.equal(oldDiff && oldDiff.result, diff && diff.result)) {
942
+ (_b = this.observableQuery) === null || _b === void 0 ? void 0 : _b["scheduleNotify"]();
913
943
  }
914
944
  };
915
945
  QueryInfo.prototype.setObservableQuery = function (oq) {
916
- var _this = this;
917
946
  if (oq === this.observableQuery)
918
947
  return;
919
- if (this.oqListener) {
920
- this.listeners.delete(this.oqListener);
921
- }
922
948
  this.observableQuery = oq;
923
949
  if (oq) {
924
950
  oq["queryInfo"] = this;
925
- this.listeners.add((this.oqListener = function () {
926
- var diff = _this.getDiff();
927
- if (diff.fromOptimisticTransaction) {
928
- oq["observe"]();
929
- }
930
- else {
931
- reobserveCacheFirst(oq);
932
- }
933
- }));
934
- }
935
- else {
936
- delete this.oqListener;
937
951
  }
938
952
  };
939
- QueryInfo.prototype.notify = function () {
940
- var _this = this;
941
- cancelNotifyTimeout(this);
942
- if (this.shouldNotify()) {
943
- this.listeners.forEach(function (listener) { return listener(_this); });
944
- }
945
- this.dirty = false;
946
- };
947
- QueryInfo.prototype.shouldNotify = function () {
948
- if (!this.dirty || !this.listeners.size) {
949
- return false;
950
- }
951
- if (isNetworkRequestInFlight(this.networkStatus) && this.observableQuery) {
952
- var fetchPolicy = this.observableQuery.options.fetchPolicy;
953
- if (fetchPolicy !== "cache-only" && fetchPolicy !== "cache-and-network") {
954
- return false;
955
- }
956
- }
957
- return true;
958
- };
959
953
  QueryInfo.prototype.stop = function () {
954
+ var _a;
960
955
  if (!this.stopped) {
961
956
  this.stopped = true;
962
- this.reset();
957
+ (_a = this.observableQuery) === null || _a === void 0 ? void 0 : _a["resetNotifications"]();
963
958
  this.cancel();
964
959
  var oq = this.observableQuery;
965
960
  if (oq)
@@ -996,9 +991,10 @@ var QueryInfo = (function () {
996
991
  };
997
992
  QueryInfo.prototype.markResult = function (result, document, options, cacheWriteBehavior) {
998
993
  var _this = this;
994
+ var _a;
999
995
  var merger = new utilities.DeepMerger();
1000
996
  var graphQLErrors = utilities.isNonEmptyArray(result.errors) ? result.errors.slice(0) : [];
1001
- this.reset();
997
+ (_a = this.observableQuery) === null || _a === void 0 ? void 0 : _a["resetNotifications"]();
1002
998
  if ("incremental" in result && utilities.isNonEmptyArray(result.incremental)) {
1003
999
  var mergedData = utilities.mergeIncrementalData(this.getDiff().result, result);
1004
1000
  result.data = mergedData;
@@ -1054,9 +1050,10 @@ var QueryInfo = (function () {
1054
1050
  return (this.networkStatus = exports.NetworkStatus.ready);
1055
1051
  };
1056
1052
  QueryInfo.prototype.markError = function (error) {
1053
+ var _a;
1057
1054
  this.networkStatus = exports.NetworkStatus.error;
1058
1055
  this.lastWrite = void 0;
1059
- this.reset();
1056
+ (_a = this.observableQuery) === null || _a === void 0 ? void 0 : _a["resetNotifications"]();
1060
1057
  if (error.graphQLErrors) {
1061
1058
  this.graphQLErrors = error.graphQLErrors;
1062
1059
  }
@@ -1384,8 +1381,7 @@ var QueryManager = (function () {
1384
1381
  return true;
1385
1382
  };
1386
1383
  QueryManager.prototype.fetchQuery = function (queryId, options, networkStatus) {
1387
- return this.fetchConcastWithInfo(queryId, options, networkStatus).concast
1388
- .promise;
1384
+ return this.fetchConcastWithInfo(this.getOrCreateQuery(queryId), options, networkStatus).concast.promise;
1389
1385
  };
1390
1386
  QueryManager.prototype.getQueryStore = function () {
1391
1387
  var store = Object.create(null);
@@ -1453,7 +1449,9 @@ var QueryManager = (function () {
1453
1449
  options: options,
1454
1450
  });
1455
1451
  observable["lastQuery"] = query;
1456
- this.queries.set(observable.queryId, queryInfo);
1452
+ if (!ObservableQuery["inactiveOnCreation"].getValue()) {
1453
+ this.queries.set(observable.queryId, queryInfo);
1454
+ }
1457
1455
  queryInfo.init({
1458
1456
  document: query,
1459
1457
  observableQuery: observable,
@@ -1565,7 +1563,7 @@ var QueryManager = (function () {
1565
1563
  if (legacyQueryOptions.size) {
1566
1564
  legacyQueryOptions.forEach(function (options) {
1567
1565
  var queryId = utilities.makeUniqueId("legacyOneTimeQuery");
1568
- var queryInfo = _this.getQuery(queryId).init({
1566
+ var queryInfo = _this.getOrCreateQuery(queryId).init({
1569
1567
  document: options.query,
1570
1568
  variables: options.variables,
1571
1569
  });
@@ -1605,14 +1603,11 @@ var QueryManager = (function () {
1605
1603
  (fetchPolicy !== "standby" && fetchPolicy !== "cache-only")) {
1606
1604
  observableQueryPromises.push(observableQuery.refetch());
1607
1605
  }
1608
- _this.getQuery(queryId).setDiff(null);
1606
+ (_this.queries.get(queryId) || observableQuery["queryInfo"]).setDiff(null);
1609
1607
  });
1610
1608
  this.broadcastQueries();
1611
1609
  return Promise.all(observableQueryPromises);
1612
1610
  };
1613
- QueryManager.prototype.setObservableQuery = function (observableQuery) {
1614
- this.getQuery(observableQuery.queryId).setObservableQuery(observableQuery);
1615
- };
1616
1611
  QueryManager.prototype.startGraphQLSubscription = function (options) {
1617
1612
  var _this = this;
1618
1613
  var query = options.query, variables = options.variables;
@@ -1673,16 +1668,17 @@ var QueryManager = (function () {
1673
1668
  this.removeQuery(queryId);
1674
1669
  };
1675
1670
  QueryManager.prototype.removeQuery = function (queryId) {
1671
+ var _a;
1676
1672
  this.fetchCancelFns.delete(queryId);
1677
1673
  if (this.queries.has(queryId)) {
1678
- this.getQuery(queryId).stop();
1674
+ (_a = this.queries.get(queryId)) === null || _a === void 0 ? void 0 : _a.stop();
1679
1675
  this.queries.delete(queryId);
1680
1676
  }
1681
1677
  };
1682
1678
  QueryManager.prototype.broadcastQueries = function () {
1683
1679
  if (this.onBroadcast)
1684
1680
  this.onBroadcast();
1685
- this.queries.forEach(function (info) { return info.notify(); });
1681
+ this.queries.forEach(function (info) { var _a; return (_a = info.observableQuery) === null || _a === void 0 ? void 0 : _a["notify"](); });
1686
1682
  };
1687
1683
  QueryManager.prototype.getLocalState = function () {
1688
1684
  return this.localState;
@@ -1783,13 +1779,12 @@ var QueryManager = (function () {
1783
1779
  throw error;
1784
1780
  });
1785
1781
  };
1786
- QueryManager.prototype.fetchConcastWithInfo = function (queryId, options,
1782
+ QueryManager.prototype.fetchConcastWithInfo = function (queryInfo, options,
1787
1783
  networkStatus, query) {
1788
1784
  var _this = this;
1789
1785
  if (networkStatus === void 0) { networkStatus = exports.NetworkStatus.loading; }
1790
1786
  if (query === void 0) { query = options.query; }
1791
1787
  var variables = this.getVariables(query, options.variables);
1792
- var queryInfo = this.getQuery(queryId);
1793
1788
  var defaults = this.defaultOptions.watchQuery;
1794
1789
  var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? (defaults && defaults.fetchPolicy) || "cache-first" : _a, _b = options.errorPolicy, errorPolicy = _b === void 0 ? (defaults && defaults.errorPolicy) || "none" : _b, _c = options.returnPartialData, returnPartialData = _c === void 0 ? false : _c, _d = options.notifyOnNetworkStatusChange, notifyOnNetworkStatusChange = _d === void 0 ? false : _d, _e = options.context, context = _e === void 0 ? {} : _e;
1795
1790
  var normalized = Object.assign({}, options, {
@@ -1812,8 +1807,8 @@ var QueryManager = (function () {
1812
1807
  }
1813
1808
  return sourcesWithInfo;
1814
1809
  };
1815
- var cleanupCancelFn = function () { return _this.fetchCancelFns.delete(queryId); };
1816
- this.fetchCancelFns.set(queryId, function (reason) {
1810
+ var cleanupCancelFn = function () { return _this.fetchCancelFns.delete(queryInfo.queryId); };
1811
+ this.fetchCancelFns.set(queryInfo.queryId, function (reason) {
1817
1812
  cleanupCancelFn();
1818
1813
  setTimeout(function () { return concast.cancel(reason); });
1819
1814
  });
@@ -1844,7 +1839,7 @@ var QueryManager = (function () {
1844
1839
  this.getObservableQueries(include).forEach(function (oq, queryId) {
1845
1840
  includedQueriesById.set(queryId, {
1846
1841
  oq: oq,
1847
- lastDiff: _this.getQuery(queryId).getDiff(),
1842
+ lastDiff: (_this.queries.get(queryId) || oq["queryInfo"]).getDiff(),
1848
1843
  });
1849
1844
  });
1850
1845
  }
@@ -1881,9 +1876,7 @@ var QueryManager = (function () {
1881
1876
  var result;
1882
1877
  if (onQueryUpdated) {
1883
1878
  if (!diff) {
1884
- var info = oq["queryInfo"];
1885
- info.reset();
1886
- diff = info.getDiff();
1879
+ diff = _this.cache.diff(oq["queryInfo"]["getDiffOptions"]());
1887
1880
  }
1888
1881
  result = onQueryUpdated(oq, diff, lastDiff);
1889
1882
  }
@@ -2040,7 +2033,7 @@ var QueryManager = (function () {
2040
2033
  return { fromLink: false, sources: [] };
2041
2034
  }
2042
2035
  };
2043
- QueryManager.prototype.getQuery = function (queryId) {
2036
+ QueryManager.prototype.getOrCreateQuery = function (queryId) {
2044
2037
  if (queryId && !this.queries.has(queryId)) {
2045
2038
  this.queries.set(queryId, new QueryInfo(this, queryId));
2046
2039
  }