@apollo/client 4.0.6 → 4.1.0-alpha.1

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 (121) hide show
  1. package/CHANGELOG.md +43 -4
  2. package/__cjs/cache/core/cache.cjs +1 -1
  3. package/__cjs/cache/core/cache.cjs.map +1 -1
  4. package/__cjs/cache/core/cache.d.cts +19 -0
  5. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  6. package/__cjs/cache/inmemory/entityStore.cjs.map +1 -1
  7. package/__cjs/cache/inmemory/inMemoryCache.cjs +3 -0
  8. package/__cjs/cache/inmemory/inMemoryCache.cjs.map +1 -1
  9. package/__cjs/cache/inmemory/inMemoryCache.d.cts +1 -0
  10. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  11. package/__cjs/cache/inmemory/key-extractor.cjs.map +1 -1
  12. package/__cjs/cache/inmemory/policies.cjs +4 -4
  13. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  14. package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
  15. package/__cjs/cache/inmemory/writeToStore.cjs +5 -5
  16. package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
  17. package/__cjs/core/ApolloClient.cjs +14 -13
  18. package/__cjs/core/ApolloClient.cjs.map +1 -1
  19. package/__cjs/core/ApolloClient.d.cts +12 -0
  20. package/__cjs/core/ObservableQuery.cjs +157 -110
  21. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  22. package/__cjs/core/ObservableQuery.d.cts +1 -0
  23. package/__cjs/core/QueryManager.cjs +19 -17
  24. package/__cjs/core/QueryManager.cjs.map +1 -1
  25. package/__cjs/incremental/handlers/defer20220824.cjs +20 -9
  26. package/__cjs/incremental/handlers/defer20220824.cjs.map +1 -1
  27. package/__cjs/incremental/handlers/defer20220824.d.cts +14 -5
  28. package/__cjs/incremental/handlers/graphql17Alpha9.cjs +181 -0
  29. package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -0
  30. package/__cjs/incremental/handlers/graphql17Alpha9.d.cts +97 -0
  31. package/__cjs/incremental/handlers/notImplemented.cjs +1 -1
  32. package/__cjs/incremental/index.cjs +3 -1
  33. package/__cjs/incremental/index.cjs.map +1 -1
  34. package/__cjs/incremental/index.d.cts +1 -0
  35. package/__cjs/invariantErrorCodes.cjs +75 -59
  36. package/__cjs/link/core/ApolloLink.cjs +3 -3
  37. package/__cjs/link/http/checkFetcher.cjs +1 -1
  38. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
  39. package/__cjs/link/persisted-queries/index.cjs +2 -2
  40. package/__cjs/link/ws/index.cjs +1 -1
  41. package/__cjs/local-state/LocalState.cjs +28 -14
  42. package/__cjs/local-state/LocalState.cjs.map +1 -1
  43. package/__cjs/local-state/LocalState.d.cts +3 -2
  44. package/__cjs/react/hooks/useReactiveVar.cjs +1 -2
  45. package/__cjs/react/hooks/useReactiveVar.cjs.map +1 -1
  46. package/__cjs/react/hooks/useReactiveVar.d.cts +1 -2
  47. package/__cjs/react/internal/cache/QueryReference.cjs +16 -0
  48. package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
  49. package/__cjs/react/internal/cache/QueryReference.d.cts +1 -0
  50. package/__cjs/testing/core/mocking/mockLink.cjs.map +1 -1
  51. package/__cjs/testing/core/mocking/mockLink.d.cts +1 -3
  52. package/__cjs/utilities/internal/DeepMerger.cjs +10 -1
  53. package/__cjs/utilities/internal/DeepMerger.cjs.map +1 -1
  54. package/__cjs/utilities/internal/DeepMerger.d.cts +14 -2
  55. package/__cjs/utilities/internal/getStoreKeyName.cjs +1 -0
  56. package/__cjs/utilities/internal/getStoreKeyName.cjs.map +1 -1
  57. package/__cjs/version.cjs +1 -1
  58. package/__cjs/version.cjs.map +1 -1
  59. package/cache/core/cache.d.ts +19 -0
  60. package/cache/core/cache.js +1 -1
  61. package/cache/core/cache.js.map +1 -1
  62. package/cache/inmemory/entityStore.js +3 -3
  63. package/cache/inmemory/entityStore.js.map +1 -1
  64. package/cache/inmemory/inMemoryCache.d.ts +1 -0
  65. package/cache/inmemory/inMemoryCache.js +3 -0
  66. package/cache/inmemory/inMemoryCache.js.map +1 -1
  67. package/cache/inmemory/key-extractor.js +1 -1
  68. package/cache/inmemory/key-extractor.js.map +1 -1
  69. package/cache/inmemory/policies.js +4 -4
  70. package/cache/inmemory/policies.js.map +1 -1
  71. package/cache/inmemory/readFromStore.js +2 -2
  72. package/cache/inmemory/writeToStore.js +5 -5
  73. package/cache/inmemory/writeToStore.js.map +1 -1
  74. package/core/ApolloClient.d.ts +12 -0
  75. package/core/ApolloClient.js +14 -13
  76. package/core/ApolloClient.js.map +1 -1
  77. package/core/ObservableQuery.d.ts +1 -0
  78. package/core/ObservableQuery.js +159 -112
  79. package/core/ObservableQuery.js.map +1 -1
  80. package/core/QueryManager.js +19 -17
  81. package/core/QueryManager.js.map +1 -1
  82. package/incremental/handlers/defer20220824.d.ts +14 -5
  83. package/incremental/handlers/defer20220824.js +20 -9
  84. package/incremental/handlers/defer20220824.js.map +1 -1
  85. package/incremental/handlers/graphql17Alpha9.d.ts +97 -0
  86. package/incremental/handlers/graphql17Alpha9.js +177 -0
  87. package/incremental/handlers/graphql17Alpha9.js.map +1 -0
  88. package/incremental/handlers/notImplemented.js +1 -1
  89. package/incremental/index.d.ts +1 -0
  90. package/incremental/index.js +3 -2
  91. package/incremental/index.js.map +1 -1
  92. package/invariantErrorCodes.js +75 -59
  93. package/link/core/ApolloLink.js +3 -3
  94. package/link/http/checkFetcher.js +1 -1
  95. package/link/http/parseAndCheckHttpResponse.js +1 -1
  96. package/link/persisted-queries/index.js +2 -2
  97. package/link/ws/index.js +1 -1
  98. package/local-state/LocalState.d.ts +3 -2
  99. package/local-state/LocalState.js +28 -14
  100. package/local-state/LocalState.js.map +1 -1
  101. package/package.json +1 -1
  102. package/react/hooks/useReactiveVar.d.ts +1 -2
  103. package/react/hooks/useReactiveVar.js +1 -2
  104. package/react/hooks/useReactiveVar.js.map +1 -1
  105. package/react/hooks-compiled/useReactiveVar.d.ts +1 -2
  106. package/react/hooks-compiled/useReactiveVar.js +1 -2
  107. package/react/hooks-compiled/useReactiveVar.js.map +1 -1
  108. package/react/internal/cache/QueryReference.d.ts +1 -0
  109. package/react/internal/cache/QueryReference.js +16 -0
  110. package/react/internal/cache/QueryReference.js.map +1 -1
  111. package/testing/core/mocking/mockLink.d.ts +1 -3
  112. package/testing/core/mocking/mockLink.js.map +1 -1
  113. package/utilities/internal/DeepMerger.d.ts +14 -2
  114. package/utilities/internal/DeepMerger.js +10 -1
  115. package/utilities/internal/DeepMerger.js.map +1 -1
  116. package/utilities/internal/getStoreKeyName.js +1 -0
  117. package/utilities/internal/getStoreKeyName.js.map +1 -1
  118. package/utilities/internal/globals/global.js +2 -2
  119. package/utilities/internal/globals/global.js.map +1 -1
  120. package/version.js +1 -1
  121. package/version.js.map +1 -1
