@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
@@ -9,13 +9,11 @@ import { execute } from "@apollo/client/link";
9
9
  import { maskFragment, maskOperation } from "@apollo/client/masking";
10
10
  import { cacheSizes, DocumentTransform, print } from "@apollo/client/utilities";
11
11
  import { __DEV__ } from "@apollo/client/utilities/environment";
12
- import { AutoCleanedWeakCache, checkDocument, filterMap, getDefaultValues, getGraphQLErrorsFromResult, getOperationDefinition, getOperationName, graphQLResultHasError, hasDirectives, hasForcedResolvers, isDocumentNode, isExecutionPatchIncrementalResult, isExecutionPatchResult, isNonEmptyArray, isNonNullObject, makeUniqueId, mergeIncrementalData, removeDirectivesFromDocument, toQueryResult, } from "@apollo/client/utilities/internal";
12
+ import { AutoCleanedWeakCache, checkDocument, filterMap, getDefaultValues, getGraphQLErrorsFromResult, getOperationDefinition, getOperationName, graphQLResultHasError, hasDirectives, hasForcedResolvers, isDocumentNode, isExecutionPatchResult, isNonNullObject, makeUniqueId, removeDirectivesFromDocument, toQueryResult, } from "@apollo/client/utilities/internal";
13
13
  import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant";
14
14
  import { isNetworkRequestInFlight, NetworkStatus } from "./networkStatus.js";
15
15
  import { logMissingFieldErrors, ObservableQuery } from "./ObservableQuery.js";
