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

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 (90) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/__cjs/cache/core/cache.cjs +1 -1
  3. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  4. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  5. package/__cjs/cache/inmemory/policies.cjs +4 -4
  6. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  7. package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
  8. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  9. package/__cjs/core/ApolloClient.cjs +10 -10
  10. package/__cjs/core/ObservableQuery.cjs +15 -11
  11. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  12. package/__cjs/core/ObservableQuery.d.cts +1 -2
  13. package/__cjs/core/QueryInfo.cjs +223 -34
  14. package/__cjs/core/QueryInfo.cjs.map +1 -1
  15. package/__cjs/core/QueryInfo.d.cts +34 -24
  16. package/__cjs/core/QueryManager.cjs +75 -256
  17. package/__cjs/core/QueryManager.cjs.map +1 -1
  18. package/__cjs/core/QueryManager.d.cts +4 -36
  19. package/__cjs/core/networkStatus.cjs +7 -1
  20. package/__cjs/core/networkStatus.cjs.map +1 -1
  21. package/__cjs/core/networkStatus.d.cts +7 -1
  22. package/__cjs/invariantErrorCodes.cjs +64 -69
  23. package/__cjs/link/core/ApolloLink.cjs +2 -2
  24. package/__cjs/link/http/checkFetcher.cjs +1 -1
  25. package/__cjs/link/http/createHttpLink.cjs +1 -1
  26. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
  27. package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
  28. package/__cjs/link/persisted-queries/index.cjs +2 -2
  29. package/__cjs/link/utils/validateOperation.cjs +1 -1
  30. package/__cjs/local-state/LocalState.cjs +10 -10
  31. package/__cjs/masking/maskDefinition.cjs.map +1 -1
  32. package/__cjs/masking/maskFragment.cjs +0 -8
  33. package/__cjs/masking/maskFragment.cjs.map +1 -1
  34. package/__cjs/masking/maskOperation.cjs +0 -8
  35. package/__cjs/masking/maskOperation.cjs.map +1 -1
  36. package/__cjs/masking/utils.cjs +3 -11
  37. package/__cjs/masking/utils.cjs.map +1 -1
  38. package/__cjs/masking/utils.d.cts +0 -1
  39. package/__cjs/react/hooks/useLazyQuery.cjs +1 -2
  40. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  41. package/__cjs/react/hooks/useLazyQuery.d.cts +0 -8
  42. package/__cjs/utilities/index.cjs +1 -16
  43. package/__cjs/utilities/index.cjs.map +1 -1
  44. package/__cjs/utilities/index.d.cts +0 -13
  45. package/__cjs/version.cjs +1 -1
  46. package/cache/core/cache.js +1 -1
  47. package/cache/inmemory/entityStore.js +3 -3
  48. package/cache/inmemory/key-extractor.js +1 -1
  49. package/cache/inmemory/policies.js +4 -4
  50. package/cache/inmemory/policies.js.map +1 -1
  51. package/cache/inmemory/readFromStore.js +2 -2
  52. package/cache/inmemory/writeToStore.js +4 -4
  53. package/core/ApolloClient.js +10 -10
  54. package/core/ObservableQuery.d.ts +1 -2
  55. package/core/ObservableQuery.js +15 -11
  56. package/core/ObservableQuery.js.map +1 -1
  57. package/core/QueryInfo.d.ts +34 -24
  58. package/core/QueryInfo.js +221 -34
  59. package/core/QueryInfo.js.map +1 -1
  60. package/core/QueryManager.d.ts +4 -36
  61. package/core/QueryManager.js +77 -258
  62. package/core/QueryManager.js.map +1 -1
  63. package/core/networkStatus.d.ts +7 -1
  64. package/core/networkStatus.js +7 -1
  65. package/core/networkStatus.js.map +1 -1
  66. package/invariantErrorCodes.js +64 -69
  67. package/link/core/ApolloLink.js +2 -2
  68. package/link/http/checkFetcher.js +1 -1
  69. package/link/http/createHttpLink.js +1 -1
  70. package/link/http/parseAndCheckHttpResponse.js +1 -1
  71. package/link/http/serializeFetchParameter.js +1 -1
  72. package/link/persisted-queries/index.js +2 -2
  73. package/link/utils/validateOperation.js +1 -1
  74. package/local-state/LocalState.js +10 -10
  75. package/masking/maskDefinition.js.map +1 -1
  76. package/masking/maskFragment.js +0 -8
  77. package/masking/maskFragment.js.map +1 -1
  78. package/masking/maskOperation.js +0 -8
  79. package/masking/maskOperation.js.map +1 -1
  80. package/masking/utils.d.ts +0 -1
  81. package/masking/utils.js +3 -10
  82. package/masking/utils.js.map +1 -1
  83. package/package.json +1 -1
  84. package/react/hooks/useLazyQuery.d.ts +0 -8
  85. package/react/hooks/useLazyQuery.js +1 -2
  86. package/react/hooks/useLazyQuery.js.map +1 -1
  87. package/utilities/index.d.ts +0 -13
  88. package/utilities/index.js +0 -12
  89. package/utilities/index.js.map +1 -1
  90. package/version.js +1 -1