@@ -68,6 +68,9 @@ class ObservableQuery {
68
68
  get networkStatus() {
69
69
  return this.subject.getValue().result.networkStatus;
70
70
  }
71
+ get cache() {
72
+ return this.queryManager.cache;
73
+ }
71
74
  constructor({ queryManager, options, transformedQuery = queryManager.transform(options.query), }) {
72
75
  this.queryManager = queryManager;
73
76
  // active state
@@ -205,7 +208,7 @@ class ObservableQuery {
205
208
  * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
206
209
  */
207
210
  getCacheDiff({ optimistic = true } = {}) {
208
- return this.queryManager.cache.diff({
211
+ return this.cache.diff({
209
212
  query: this.query,
210
213
  variables: this.variables,
211
214
  returnPartialData: true,
@@ -316,7 +319,7 @@ class ObservableQuery {
316
319
  }
317
320
  },
318
321
  };
319
- const cancelWatch = this.queryManager.cache.watch(watch);
322
+ const cancelWatch = this.cache.watch(watch);
320
323
  this.unsubscribeFromCache = Object.assign(() => {
321
324
  this.unsubscribeFromCache = undefined;
322
325
  cancelWatch();
@@ -380,7 +383,7 @@ class ObservableQuery {
380
383
  const queryDef = (0, internal_1.getQueryDefinition)(this.query);
381
384
  const vars = queryDef.variableDefinitions;
382
385
  if (!vars || !vars.some((v) => v.variable.name.value === "variables")) {
383
- __DEV__ && invariant_1.invariant.warn(77, variables, queryDef.name?.value || queryDef);
386
+ __DEV__ && invariant_1.invariant.warn(80, variables, queryDef.name?.value || queryDef);
384
387
  }
385
388
  }
386
389
  if (variables && !(0, equality_1.equal)(this.variables, variables)) {
@@ -396,7 +399,7 @@ class ObservableQuery {
396
399
  fetchMore({ query, variables, context, errorPolicy, updateQuery, }) {
397
400
  (0, invariant_1.invariant)(
398
401
  this.options.fetchPolicy !== "cache-only",
399
- 78,
402
+ 81,
400
403
  (0, internal_1.getOperationName)(this.query, "(anonymous)")
401
404
  );
402
405
  const combinedOptions = {
@@ -430,7 +433,7 @@ class ObservableQuery {
430
433
  let wasUpdated = false;
431
434
  const isCached = this.options.fetchPolicy !== "no-cache";
432
435
  if (!isCached) {
433
- (0, invariant_1.invariant)(updateQuery, 79);
436
+ (0, invariant_1.invariant)(updateQuery, 82);
434
437
  }
435
438
  const { finalize, pushNotification } = this.pushOperation(networkStatus_js_1.NetworkStatus.fetchMore);
436
439
  pushNotification({
@@ -438,109 +441,153 @@ class ObservableQuery {
438
441
  kind: "N",
439
442
  value: {},
440
443
  }, { shouldEmit: 3 /* EmitBehavior.networkStatusChange */ });
441
- return this.queryManager
442
- .fetchQuery(combinedOptions, networkStatus_js_1.NetworkStatus.fetchMore)
443
- .then((fetchMoreResult) => {
444
- // disable the `fetchMore` override that is currently active
445
- // the next updates caused by this should not be `fetchMore` anymore,
446
- // but `ready` or whatever other calculated loading state is currently
447
- // appropriate
448
- finalize();
449
- if (isCached) {
450
- // Separately getting a diff here before the batch - `onWatchUpdated` might be
451
- // called with an `undefined` `lastDiff` on the watcher if the cache was just subscribed to.
452
- const lastDiff = this.getCacheDiff();
453
- // Performing this cache update inside a cache.batch transaction ensures
454
- // any affected cache.watch watchers are notified at most once about any
455
- // updates. Most watchers will be using the QueryInfo class, which
456
- // responds to notifications by calling reobserveCacheFirst to deliver
457
- // fetchMore cache results back to this ObservableQuery.
458
- this.queryManager.cache.batch({
459
- update: (cache) => {
460
- if (updateQuery) {
461
- cache.updateQuery({
462
- query: this.query,
463
- variables: this.variables,
464
- returnPartialData: true,
465
- optimistic: false,
466
- }, (previous) => updateQuery(previous, {
467
- fetchMoreResult: fetchMoreResult.data,
468
- variables: combinedOptions.variables,
469
- }));
470
- }
471
- else {
472
- // If we're using a field policy instead of updateQuery, the only
473
- // thing we need to do is write the new data to the cache using
474
- // combinedOptions.variables (instead of this.variables, which is
475
- // what this.updateQuery uses, because it works by abusing the
476
- // original field value, keyed by the original variables).
477
- cache.writeQuery({
478
- query: combinedOptions.query,
479
- variables: combinedOptions.variables,
480
- data: fetchMoreResult.data,
481
- });
482
- }
483
- },
484
- onWatchUpdated: (watch, diff) => {
485
- if (watch.watcher === this &&
486
- !(0, equality_1.equal)(diff.result, lastDiff.result)) {
487
- wasUpdated = true;
488
- }
489
- },
490
- });
491
- }
492
- else {
493
- // There is a possibility `lastResult` may not be set when
494
- // `fetchMore` is called which would cause this to crash. This should
495
- // only happen if we haven't previously reported a result. We don't
496
- // quite know what the right behavior should be here since this block
497
- // of code runs after the fetch result has executed on the network.
498
- // We plan to let it crash in the meantime.
499
- //
500
- // If we get bug reports due to the `data` property access on
501
- // undefined, this should give us a real-world scenario that we can
502
- // use to test against and determine the right behavior. If we do end
503
- // up changing this behavior, this may require, for example, an
504
- // adjustment to the types on `updateQuery` since that function
505
- // expects that the first argument always contains previous result
506
- // data, but not `undefined`.
507
- const lastResult = this.getCurrentResult();
508
- const data = updateQuery(lastResult.data, {
509
- fetchMoreResult: fetchMoreResult.data,
510
- variables: combinedOptions.variables,
511
- });
512
- // was reportResult
513
- pushNotification({
514
- kind: "N",
515
- value: {
516
- ...lastResult,
517
- networkStatus: networkStatus_js_1.NetworkStatus.ready,
518
- // will be overwritten anyways, just here for types sake
519
- loading: false,
520
- data: data,
521
- dataState: lastResult.dataState === "streaming" ? "streaming" : "complete",
522
- },
523
- source: "network",
524
- });
444
+ const { promise, operator } = getTrackingOperatorPromise((value) => {
445
+ switch (value.kind) {
446
+ case "E": {
447
+ throw value.error;
448
+ }
449
+ case "N": {
450
+ if (value.source !== "newNetworkStatus" && !value.value.loading) {
451
+ return value.value;
452
+ }
453
+ }
525
454
  }
526
- return this.maskResult(fetchMoreResult);
527
- })
455
+ });
456
+ const { observable } = this.queryManager.fetchObservableWithInfo(combinedOptions, { networkStatus: networkStatus_js_1.NetworkStatus.fetchMore });
457
+ const subscription = observable
458
+ .pipe(operator, (0, rxjs_1.filter)((notification) => notification.kind === "N" && notification.source === "network"))
459
+ .subscribe({
460
+ next: (notification) => {
461
+ wasUpdated = false;
462
+ const fetchMoreResult = notification.value;
463
+ if ((0, utilities_1.isNetworkRequestSettled)(notification.value.networkStatus)) {
464
+ finalize();
465
+ }
466
+ if (isCached) {
467
+ // Performing this cache update inside a cache.batch transaction ensures
468
+ // any affected cache.watch watchers are notified at most once about any
469
+ // updates. Most watchers will be using the QueryInfo class, which
470
+ // responds to notifications by calling reobserveCacheFirst to deliver
471
+ // fetchMore cache results back to this ObservableQuery.
472
+ this.cache.batch({
473
+ update: (cache) => {
474
+ if (updateQuery) {
475
+ cache.updateQuery({
476
+ query: this.query,
477
+ variables: this.variables,
478
+ returnPartialData: true,
479
+ optimistic: false,
480
+ }, (previous) => updateQuery(previous, {
481
+ fetchMoreResult: fetchMoreResult.data,
482
+ variables: combinedOptions.variables,
483
+ }));
484
+ }
485
+ else {
486
+ // If we're using a field policy instead of updateQuery, the only
487
+ // thing we need to do is write the new data to the cache using
488
+ // combinedOptions.variables (instead of this.variables, which is
489
+ // what this.updateQuery uses, because it works by abusing the
490
+ // original field value, keyed by the original variables).
491
+ cache.writeQuery({
492
+ query: combinedOptions.query,
493
+ variables: combinedOptions.variables,
494
+ data: fetchMoreResult.data,
495
+ });
496
+ }
497
+ },
498
+ onWatchUpdated: (watch, diff) => {
499
+ if (watch.watcher === this) {
500
+ wasUpdated = true;
501
+ const lastResult = this.getCurrentResult();
502
+ // Let the cache watch from resubscribeCache handle the final
503
+ // result
504
+ if ((0, utilities_1.isNetworkRequestInFlight)(fetchMoreResult.networkStatus)) {
505
+ pushNotification({
506
+ kind: "N",
507
+ source: "network",
508
+ value: {
509
+ ...lastResult,
510
+ networkStatus: (fetchMoreResult.networkStatus ===
511
+ networkStatus_js_1.NetworkStatus.error) ?
512
+ networkStatus_js_1.NetworkStatus.ready
513
+ : fetchMoreResult.networkStatus,
514
+ // will be overwritten anyways, just here for types sake
515
+ loading: false,
516
+ data: diff.result,
517
+ dataState: fetchMoreResult.dataState === "streaming" ?
518
+ "streaming"
519
+ : "complete",
520
+ },
521
+ });
522
+ }
523
+ }
524
+ },
525
+ });
526
+ }
527
+ else {
528
+ // There is a possibility `lastResult` may not be set when
529
+ // `fetchMore` is called which would cause this to crash. This should
530
+ // only happen if we haven't previously reported a result. We don't
531
+ // quite know what the right behavior should be here since this block
532
+ // of code runs after the fetch result has executed on the network.
533
+ // We plan to let it crash in the meantime.
534
+ //
535
+ // If we get bug reports due to the `data` property access on
536
+ // undefined, this should give us a real-world scenario that we can
537
+ // use to test against and determine the right behavior. If we do end
538
+ // up changing this behavior, this may require, for example, an
539
+ // adjustment to the types on `updateQuery` since that function
540
+ // expects that the first argument always contains previous result
541
+ // data, but not `undefined`.
542
+ const lastResult = this.getCurrentResult();
543
+ const data = updateQuery(lastResult.data, {
544
+ fetchMoreResult: fetchMoreResult.data,
545
+ variables: combinedOptions.variables,
546
+ });
547
+ pushNotification({
548
+ kind: "N",
549
+ value: {
550
+ ...lastResult,
551
+ networkStatus: networkStatus_js_1.NetworkStatus.ready,
552
+ // will be overwritten anyways, just here for types sake
553
+ loading: false,
554
+ data: data,
555
+ dataState: lastResult.dataState === "streaming" ?
556
+ "streaming"
557
+ : "complete",
558
+ },
559
+ source: "network",
560
+ });
561
+ }
562
+ },
563
+ });
564
+ return (0, internal_1.preventUnhandledRejection)(promise
565
+ .then((result) => (0, internal_1.toQueryResult)(this.maskResult(result)))
528
566
  .finally(() => {
529
- // call `finalize` a second time in case the `.then` case above was not reached
530
- finalize();
531
- // In case the cache writes above did not generate a broadcast
532
- // notification (which would have been intercepted by onWatchUpdated),
533
- // likely because the written data were the same as what was already in
534
- // the cache, we still want fetchMore to deliver its final loading:false
535
- // result with the unchanged data.
567
+ subscription.unsubscribe();
536
568
  if (isCached && !wasUpdated) {
537
- pushNotification({
538
- kind: "N",
539
- source: "newNetworkStatus",
540
- value: {},
541
- }, { shouldEmit: 1 /* EmitBehavior.force */ });
569
+ finalize();
570
+ const lastResult = this.getCurrentResult();
571
+ if (lastResult.networkStatus === networkStatus_js_1.NetworkStatus.streaming) {
572
+ pushNotification({
573
+ kind: "N",
574
+ source: "network",
575
+ value: {
576
+ ...lastResult,
577
+ dataState: "complete",
578
+ networkStatus: networkStatus_js_1.NetworkStatus.ready,
579
+ },
580
+ });
581
+ }
582
+ else {
583
+ pushNotification({
584
+ kind: "N",
585
+ source: "newNetworkStatus",
586
+ value: {},
587
+ }, { shouldEmit: 1 /* EmitBehavior.force */ });
588
+ }
542
589
  }
543
- });
590
+ }));
544
591
  }
545
592
  // XXX the subscription variables are separate from the query variables.
546
593
  // if you want to update subscription variables, right now you have to do that separately,
@@ -566,7 +613,7 @@ class ObservableQuery {
566
613
  onError(error);
567
614
  }
568
615
  else {
569
- invariant_1.invariant.error(80, error);
616
+ invariant_1.invariant.error(83, error);
570
617
  }
571
618
  return;
572
619
  }
@@ -644,7 +691,7 @@ class ObservableQuery {
644
691
  previousData: result,
645
692
  });
646
693
  if (newResult) {
647
- queryManager.cache.writeQuery({
694
+ this.cache.writeQuery({
648
695
  query: this.options.query,
649
696
  data: newResult,
650
697
  variables: this.variables,
@@ -810,7 +857,7 @@ class ObservableQuery {
810
857
  if (!this.didWarnCacheOnlyPolling &&
811
858
  pollInterval &&
812
859
  fetchPolicy === "cache-only") {
813
- __DEV__ && invariant_1.invariant.warn(81, (0, internal_1.getOperationName)(this.query, "(anonymous)"));
860
+ __DEV__ && invariant_1.invariant.warn(84, (0, internal_1.getOperationName)(this.query, "(anonymous)"));
814
861
  this.didWarnCacheOnlyPolling = true;
815
862
  }
816
863
  }
@@ -1077,8 +1124,8 @@ class ObservableQuery {
1077
1124
  const { dirty } = this;
1078
1125
  this.resetNotifications();
1079
1126
  if (dirty &&
1080
- (this.options.fetchPolicy == "cache-only" ||
1081
- this.options.fetchPolicy == "cache-and-network" ||
1127
+ (this.options.fetchPolicy === "cache-only" ||
1128
+ this.options.fetchPolicy === "cache-and-network" ||
1082
1129
  !this.activeOperations.size)) {
1083
1130
  const diff = this.getCacheDiff();
1084
1131
  if (
@@ -1307,7 +1354,7 @@ class ObservableQuery {
1307
1354
  exports.ObservableQuery = ObservableQuery;
1308
1355
  function logMissingFieldErrors(missing) {
1309
1356
  if (environment_1.__DEV__ && missing) {
1310
- __DEV__ && invariant_1.invariant.debug(82, missing);
1357
+ __DEV__ && invariant_1.invariant.debug(85, missing);
1311
1358
  }
1312
1359
  }
1313
1360
  function isEqualQuery(a, b) {