16
- import { QueryInfo, shouldWriteResult, } from "./QueryInfo.js";
17
- const { hasOwnProperty } = Object.prototype;
18
- const IGNORE = {};
16
+ import { QueryInfo } from "./QueryInfo.js";
19
17
  export class QueryManager {
20
18
  defaultOptions;
21
19
  client;
@@ -44,7 +42,7 @@ export class QueryManager {
44
42
  * All ObservableQueries that currently have at least one subscriber.
45
43
  */
46
44
  obsQueries = new Set();
47
- // Maps from queryId strings to Promise rejection functions for
45
+ // Maps from queryInfo.id strings to Promise rejection functions for
48
46
  // currently active queries and fetches.
49
47
  // Use protected instead of private field so
50
48
  // @apollo/experimental-nextjs-app-support can access type info.
@@ -89,23 +87,23 @@ export class QueryManager {
89
87
  */
90
88
  stop() {
91
89
  this.obsQueries.forEach((oq) => oq.stop());
92
- this.cancelPendingFetches(newInvariantError(82));
90
+ this.cancelPendingFetches(newInvariantError(81));
93
91
  }
94
92
  cancelPendingFetches(error) {
95
93
  this.fetchCancelFns.forEach((cancel) => cancel(error));
96
94
  this.fetchCancelFns.clear();
97
95
  }
98
96
  async mutate({ mutation, variables, optimisticResponse, updateQueries, refetchQueries = [], awaitRefetchQueries = false, update: updateWithProxyFn, onQueryUpdated, fetchPolicy = this.defaultOptions.mutate?.fetchPolicy || "network-only", errorPolicy = this.defaultOptions.mutate?.errorPolicy || "none", keepRootFields, context, }) {
99
- invariant(mutation, 83);
97
+ invariant(mutation, 82);
100
98
  checkDocument(mutation, OperationTypeNode.MUTATION);
101
- invariant(fetchPolicy === "network-only" || fetchPolicy === "no-cache", 84);
102
- const mutationId = this.generateMutationId();
99
+ invariant(fetchPolicy === "network-only" || fetchPolicy === "no-cache", 83);
100
+ const queryInfo = new QueryInfo(this);
103
101
  mutation = this.cache.transformForLink(this.transform(mutation));
104
102
  const { hasClientExports } = this.getDocumentInfo(mutation);
105
103
  variables = this.getVariables(mutation, variables);
106
104
  if (hasClientExports) {
107
105
  if (__DEV__) {
108
- invariant(this.localState, 85, getOperationName(mutation, "(anonymous)"));
106
+ invariant(this.localState, 84, getOperationName(mutation, "(anonymous)"));
109
107
  }
110
108
  variables = await this.localState.getExportedVariables({
111
109
  client: this.client,
@@ -115,18 +113,19 @@ export class QueryManager {
115
113
  });
116
114
  }
117
115
  const mutationStoreValue = this.mutationStore &&
118
- (this.mutationStore[mutationId] = {
116
+ (this.mutationStore[queryInfo.id] = {
119
117
  mutation,
120
118
  variables,
121
119
  loading: true,
122
120
  error: null,
123
121
  });
124
122
  const isOptimistic = optimisticResponse &&
125
- this.markMutationOptimistic(optimisticResponse, {
126
- mutationId,
123
+ queryInfo.markMutationOptimistic(optimisticResponse, {
127
124
  document: mutation,
128
125
  variables,
129
- fetchPolicy,
126
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
127
+ 0 /* CacheWriteBehavior.FORBID */
128
+ : 2 /* CacheWriteBehavior.MERGE */,
130
129
  errorPolicy,
131
130
  context,
132
131
  updateQueries,
@@ -156,19 +155,19 @@ export class QueryManager {
156
155
  if (errorPolicy === "ignore" && hasErrors) {
157
156
  delete storeResult.errors;
158
157
  }
159
- return from(this.markMutationResult({
160
- mutationId,
161
- result: storeResult,
158
+ return from(queryInfo.markMutationResult(storeResult, {
162
159
  document: mutation,
163
160
  variables,
164
- fetchPolicy,
161
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
162
+ 0 /* CacheWriteBehavior.FORBID */
163
+ : 2 /* CacheWriteBehavior.MERGE */,
165
164
  errorPolicy,
166
165
  context,
167
166
  update: updateWithProxyFn,
168
167
  updateQueries,
169
168
  awaitRefetchQueries,
170
169
  refetchQueries,
171
- removeOptimistic: isOptimistic ? mutationId : void 0,
170
+ removeOptimistic: isOptimistic ? queryInfo.id : void 0,
172
171
  onQueryUpdated,
173
172
  keepRootFields,
174
173
  }));
@@ -205,7 +204,7 @@ export class QueryManager {
205
204
  mutationStoreValue.error = error;
206
205
  }
207
206
  if (isOptimistic) {
208
- this.cache.removeOptimistic(mutationId);
207
+ this.cache.removeOptimistic(queryInfo.id);
209
208
  }
210
209
  this.broadcastQueries();
211
210
  if (errorPolicy === "ignore") {
@@ -219,190 +218,8 @@ export class QueryManager {
219
218
  });
220
219
  });
221
220
  }
222
- markMutationResult(mutation, cache = this.cache) {
223
- let { result } = mutation;
224
- const cacheWrites = [];
225
- const skipCache = mutation.fetchPolicy === "no-cache";
226
- if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
227
- if (!isExecutionPatchIncrementalResult(result)) {
228
- cacheWrites.push({
229
- result: result.data,
230
- dataId: "ROOT_MUTATION",
231
- query: mutation.document,
232
- variables: mutation.variables,
233
- });
234
- }
235
- if (isExecutionPatchIncrementalResult(result) &&
236
- isNonEmptyArray(result.incremental)) {
237
- const diff = cache.diff({
238
- id: "ROOT_MUTATION",
239
- // The cache complains if passed a mutation where it expects a
240
- // query, so we transform mutations and subscriptions to queries
241
- // (only once, thanks to this.transformCache).
242
- query: this.getDocumentInfo(mutation.document).asQuery,
243
- variables: mutation.variables,
244
- optimistic: false,
245
- returnPartialData: true,
246
- });
247
- let mergedData;
248
- if (diff.result) {
249
- mergedData = mergeIncrementalData(diff.result, result);
250
- }
251
- if (typeof mergedData !== "undefined") {
252
- // cast the ExecutionPatchResult to FetchResult here since
253
- // ExecutionPatchResult never has `data` when returned from the server
254
- result.data = mergedData;
255
- cacheWrites.push({
256
- result: mergedData,
257
- dataId: "ROOT_MUTATION",
258
- query: mutation.document,
259
- variables: mutation.variables,
260
- });
261
- }
262
- }
263
- const { updateQueries } = mutation;
264
- if (updateQueries) {
265
- this.obsQueries.forEach((observableQuery) => {
266
- const queryName = observableQuery && observableQuery.queryName;
267
- if (!queryName || !hasOwnProperty.call(updateQueries, queryName)) {
268
- return;
269
- }
270
- const updater = updateQueries[queryName];
271
- const { query: document, variables } = observableQuery;
272
- // Read the current query result from the store.
273
- const { result: currentQueryResult, complete } = observableQuery.getCacheDiff({ optimistic: false });
274
- if (complete && currentQueryResult) {
275
- // Run our reducer using the current query result and the mutation result.
276
- const nextQueryResult = updater(currentQueryResult, {
277
- mutationResult: result,
278
- queryName: (document && getOperationName(document)) || void 0,
279
- queryVariables: variables,
280
- });
281
- // Write the modified result back into the store if we got a new result.
282
- if (nextQueryResult) {
283
- cacheWrites.push({
284
- result: nextQueryResult,
285
- dataId: "ROOT_QUERY",
286
- query: document,
287
- variables,
288
- });
289
- }
290
- }
291
- });
292
- }
293
- }
294
- if (cacheWrites.length > 0 ||
295
- (mutation.refetchQueries || "").length > 0 ||
296
- mutation.update ||
297
- mutation.onQueryUpdated ||
298
- mutation.removeOptimistic) {
299
- const results = [];
300
- this.refetchQueries({
301
- updateCache: (cache) => {
302
- if (!skipCache) {
303
- cacheWrites.forEach((write) => cache.write(write));
304
- }
305
- // If the mutation has some writes associated with it then we need to
306
- // apply those writes to the store by running this reducer again with
307
- // a write action.
308
- const { update } = mutation;
309
- // Determine whether result is a SingleExecutionResult,
310
- // or the final ExecutionPatchResult.
311
- const isFinalResult = !isExecutionPatchResult(result) ||
312
- (isExecutionPatchIncrementalResult(result) && !result.hasNext);
313
- if (update) {
314
- if (!skipCache) {
315
- // Re-read the ROOT_MUTATION data we just wrote into the cache
316
- // (the first cache.write call in the cacheWrites.forEach loop
317
- // above), so field read functions have a chance to run for
318
- // fields within mutation result objects.
319
- const diff = cache.diff({
320
- id: "ROOT_MUTATION",
321
- // The cache complains if passed a mutation where it expects a
322
- // query, so we transform mutations and subscriptions to queries
323
- // (only once, thanks to this.transformCache).
324
- query: this.getDocumentInfo(mutation.document).asQuery,
325
- variables: mutation.variables,
326
- optimistic: false,
327
- returnPartialData: true,
328
- });
329
- if (diff.complete) {
330
- result = { ...result, data: diff.result };
331
- if ("incremental" in result) {
332
- delete result.incremental;
333
- }
334
- if ("hasNext" in result) {
335
- delete result.hasNext;
336
- }
337
- }
338
- }
339
- // If we've received the whole response,
340
- // either a SingleExecutionResult or the final ExecutionPatchResult,
341
- // call the update function.
342
- if (isFinalResult) {
343
- update(cache, result, {
344
- context: mutation.context,
345
- variables: mutation.variables,
346
- });
347
- }
348
- }
349
- // TODO Do this with cache.evict({ id: 'ROOT_MUTATION' }) but make it
350
- // shallow to allow rolling back optimistic evictions.
351
- if (!skipCache && !mutation.keepRootFields && isFinalResult) {
352
- cache.modify({
353
- id: "ROOT_MUTATION",
354
- fields(value, { fieldName, DELETE }) {
355
- return fieldName === "__typename" ? value : DELETE;
356
- },
357
- });
358
- }
359
- },
360
- include: mutation.refetchQueries,
361
- // Write the final mutation.result to the root layer of the cache.
362
- optimistic: false,
363
- // Remove the corresponding optimistic layer at the same time as we
364
- // write the final non-optimistic result.
365
- removeOptimistic: mutation.removeOptimistic,
366
- // Let the caller of client.mutate optionally determine the refetching
367
- // behavior for watched queries after the mutation.update function runs.
368
- // If no onQueryUpdated function was provided for this mutation, pass
369
- // null instead of undefined to disable the default refetching behavior.
370
- onQueryUpdated: mutation.onQueryUpdated || null,
371
- }).forEach((result) => results.push(result));
372
- if (mutation.awaitRefetchQueries || mutation.onQueryUpdated) {
373
- // Returning a promise here makes the mutation await that promise, so we
374
- // include results in that promise's work if awaitRefetchQueries or an
375
- // onQueryUpdated function was specified.
376
- return Promise.all(results).then(() => result);
377
- }
378
- }
379
- return Promise.resolve(result);
380
- }
381
- markMutationOptimistic(optimisticResponse, mutation) {
382
- const data = typeof optimisticResponse === "function" ?
383
- optimisticResponse(mutation.variables, { IGNORE })
384
- : optimisticResponse;
385
- if (data === IGNORE) {
386
- return false;
387
- }
388
- this.cache.recordOptimisticTransaction((cache) => {
389
- try {
390
- this.markMutationResult({
391
- ...mutation,
392
- result: { data },
393
- }, cache);
394
- }
395
- catch (error) {
396
- __DEV__ && invariant.error(error);
397
- }
398
- }, mutation.mutationId);
399
- return true;
400
- }
401
221
  fetchQuery(options, networkStatus) {
402
- const queryInfo = new QueryInfo({
403
- queryManager: this,
404
- });
405
- return lastValueFrom(this.fetchObservableWithInfo(queryInfo, options, {
222
+ return lastValueFrom(this.fetchObservableWithInfo(options, {
406
223
  networkStatus,
407
224
  }).observable.pipe(filterMap((value) => {
408
225
  switch (value.kind) {
@@ -506,18 +323,10 @@ export class QueryManager {
506
323
  }),
507
324
  }));
508
325
  }
509
- queryIdCounter = 1;
510
- generateQueryId() {
511
- return String(this.queryIdCounter++);
512
- }
513
326
  requestIdCounter = 1;
514
327
  generateRequestId() {
515
328
  return this.requestIdCounter++;
516
329
  }
517
- mutationIdCounter = 1;
518
- generateMutationId() {
519
- return String(this.mutationIdCounter++);
520
- }
521
330
  clearStore(options = {
522
331
  discardWatches: true,
523
332
  }) {
@@ -526,7 +335,7 @@ export class QueryManager {
526
335
  // depend on values that previously existed in the data portion of the
527
336
  // store. So, we cancel the promises and observers that we have issued
528
337
  // so far and not yet resolved (in the case of queries).
529
- this.cancelPendingFetches(newInvariantError(86));
338
+ this.cancelPendingFetches(newInvariantError(85));
530
339
  this.obsQueries.forEach((observableQuery) => {
531
340
  // Set loading to true so listeners don't trigger unless they want
532
341
  // results with partial data.
@@ -596,10 +405,10 @@ export class QueryManager {
596
405
  if (!included) {
597
406
  const queryName = queryNames.get(nameOrQueryString);
598
407
  if (queryName) {
599
- __DEV__ && invariant.warn(87, queryName);
408
+ __DEV__ && invariant.warn(86, queryName);
600
409
  }
601
410
  else {
602
- __DEV__ && invariant.warn(88);
411
+ __DEV__ && invariant.warn(87);
603
412
  }
604
413
  }
605
414
  });
@@ -628,7 +437,7 @@ export class QueryManager {
628
437
  if (__DEV__) {
629
438
  invariant(
630
439
  !this.getDocumentInfo(query).hasClientExports || this.localState,
631
- 89,
440
+ 88,
632
441
  getOperationName(query, "(anonymous)")
633
442
  );
634
443
  }
@@ -641,21 +450,17 @@ export class QueryManager {
641
450
  }))
642
451
  : of(variables)).pipe(mergeMap((variables) => {
643
452
  const { observable, restart: res } = this.getObservableFromLink(query, context, variables, extensions);
453
+ const queryInfo = new QueryInfo(this);
644
454
  restart = res;
645
455
  return observable.pipe(map((rawResult) => {
646
- if (fetchPolicy !== "no-cache") {
647
- // the subscription interface should handle not sending us results we no longer subscribe to.
648
- // XXX I don't think we ever send in an object with errors, but we might in the future...
649
- if (shouldWriteResult(rawResult, errorPolicy)) {
650
- this.cache.write({
651
- query,
652
- result: rawResult.data,
653
- dataId: "ROOT_SUBSCRIPTION",
654
- variables: variables,
655
- });
656
- }
657
- this.broadcastQueries();
658
- }
456
+ queryInfo.markSubscriptionResult(rawResult, {
457
+ document: query,
458
+ variables,
459
+ errorPolicy,
460
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
461
+ 0 /* CacheWriteBehavior.FORBID */
462
+ : 2 /* CacheWriteBehavior.MERGE */,
463
+ });
659
464
  const result = {
660
465
  data: rawResult.data ?? undefined,
661
466
  };
@@ -771,7 +576,7 @@ export class QueryManager {
771
576
  const { operation } = getOperationDefinition(query);
772
577
  invariant(
773
578
  this.localState,
774
- 90,
579
+ 89,
775
580
  operation[0].toUpperCase() + operation.slice(1),
776
581
  operationName ?? "(anonymous)"
777
582
  );
@@ -795,7 +600,7 @@ export class QueryManager {
795
600
  })),
796
601
  };
797
602
  }
798
- getResultsFromLink(queryInfo, cacheWriteBehavior, options) {
603
+ getResultsFromLink(options, { queryInfo, cacheWriteBehavior, observableQuery, }) {
799
604
  const requestId = (queryInfo.lastRequestId = this.generateRequestId());
800
605
  const { errorPolicy } = options;
801
606
  // Performing transformForLink here gives this.cache a chance to fill in
@@ -810,26 +615,35 @@ export class QueryManager {
810
615
  if (requestId >= queryInfo.lastRequestId) {
811
616
  if (hasErrors && errorPolicy === "none") {
812
617
  queryInfo.resetLastWrite();
813
- queryInfo.observableQuery?.["resetNotifications"]();
618
+ observableQuery?.["resetNotifications"]();
814
619
  // Throwing here effectively calls observer.error.
815
620
  throw new CombinedGraphQLErrors(result);
816
621
  }
817
622
  // Use linkDocument rather than queryInfo.document so the
818
623
  // operation/fragments used to write the result are the same as the
819
624
  // ones used to obtain it from the link.
820
- queryInfo.markResult(result, linkDocument, options, cacheWriteBehavior);
625
+ queryInfo.markQueryResult(result, {
626
+ ...options,
627
+ document: linkDocument,
628
+ cacheWriteBehavior,
629
+ });
821
630
  }
822
631
  const aqr = {
823
632
  data: result.data,
824
- dataState: result.data ? "complete" : "empty",
825
- loading: false,
826
- networkStatus: NetworkStatus.ready,
827
- partial: !result.data,
633
+ ...(isExecutionPatchResult(result) && result.hasNext ?
634
+ {
635
+ loading: true,
636
+ networkStatus: NetworkStatus.streaming,
637
+ dataState: "streaming",
638
+ partial: true,
639
+ }
640
+ : {
641
+ dataState: result.data ? "complete" : "empty",
642
+ loading: false,
643
+ networkStatus: NetworkStatus.ready,
644
+ partial: !result.data,
645
+ }),
828
646
  };
829
- if (isExecutionPatchResult(result) && result.hasNext) {
830
- aqr.dataState = "streaming";
831
- aqr.partial = true;
832
- }
833
647
  // In the case we start multiple network requests simulatenously, we
834
648
  // want to ensure we properly set `data` if we're reporting on an old
835
649
  // result which will not be caught by the conditional above that ends up
@@ -850,7 +664,7 @@ export class QueryManager {
850
664
  // Avoid storing errors from older interrupted queries.
851
665
  if (requestId >= queryInfo.lastRequestId && errorPolicy === "none") {
852
666
  queryInfo.resetLastWrite();
853
- queryInfo.observableQuery?.["resetNotifications"]();
667
+ observableQuery?.["resetNotifications"]();
854
668
  throw error;
855
669
  }
856
670
  const aqr = {
@@ -867,11 +681,11 @@ export class QueryManager {
867
681
  return of(aqr);
868
682
  }));
869
683
  }
870
- fetchObservableWithInfo(queryInfo, options, {
684
+ fetchObservableWithInfo(options, {
871
685
  // The initial networkStatus for this fetch, most often
872
686
  // NetworkStatus.loading, but also possibly fetchMore, poll, refetch,
873
687
  // or setVariables.
874
- networkStatus = NetworkStatus.loading, query = options.query, fetchQueryOperator = (x) => x, onCacheHit = () => { }, }) {
688
+ networkStatus = NetworkStatus.loading, query = options.query, fetchQueryOperator = (x) => x, onCacheHit = () => { }, observableQuery, }) {
875
689
  const variables = this.getVariables(query, options.variables);
876
690
  const defaults = this.defaultOptions.watchQuery;
877
691
  let { fetchPolicy = (defaults && defaults.fetchPolicy) || "cache-first", errorPolicy = (defaults && defaults.errorPolicy) || "none", returnPartialData = false, notifyOnNetworkStatusChange = true, context = {}, } = options;
@@ -888,6 +702,7 @@ export class QueryManager {
888
702
  notifyOnNetworkStatusChange,
889
703
  context,
890
704
  });
705
+ const queryInfo = new QueryInfo(this, observableQuery);
891
706
  const fromVariables = (variables) => {
892
707
  // Since normalized is always a fresh copy of options, it's safe to
893
708
  // modify its properties here, rather than creating yet another new
@@ -900,24 +715,23 @@ export class QueryManager {
900
715
  normalized.refetchWritePolicy !== "merge") ?
901
716
  1 /* CacheWriteBehavior.OVERWRITE */
902
717
  : 2 /* CacheWriteBehavior.MERGE */;
903
- const observableWithInfo = this.fetchQueryByPolicy(queryInfo, normalized, cacheWriteBehavior, onCacheHit);
718
+ const observableWithInfo = this.fetchQueryByPolicy(normalized, { queryInfo, cacheWriteBehavior, onCacheHit, observableQuery });
904
719
  observableWithInfo.observable =
905
720
  observableWithInfo.observable.pipe(fetchQueryOperator);
906
721
  if (
907
722
  // If we're in standby, postpone advancing options.fetchPolicy using
908
723
  // applyNextFetchPolicy.
909
- normalized.fetchPolicy !== "standby" &&
910
- queryInfo.observableQuery) {
911
- queryInfo.observableQuery["applyNextFetchPolicy"]("after-fetch", options);
724
+ normalized.fetchPolicy !== "standby") {
725
+ observableQuery?.["applyNextFetchPolicy"]("after-fetch", options);
912
726
  }
913
727
  return observableWithInfo;
914
728
  };
915
729
  // This cancel function needs to be set before the concast is created,
916
730
  // in case concast creation synchronously cancels the request.
917
731
  const cleanupCancelFn = () => {
918
- this.fetchCancelFns.delete(queryInfo.queryId);
732
+ this.fetchCancelFns.delete(queryInfo.id);
919
733
  };
920
- this.fetchCancelFns.set(queryInfo.queryId, (error) => {
734
+ this.fetchCancelFns.set(queryInfo.id, (error) => {
921
735
  fetchCancelSubject.next({
922
736
  kind: "E",
923
737
  error,
@@ -935,7 +749,7 @@ export class QueryManager {
935
749
  // we deprecated and removed LocalState.
936
750
  if (this.getDocumentInfo(normalized.query).hasClientExports) {
937
751
  if (__DEV__) {
938
- invariant(this.localState, 91, getOperationName(normalized.query, "(anonymous)"));
752
+ invariant(this.localState, 90, getOperationName(normalized.query, "(anonymous)"));
939
753
  }
940
754
  observable = from(this.localState.getExportedVariables({
941
755
  client: this.client,
@@ -1104,7 +918,7 @@ export class QueryManager {
1104
918
  !isFullyUnmaskedOperation(document) &&
1105
919
  !this.noCacheWarningsByCause.has(cause)) {
1106
920
  this.noCacheWarningsByCause.add(cause);
1107
- __DEV__ && invariant.warn(92, getOperationName(document, `Unnamed ${operationType ?? "operation"}`));
921
+ __DEV__ && invariant.warn(91, getOperationName(document, `Unnamed ${operationType ?? "operation"}`));
1108
922
  }
1109
923
  }
1110
924
  return (this.dataMasking ?
@@ -1117,12 +931,13 @@ export class QueryManager {
1117
931
  maskFragment(data, fragment, this.cache, fragmentName)
1118
932
  : data;
1119
933
  }
1120
- fetchQueryByPolicy(queryInfo, { query, variables, fetchPolicy, errorPolicy, returnPartialData, context, }, cacheWriteBehavior, onCacheHit) {
1121
- queryInfo.init({
1122
- document: query,
934
+ fetchQueryByPolicy({ query, variables, fetchPolicy, errorPolicy, returnPartialData, context, }, { cacheWriteBehavior, onCacheHit, queryInfo, observableQuery, }) {
935
+ const readCache = () => this.cache.diff({
936
+ query,
1123
937
  variables,
938
+ returnPartialData: true,
939
+ optimistic: true,
1124
940
  });
1125
- const readCache = () => this.cache.diff(queryInfo.getDiffOptions());
1126
941
  const resultsFromCache = (diff, networkStatus) => {
1127
942
  const data = diff.result;
1128
943
  if (__DEV__ && !returnPartialData && data !== null) {
@@ -1161,7 +976,7 @@ export class QueryManager {
1161
976
  (diff.complete || returnPartialData) &&
1162
977
  this.getDocumentInfo(query).hasForcedResolvers) {
1163
978
  if (__DEV__) {
1164
- invariant(this.localState, 93, getOperationName(query, "(anonymous)"));
979
+ invariant(this.localState, 92, getOperationName(query, "(anonymous)"));
1165
980
  }
1166
981
  onCacheHit();
1167
982
  return from(this.localState.execute({
@@ -1189,12 +1004,16 @@ export class QueryManager {
1189
1004
  }
1190
1005
  return fromData(data || undefined);
1191
1006
  };
1192
- const resultsFromLink = () => this.getResultsFromLink(queryInfo, cacheWriteBehavior, {
1007
+ const resultsFromLink = () => this.getResultsFromLink({
1193
1008
  query,
1194
1009
  variables,
1195
1010
  context,
1196
1011
  fetchPolicy,
1197
1012
  errorPolicy,
1013
+ }, {
1014
+ cacheWriteBehavior,
1015
+ queryInfo,
1016
+ observableQuery,
1198
1017
  }).pipe(validateDidEmitValue(), materialize(), map((result) => ({
1199
1018
  ...result,
1200
1019
  source: "network",
@@ -1248,7 +1067,7 @@ function validateDidEmitValue() {
1248
1067
  didEmitValue = true;
1249
1068
  },
1250
1069
  complete() {
1251
- invariant(didEmitValue, 94);
1070
+ invariant(didEmitValue, 93);
1252
1071
  },
1253
1072
  });
1254
1073
  }