@@ -4,7 +4,6 @@ import { Observable } from "rxjs";
4
4
  import type { Cache, MissingFieldError } from "@apollo/client/cache";
5
5
  import type { MissingTree } from "@apollo/client/cache";
6
6
  import type { MaybeMasked, Unmasked } from "@apollo/client/masking";
7
- import type { LastWrite } from "./QueryInfo.cjs";
8
7
  import type { QueryManager } from "./QueryManager.cjs";
9
8
  import type { ApolloQueryResult, DefaultContext, OperationVariables, QueryResult, TypedDocumentNode } from "./types.cjs";
10
9
  import type { ErrorPolicy, FetchMoreQueryOptions, NextFetchPolicyContext, RefetchWritePolicy, SubscribeToMoreOptions, UpdateQueryMapFn, WatchQueryFetchPolicy, WatchQueryOptions } from "./watchQueryOptions.cjs";
@@ -177,7 +176,7 @@ export declare class ObservableQuery<TData = unknown, TVariables extends Operati
177
176
  *
178
177
  * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
179
178
  */
180
- _lastWrite?: LastWrite;
179
+ _lastWrite?: unknown;
181
180
  get query(): TypedDocumentNode<TData, TVariables>;
182
181
  /**
183
182
  * An object containing the variables that were provided for the query.
@@ -1,9 +1,14 @@
1
- "use strict";
1
+ "use strict";;
2
+ const {
3
+ __DEV__
4
+ } = require("@apollo/client/utilities/environment");
5
+
2
6
  Object.defineProperty(exports, "__esModule", { value: true });
3
7
  exports.QueryInfo = void 0;
4
- exports.shouldWriteResult = shouldWriteResult;
5
8
  const equality_1 = require("@wry/equality");
6
9
  const internal_1 = require("@apollo/client/utilities/internal");
10
+ const invariant_1 = require("@apollo/client/utilities/invariant");
11
+ const IGNORE = {};
7
12
  const destructiveMethodCounts = new WeakMap();
8
13
  function wrapDestructiveCacheMethod(cache, methodName) {
9
14
  const original = cache[methodName];
@@ -21,26 +26,28 @@ function wrapDestructiveCacheMethod(cache, methodName) {
21
26
  };
22
27
  }
23
28
  }
29
+ const queryInfoIds = new WeakMap();
24
30
  // A QueryInfo object represents a single network request, either initiated
25
31
  // from the QueryManager or from an ObservableQuery.
26
32
  // It will only ever be used for a single network call.
27
33
  // It is responsible for reporting results to the cache, merging and in a no-cache
28
34
  // scenario accumulating the response.
29
35
  class QueryInfo {
30
- document = null;
31
36
  // TODO remove soon - this should be able to be handled by cancelling old operations before starting new ones
32
37
  lastRequestId = 1;
33
- variables;
34
38
  cache;
35
- queryId;
36
- // TODO should be private
39
+ queryManager;
40
+ id;
37
41
  observableQuery;
38
- constructor({ queryManager, observableQuery, }) {
42
+ constructor(queryManager, observableQuery) {
39
43
  const cache = (this.cache = queryManager.cache);
40
- this.queryId = queryManager.generateQueryId();
44
+ const id = (queryInfoIds.get(queryManager) || 0) + 1;
45
+ queryInfoIds.set(queryManager, id);
46
+ this.id = id + "";
41
47
  this.observableQuery = observableQuery;
48
+ this.queryManager = queryManager;
42
49
  // Track how often cache.evict is called, since we want eviction to
43
- // override the feud-stopping logic in the markResult method, by
50
+ // override the feud-stopping logic in the markQueryResult method, by
44
51
  // causing shouldWrite to return true. Wrapping the cache.evict method
45
52
  // is a bit of a hack, but it saves us from having to make eviction
46
53
  // counting an official part of the ApolloCache API.
@@ -51,19 +58,6 @@ class QueryInfo {
51
58
  wrapDestructiveCacheMethod(cache, "reset");
52
59
  }
53
60
  }
54
- init(query) {
55
- this.document = query.document;
56
- this.variables = query.variables;
57
- return this;
58
- }
59
- getDiffOptions(variables = this.variables) {
60
- return {
61
- query: this.document,
62
- variables,
63
- returnPartialData: true,
64
- optimistic: true,
65
- };
66
- }
67
61
  /**
68
62
  * @internal
69
63
  * For feud-preventing behaviour, `lastWrite` should be shared by all `QueryInfo` instances of an `ObservableQuery`.
@@ -91,16 +85,18 @@ class QueryInfo {
91
85
  (0, equality_1.equal)(variables, lastWrite.variables) &&
92
86
  (0, equality_1.equal)(result.data, lastWrite.result.data));
93
87
  }
94
- resetDiff() {
95
- this.lastDiff = void 0;
96
- }
97
88
  lastDiff;
98
- markResult(result, document, options, cacheWriteBehavior) {
89
+ markQueryResult(result, { document: query, variables, errorPolicy, cacheWriteBehavior, }) {
90
+ const diffOptions = {
91
+ query,
92
+ variables,
93
+ returnPartialData: true,
94
+ optimistic: true,
95
+ };
99
96
  // Cancel the pending notify timeout (if it exists) to prevent extraneous network
100
97
  // requests. To allow future notify timeouts, diff and dirty are reset as well.
101
98
  this.observableQuery?.["resetNotifications"]();
102
99
  if (cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */) {
103
- const diffOptions = this.getDiffOptions(options.variables);
104
100
  const lastDiff = this.lastDiff && (0, equality_1.equal)(diffOptions, this.lastDiff.options) ?
105
101
  this.lastDiff.diff
106
102
  : { result: null, complete: false };
@@ -111,9 +107,9 @@ class QueryInfo {
111
107
  };
112
108
  }
