@apollo/client 4.0.0-alpha.19 → 4.0.0-alpha.20

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 (50) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/__cjs/core/ApolloClient.cjs +2 -2
  3. package/__cjs/core/ApolloClient.cjs.map +1 -1
  4. package/__cjs/core/ApolloClient.d.cts +1 -1
  5. package/__cjs/core/ObservableQuery.cjs +51 -45
  6. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  7. package/__cjs/core/ObservableQuery.d.cts +16 -16
  8. package/__cjs/core/QueryInfo.cjs +27 -31
  9. package/__cjs/core/QueryInfo.cjs.map +1 -1
  10. package/__cjs/core/QueryInfo.d.cts +23 -6
  11. package/__cjs/core/QueryManager.cjs +66 -121
  12. package/__cjs/core/QueryManager.cjs.map +1 -1
  13. package/__cjs/core/QueryManager.d.cts +14 -8
  14. package/__cjs/react/hooks/useQuery.cjs +1 -1
  15. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  16. package/__cjs/react/hooks/useQuery.d.cts +2 -2
  17. package/__cjs/react/internal/cache/FragmentReference.cjs +4 -1
  18. package/__cjs/react/internal/cache/FragmentReference.cjs.map +1 -1
  19. package/__cjs/react/internal/cache/QueryReference.cjs +5 -18
  20. package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
  21. package/__cjs/react/internal/cache/QueryReference.d.cts +6 -43
  22. package/__cjs/react/query-preloader/createQueryPreloader.cjs +8 -2
  23. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  24. package/__cjs/react/query-preloader/createQueryPreloader.d.cts +38 -0
  25. package/__cjs/version.cjs +1 -1
  26. package/core/ApolloClient.d.ts +1 -1
  27. package/core/ApolloClient.js +2 -2
  28. package/core/ApolloClient.js.map +1 -1
  29. package/core/ObservableQuery.d.ts +16 -16
  30. package/core/ObservableQuery.js +51 -45
  31. package/core/ObservableQuery.js.map +1 -1
  32. package/core/QueryInfo.d.ts +23 -6
  33. package/core/QueryInfo.js +27 -31
  34. package/core/QueryInfo.js.map +1 -1
  35. package/core/QueryManager.d.ts +14 -8
  36. package/core/QueryManager.js +67 -122
  37. package/core/QueryManager.js.map +1 -1
  38. package/package.json +1 -1
  39. package/react/hooks/useQuery.d.ts +2 -2
  40. package/react/hooks/useQuery.js +2 -2
  41. package/react/hooks/useQuery.js.map +1 -1
  42. package/react/internal/cache/FragmentReference.js +4 -1
  43. package/react/internal/cache/FragmentReference.js.map +1 -1
  44. package/react/internal/cache/QueryReference.d.ts +6 -43
  45. package/react/internal/cache/QueryReference.js +5 -18
  46. package/react/internal/cache/QueryReference.js.map +1 -1
  47. package/react/query-preloader/createQueryPreloader.d.ts +38 -0
  48. package/react/query-preloader/createQueryPreloader.js +9 -3
  49. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  50. package/version.js +1 -1
@@ -7,13 +7,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.ObservableQuery = void 0;
8
8
  exports.logMissingFieldErrors = logMissingFieldErrors;
9
9
  const equality_1 = require("@wry/equality");
10
- const optimism_1 = require("optimism");
11
10
  const rxjs_1 = require("rxjs");
12
11
  const environment_1 = require("@apollo/client/utilities/environment");
13
12
  const internal_1 = require("@apollo/client/utilities/internal");
14
13
  const invariant_1 = require("@apollo/client/utilities/invariant");
15
14
  const equalByQuery_js_1 = require("./equalByQuery.cjs");
16
15
  const networkStatus_js_1 = require("./networkStatus.cjs");
16
+ const QueryInfo_js_1 = require("./QueryInfo.cjs");
17
17
  const { assign, hasOwnProperty } = Object;