113
109
  else {
114
- const lastDiff = this.cache.diff(this.getDiffOptions());
110
+ const lastDiff = this.cache.diff(diffOptions);
115
111
  handleIncrementalResult(result, lastDiff);
116
- if (shouldWriteResult(result, options.errorPolicy)) {
112
+ if (shouldWriteResult(result, errorPolicy)) {
117
113
  // Using a transaction here so we have a chance to read the result
118
114
  // back from the cache before the watch callback fires as a result
119
115
  // of writeQuery, so we can store the new diff quietly and ignore
@@ -129,16 +125,16 @@ class QueryInfo {
129
125
  }
130
126
  },
131
127
  update: (cache) => {
132
- if (this.shouldWrite(result, options.variables)) {
128
+ if (this.shouldWrite(result, variables)) {
133
129
  cache.writeQuery({
134
- query: document,
130
+ query,
135
131
  data: result.data,
136
- variables: options.variables,
132
+ variables,
137
133
  overwrite: cacheWriteBehavior === 1 /* CacheWriteBehavior.OVERWRITE */,
138
134
  });
139
135
  this.lastWrite = {
140
136
  result,
141
- variables: options.variables,
137
+ variables,
142
138
  dmCount: destructiveMethodCounts.get(this.cache),
143
139
  };
144
140
  }
@@ -184,7 +180,6 @@ class QueryInfo {
184
180
  // If the previous this.diff was incomplete, fall through to
185
181
  // re-reading the latest data with cache.diff, below.
186
182
  }
187
- const diffOptions = this.getDiffOptions(options.variables);
188
183
  const diff = cache.diff(diffOptions);
189
184
  // If we're allowed to write to the cache, and we can read a
190
185
  // complete result from the cache, update result.data to be the
@@ -202,6 +197,200 @@ class QueryInfo {
202
197
  }
203
198
  }
204
199
  }
200
+ markMutationResult(result, mutation, cache = this.cache) {
201
+ const cacheWrites = [];
202
+ const skipCache = mutation.cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */;
203
+ if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
204
+ if (!(0, internal_1.isExecutionPatchIncrementalResult)(result)) {
205
+ cacheWrites.push({
206
+ result: result.data,
207
+ dataId: "ROOT_MUTATION",
208
+ query: mutation.document,
209
+ variables: mutation.variables,
210
+ });
211
+ }
212
+ if ((0, internal_1.isExecutionPatchIncrementalResult)(result) &&
213
+ (0, internal_1.isNonEmptyArray)(result.incremental)) {
214
+ const diff = cache.diff({
215
+ id: "ROOT_MUTATION",
216
+ // The cache complains if passed a mutation where it expects a
217
+ // query, so we transform mutations and subscriptions to queries
218
+ // (only once, thanks to this.transformCache).
219
+ query: this.queryManager.getDocumentInfo(mutation.document).asQuery,
220
+ variables: mutation.variables,
221
+ optimistic: false,
222
+ returnPartialData: true,
223
+ });
224
+ let mergedData;
225
+ if (diff.result) {
226
+ mergedData = (0, internal_1.mergeIncrementalData)(diff.result, result);
227
+ }
228
+ if (typeof mergedData !== "undefined") {
229
+ // cast the ExecutionPatchResult to FetchResult here since
230
+ // ExecutionPatchResult never has `data` when returned from the server
231
+ result.data = mergedData;
232
+ cacheWrites.push({
233
+ result: mergedData,
234
+ dataId: "ROOT_MUTATION",
235
+ query: mutation.document,
236
+ variables: mutation.variables,
237
+ });
238
+ }
239
+ }
240
+ const { updateQueries } = mutation;
241
+ if (updateQueries) {
242
+ this.queryManager
243
+ .getObservableQueries("all")
244
+ .forEach((observableQuery) => {
245
+ const queryName = observableQuery && observableQuery.queryName;
246
+ if (!queryName ||
247
+ !Object.hasOwnProperty.call(updateQueries, queryName)) {
248
+ return;
249
+ }
250
+ const updater = updateQueries[queryName];
251
+ const { query: document, variables } = observableQuery;
252
+ // Read the current query result from the store.
253
+ const { result: currentQueryResult, complete } = observableQuery.getCacheDiff({ optimistic: false });
254
+ if (complete && currentQueryResult) {
255
+ // Run our reducer using the current query result and the mutation result.
256
+ const nextQueryResult = updater(currentQueryResult, {
257
+ mutationResult: result,
258
+ queryName: (document && (0, internal_1.getOperationName)(document)) || void 0,
259
+ queryVariables: variables,
260
+ });
261
+ // Write the modified result back into the store if we got a new result.
262
+ if (nextQueryResult) {
263
+ cacheWrites.push({
264
+ result: nextQueryResult,
265
+ dataId: "ROOT_QUERY",
266
+ query: document,
267
+ variables,
268
+ });
269
+ }
270
+ }
271
+ });
272
+ }
273
+ }
274
+ if (cacheWrites.length > 0 ||
275
+ (mutation.refetchQueries || "").length > 0 ||
276
+ mutation.update ||
277
+ mutation.onQueryUpdated ||
278
+ mutation.removeOptimistic) {
279
+ const results = [];
280
+ this.queryManager
281
+ .refetchQueries({
282
+ updateCache: (cache) => {
283
+ if (!skipCache) {
284
+ cacheWrites.forEach((write) => cache.write(write));
285
+ }
286
+ // If the mutation has some writes associated with it then we need to
287
+ // apply those writes to the store by running this reducer again with
288
+ // a write action.
289
+ const { update } = mutation;
290
+ // Determine whether result is a SingleExecutionResult,
291
+ // or the final ExecutionPatchResult.
292
+ const isFinalResult = !(0, internal_1.isExecutionPatchResult)(result) ||
293
+ ((0, internal_1.isExecutionPatchIncrementalResult)(result) && !result.hasNext);
294
+ if (update) {
295
+ if (!skipCache) {
296
+ // Re-read the ROOT_MUTATION data we just wrote into the cache
297
+ // (the first cache.write call in the cacheWrites.forEach loop
298
+ // above), so field read functions have a chance to run for
299
+ // fields within mutation result objects.
300
+ const diff = cache.diff({
301
+ id: "ROOT_MUTATION",
302
+ // The cache complains if passed a mutation where it expects a
303
+ // query, so we transform mutations and subscriptions to queries
304
+ // (only once, thanks to this.transformCache).
305
+ query: this.queryManager.getDocumentInfo(mutation.document)
306
+ .asQuery,
307
+ variables: mutation.variables,
308
+ optimistic: false,
309
+ returnPartialData: true,
310
+ });
311
+ if (diff.complete) {
312
+ result = { ...result, data: diff.result };
313
+ if ("incremental" in result) {
314
+ delete result.incremental;
315
+ }
316
+ if ("hasNext" in result) {
317
+ delete result.hasNext;
318
+ }
319
+ }
320
+ }
321
+ // If we've received the whole response,
322
+ // either a SingleExecutionResult or the final ExecutionPatchResult,
323
+ // call the update function.
324
+ if (isFinalResult) {
325
+ update(cache, result, {
326
+ context: mutation.context,
327
+ variables: mutation.variables,
328
+ });
329
+ }
330
+ }
331
+ // TODO Do this with cache.evict({ id: 'ROOT_MUTATION' }) but make it
332
+ // shallow to allow rolling back optimistic evictions.
333
+ if (!skipCache && !mutation.keepRootFields && isFinalResult) {
334
+ cache.modify({
335
+ id: "ROOT_MUTATION",
336
+ fields(value, { fieldName, DELETE }) {
337
+ return fieldName === "__typename" ? value : DELETE;
338
+ },
339
+ });
340
+ }
341
+ },
342
+ include: mutation.refetchQueries,
343
+ // Write the final mutation.result to the root layer of the cache.
344
+ optimistic: false,
345
+ // Remove the corresponding optimistic layer at the same time as we
346
+ // write the final non-optimistic result.
347
+ removeOptimistic: mutation.removeOptimistic,
348
+ // Let the caller of client.mutate optionally determine the refetching
349
+ // behavior for watched queries after the mutation.update function runs.
350
+ // If no onQueryUpdated function was provided for this mutation, pass
351
+ // null instead of undefined to disable the default refetching behavior.
352
+ onQueryUpdated: mutation.onQueryUpdated || null,
353
+ })
354
+ .forEach((result) => results.push(result));
355
+ if (mutation.awaitRefetchQueries || mutation.onQueryUpdated) {
356
+ // Returning a promise here makes the mutation await that promise, so we
357
+ // include results in that promise's work if awaitRefetchQueries or an
358
+ // onQueryUpdated function was specified.
359
+ return Promise.all(results).then(() => result);
360
+ }
361
+ }
362
+ return Promise.resolve(result);
363
+ }
364
+ markMutationOptimistic(optimisticResponse, mutation) {
365
+ const data = typeof optimisticResponse === "function" ?
366
+ optimisticResponse(mutation.variables, { IGNORE })
367
+ : optimisticResponse;
368
+ if (data === IGNORE) {
369
+ return false;
370
+ }
371
+ this.cache.recordOptimisticTransaction((cache) => {
372
+ try {
373
+ this.markMutationResult({ data }, mutation, cache);
374
+ }
375
+ catch (error) {
376
+ __DEV__ && invariant_1.invariant.error(error);
377
+ }
378
+ }, this.id);
379
+ return true;
380
+ }
381
+ markSubscriptionResult(result, { document, variables, errorPolicy, cacheWriteBehavior, }) {
382
+ if (cacheWriteBehavior !== 0 /* CacheWriteBehavior.FORBID */) {
383
+ if (shouldWriteResult(result, errorPolicy)) {
384
+ this.cache.write({
385
+ query: document,
386
+ result: result.data,
387
+ dataId: "ROOT_SUBSCRIPTION",
388
+ variables: variables,
389
+ });
390
+ }
391
+ this.queryManager.broadcastQueries();
392
+ }
393
+ }
205
394
  }
206
395
  exports.QueryInfo = QueryInfo;
207
396
  function handleIncrementalResult(result, lastDiff) {