18
18
  const uninitialized = {
19
19
  loading: true,
@@ -30,19 +30,14 @@ const empty = {
30
30
  partial: true,
31
31
  };
32
32
  class ObservableQuery {
33
+ options;
34
+ queryName;
33
35
  /**
34
- * @internal
35
- * A slot used by the `useQuery` hook to indicate that `client.watchQuery`
36
- * should not register the query immediately, but instead wait for the query to
37
- * be started registered with the `QueryManager` when `useSyncExternalStore`
38
- * actively subscribes to it.
36
+ * @internal will be read and written from `QueryInfo`
39
37
  *
40
38
  * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
41
39
  */
42
- static inactiveOnCreation = new optimism_1.Slot();
43
- options;
44
- queryId;
45
- queryName;
40
+ _lastWrite;
46
41
  // The `query` computed property will always reflect the document transformed
47
42
  // by the last run query. `this.options.query` will always reflect the raw
48
43
  // untransformed query to ensure document transforms with runtime conditionals
@@ -59,7 +54,6 @@ class ObservableQuery {
59
54
  unsubscribeFromCache;
60
55
  input;
61
56
  subject;
62
- observable;
63
57
  isTornDown;
64
58
  queryManager;
65
59
  subscriptions = new Set();
@@ -70,16 +64,12 @@ class ObservableQuery {
70
64
  */
71
65
  waitForNetworkResult;
72
66
  lastQuery;
73
- queryInfo;
74
67
  linkSubscription;
75
68
  pollingInfo;
76
69
  get networkStatus() {
77
70
  return this.subject.getValue().result.networkStatus;
78
71
  }
79
- constructor({ queryManager, queryInfo, options, }) {
80
- let startedInactive = ObservableQuery.inactiveOnCreation.getValue();
81
- // related classes
82
- this.queryInfo = queryInfo;
72
+ constructor({ queryManager, options, transformedQuery = queryManager.transform(options.query), }) {
83
73
  this.queryManager = queryManager;
84
74
  // active state
85
75
  this.waitForNetworkResult = options.fetchPolicy === "network-only";
@@ -90,7 +80,7 @@ class ObservableQuery {
90
80
  const { fetchPolicy = defaultFetchPolicy,
91
81
  // Make sure we don't store "standby" as the initialFetchPolicy.
92
82
  initialFetchPolicy = fetchPolicy === "standby" ? defaultFetchPolicy : (fetchPolicy), } = options;
93
- this.lastQuery = options.query;
83
+ this.lastQuery = transformedQuery;
94
84
  this.options = {
95
85
  ...options,
96
86
  // Remember the initial options.fetchPolicy so we can revert back to this
@@ -102,18 +92,23 @@ class ObservableQuery {
102
92
  fetchPolicy,
103
93
  variables: this.getVariablesWithDefaults(options.variables),
104
94
  };
95
+ this.initializeObservablesQueue();
96
+ this["@@observable"] = () => this;
97
+ if (Symbol.observable) {
98
+ this[Symbol.observable] = () => this;
99
+ }
100
+ const opDef = (0, internal_1.getOperationDefinition)(this.query);
101
+ this.queryName = opDef && opDef.name && opDef.name.value;
102
+ }
103
+ initializeObservablesQueue() {
105
104
  this.subject = new rxjs_1.BehaviorSubject({
106
105
  query: this.query,
107
106
  variables: this.variables,
108
107
  result: uninitialized,
109
108
  meta: {},
110
109
  });
111
- this.observable = this.subject.pipe((0, rxjs_1.tap)({
110
+ const observable = this.subject.pipe((0, rxjs_1.tap)({
112
111
  subscribe: () => {
113
- if (startedInactive) {
114
- queryManager["queries"].set(this.queryId, queryInfo);
115
- startedInactive = false;
116
- }
117
112
  if (!this.subject.observed) {
118
113
  this.reobserve();
119
114
  // TODO: See if we can rework updatePolling to better handle this.
@@ -165,20 +160,13 @@ class ObservableQuery {
165
160
  return current;
166
161
  }
167
162
  }, () => ({})));
168
- this["@@observable"] = () => this;
169
- if (Symbol.observable) {
170
- this[Symbol.observable] = () => this;
171
- }
172
- this.pipe = this.observable.pipe.bind(this.observable);
173
- this.subscribe = this.observable.subscribe.bind(this.observable);
163
+ this.pipe = observable.pipe.bind(observable);
164
+ this.subscribe = observable.subscribe.bind(observable);
174
165
  this.input = new rxjs_1.Subject();
175
166
  // we want to feed many streams into `this.subject`, but none of them should
176
167
  // be able to close `this.input`
177
168
  this.input.complete = () => { };
178
169
  this.input.pipe(this.operator).subscribe(this.subject);
179
- this.queryId = queryInfo.queryId || queryManager.generateQueryId();
180
- const opDef = (0, internal_1.getOperationDefinition)(this.query);
181
- this.queryName = opDef && opDef.name && opDef.name.value;
182
170
  }
183
171
  // We can't use Observable['subscribe'] here as the type as it conflicts with
184
172
  // the ability to infer T from Subscribable<T>. This limits the surface area
@@ -192,9 +180,6 @@ class ObservableQuery {
192
180
  *
193
181
  * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
194
182
  */
195
- resetDiff() {
196
- this.queryInfo.resetDiff();
197
- }
198
183
  getCacheDiff({ optimistic = true } = {}) {
199
184
  return this.queryManager.cache.diff({
200
185
  query: this.query,
@@ -377,7 +362,7 @@ class ObservableQuery {
377
362
  reobserveOptions.variables = this.options.variables =
378
363
  this.getVariablesWithDefaults({ ...this.variables, ...variables });
379
364
  }
380
- this.queryInfo.resetLastWrite();
365
+ this._lastWrite = undefined;
381
366
  return this._reobserve(reobserveOptions, {
382
367
  newNetworkStatus: networkStatus_js_1.NetworkStatus.refetch,
383
368
  });
@@ -405,7 +390,6 @@ class ObservableQuery {
405
390
  notifyOnNetworkStatusChange: this.options.notifyOnNetworkStatusChange,
406
391
  };
407
392
  combinedOptions.query = this.transformDocument(combinedOptions.query);
408
- const qid = this.queryManager.generateQueryId();
409
393
  // If a temporary query is passed to `fetchMore`, we don't want to store
410
394
  // it as the last query result since it may be an optimized query for
411
395
  // pagination. We will however run the transforms on the original document
@@ -428,9 +412,8 @@ class ObservableQuery {
428
412
  value: {},
429
413
  }, { shouldEmit: 3 /* EmitBehavior.networkStatusChange */ });
430
414
  return this.queryManager
431
- .fetchQuery(qid, combinedOptions, networkStatus_js_1.NetworkStatus.fetchMore)
415
+ .fetchQuery(combinedOptions, networkStatus_js_1.NetworkStatus.fetchMore)
432
416
  .then((fetchMoreResult) => {
433
- this.queryManager.removeQuery(qid);
434
417
  // disable the `fetchMore` override that is currently active
435
418
  // the next updates caused by this should not be `fetchMore` anymore,
436
419
  // but `ready` or whatever other calculated loading state is currently
@@ -692,8 +675,10 @@ class ObservableQuery {
692
675
  // TODO Make sure we update the networkStatus (and infer fetchVariables)
693
676
  // before actually committing to the fetch.
694
677
  const initialFetchPolicy = this.options.fetchPolicy;
695
- const queryInfo = this.queryManager.getOrCreateQuery(this.queryId);
696
- queryInfo.setObservableQuery(this);
678
+ const queryInfo = new QueryInfo_js_1.QueryInfo({
679
+ queryManager: this.queryManager,
680
+ observableQuery: this,
681
+ });
697
682
  options.context ??= {};
698
683
  let synchronouslyEmitted = false;
699
684
  const onCacheHit = () => {
@@ -714,7 +699,7 @@ class ObservableQuery {
714
699
  });
715
700
  }
716
701
  finally {
717
- if (!synchronouslyEmitted && this.activeOperations.has(operation)) {
702
+ if (!synchronouslyEmitted) {
718
703
  operation.override = networkStatus;
719
704
  this.input.next({
720
705
  kind: "N",
@@ -743,7 +728,9 @@ class ObservableQuery {
743
728
  // track query and variables from the start of the operation
744
729
  const { query, variables } = this;
745
730
  const operation = {
746
- abort: () => subscription.unsubscribe(),
731
+ abort: () => {
732
+ subscription.unsubscribe();
733
+ },
747
734
  query,
748
735
  variables,
749
736
  };
@@ -843,6 +830,7 @@ class ObservableQuery {
843
830
  _reobserve(newOptions, internalOptions) {
844
831
  this.isTornDown = false;
845
832
  let { newNetworkStatus } = internalOptions || {};
833
+ this.queryManager.obsQueries.add(this);
846
834
  const useDisposableObservable =
847
835
  // Refetching uses a disposable Observable to allow refetches using different
848
836
  // options, without permanently altering the options of the
@@ -940,7 +928,16 @@ class ObservableQuery {
940
928
  }
941
929
  this.linkSubscription = subscription;
942
930
  }
943
- const ret = Object.assign((0, internal_1.preventUnhandledRejection)(promise.then((result) => (0, internal_1.toQueryResult)(this.maskResult(result)))), {
931
+ const ret = Object.assign((0, internal_1.preventUnhandledRejection)(promise
932
+ .then((result) => (0, internal_1.toQueryResult)(this.maskResult(result)))
933
+ .finally(() => {
934
+ if (!this.hasObservers() && this.activeOperations.size === 0) {
935
+ // If `reobserve` was called on a query without any obervers,
936
+ // the teardown logic would never be called, so we need to
937
+ // call it here to ensure the query is properly torn down.
938
+ this.tearDownQuery();
939
+ }
940
+ })), {
944
941
  retain: () => {
945
942
  const subscription = observable.subscribe({});
946
943
  const unsubscribe = () => subscription.unsubscribe();
@@ -953,6 +950,14 @@ class ObservableQuery {
953
950
  hasObservers() {
954
951
  return this.subject.observed;
955
952
  }
953
+ /**
954
+ * Tears down the `ObservableQuery` and stops all active operations by sending a `complete` notification.
955
+ */
956
+ stop() {
957
+ this.subject.complete();
958
+ this.initializeObservablesQueue();
959
+ this.tearDownQuery();
960
+ }
956
961
  tearDownQuery() {
957
962
  if (this.isTornDown)
958
963
  return;
@@ -966,9 +971,10 @@ class ObservableQuery {
966
971
  // stop all active GraphQL subscriptions
967
972
  this.subscriptions.forEach((sub) => sub.unsubscribe());
968
973
  this.subscriptions.clear();
969
- this.queryManager.removeQuery(this.queryId);
974
+ this.queryManager.obsQueries.delete(this);
970
975
  this.isTornDown = true;
971
976
  this.abortActiveOperations();
977
+ this._lastWrite = undefined;
972
978
  }
973
979
  transformDocument(document) {
974
980
  return this.queryManager.transform(document);
@@ -981,7 +987,7 @@ class ObservableQuery {
981
987
  document: this.query,
982
988
  data: result.data,
983
989
  fetchPolicy: this.options.fetchPolicy,
984
- id: this.queryId,
990
+ cause: this,
985
991
  }),
986
992
  }
987
993
  : result;