@apollo/client 4.3.0-alpha.4 → 4.3.0-alpha.5

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 (150) hide show
  1. package/CHANGELOG.md +106 -0
  2. package/README.md +3 -7
  3. package/__cjs/cache/core/cache.cjs +1 -1
  4. package/__cjs/cache/core/cache.cjs.map +1 -1
  5. package/__cjs/cache/core/cache.d.cts +5 -1
  6. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  7. package/__cjs/cache/inmemory/inMemoryCache.cjs +0 -4
  8. package/__cjs/cache/inmemory/inMemoryCache.cjs.map +1 -1
  9. package/__cjs/cache/inmemory/inMemoryCache.d.cts +3 -4
  10. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  11. package/__cjs/cache/inmemory/policies.cjs +4 -4
  12. package/__cjs/cache/inmemory/readFromStore.cjs +71 -54
  13. package/__cjs/cache/inmemory/readFromStore.cjs.map +1 -1
  14. package/__cjs/cache/inmemory/readFromStore.d.cts +1 -1
  15. package/__cjs/cache/inmemory/types.d.cts +1 -2
  16. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  17. package/__cjs/core/ApolloClient.cjs +21 -22
  18. package/__cjs/core/ApolloClient.cjs.map +1 -1
  19. package/__cjs/core/ApolloClient.d.cts +44 -10
  20. package/__cjs/core/ObservableQuery.cjs +46 -5
  21. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  22. package/__cjs/core/ObservableQuery.d.cts +12 -1
  23. package/__cjs/core/QueryInfo.cjs +177 -154
  24. package/__cjs/core/QueryInfo.cjs.map +1 -1
  25. package/__cjs/core/QueryInfo.d.cts +13 -3
  26. package/__cjs/core/QueryManager.cjs +49 -27
  27. package/__cjs/core/QueryManager.cjs.map +1 -1
  28. package/__cjs/core/RefetchEventManager.cjs +3 -3
  29. package/__cjs/core/dataStateErrorCache.cjs +12 -0
  30. package/__cjs/core/dataStateErrorCache.cjs.map +1 -0
  31. package/__cjs/core/dataStateErrorCache.d.cts +3 -0
  32. package/__cjs/core/types.d.cts +2 -0
  33. package/__cjs/incremental/handlers/graphql17Alpha9.cjs +34 -11
  34. package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -1
  35. package/__cjs/incremental/handlers/graphql17Alpha9.d.cts +26 -3
  36. package/__cjs/incremental/types.d.cts +41 -3
  37. package/__cjs/invariantErrorCodes.cjs +50 -31
  38. package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
  39. package/__cjs/react/hooks/useBackgroundQuery.d.cts +100 -47
  40. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  41. package/__cjs/react/hooks/useLazyQuery.d.cts +26 -20
  42. package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
  43. package/__cjs/react/hooks/useLoadableQuery.d.cts +23 -15
  44. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  45. package/__cjs/react/hooks/useMutation.d.cts +10 -7
  46. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  47. package/__cjs/react/hooks/useQuery.d.cts +74 -32
  48. package/__cjs/react/hooks/useQueryRefHandlers.cjs.map +1 -1
  49. package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
  50. package/__cjs/react/hooks/useSuspenseQuery.d.cts +83 -34
  51. package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
  52. package/__cjs/react/internal/types.d.cts +14 -3
  53. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  54. package/__cjs/react/query-preloader/createQueryPreloader.d.cts +20 -5
  55. package/__cjs/testing/core/mocking/mockLink.cjs +21 -1
  56. package/__cjs/testing/core/mocking/mockLink.cjs.map +1 -1
  57. package/__cjs/testing/core/mocking/mockLink.d.cts +1 -2
  58. package/__cjs/utilities/internal/addDeferFragmentLabels.cjs +33 -0
  59. package/__cjs/utilities/internal/addDeferFragmentLabels.cjs.map +1 -0
  60. package/__cjs/utilities/internal/addDeferFragmentLabels.d.cts +3 -0
  61. package/__cjs/utilities/internal/getDirectiveArgValue.cjs +19 -0
  62. package/__cjs/utilities/internal/getDirectiveArgValue.cjs.map +1 -0
  63. package/__cjs/utilities/internal/getDirectiveArgValue.d.cts +9 -0
  64. package/__cjs/utilities/internal/index.cjs +6 -2
  65. package/__cjs/utilities/internal/index.cjs.map +1 -1
  66. package/__cjs/utilities/internal/index.d.cts +2 -0
  67. package/__cjs/version.cjs +1 -1
  68. package/cache/core/cache.d.ts +5 -1
  69. package/cache/core/cache.js +1 -1
  70. package/cache/core/cache.js.map +1 -1
  71. package/cache/inmemory/entityStore.js +3 -3
  72. package/cache/inmemory/inMemoryCache.d.ts +3 -4
  73. package/cache/inmemory/inMemoryCache.js +1 -5
  74. package/cache/inmemory/inMemoryCache.js.map +1 -1
  75. package/cache/inmemory/key-extractor.js +1 -1
  76. package/cache/inmemory/policies.js +4 -4
  77. package/cache/inmemory/readFromStore.d.ts +1 -1
  78. package/cache/inmemory/readFromStore.js +72 -55
  79. package/cache/inmemory/readFromStore.js.map +1 -1
  80. package/cache/inmemory/types.d.ts +1 -2
  81. package/cache/inmemory/types.js.map +1 -1
  82. package/cache/inmemory/writeToStore.js +4 -4
  83. package/core/ApolloClient.d.ts +44 -10
  84. package/core/ApolloClient.js +21 -22
  85. package/core/ApolloClient.js.map +1 -1
  86. package/core/ObservableQuery.d.ts +12 -1
  87. package/core/ObservableQuery.js +46 -5
  88. package/core/ObservableQuery.js.map +1 -1
  89. package/core/QueryInfo.d.ts +13 -3
  90. package/core/QueryInfo.js +173 -154
  91. package/core/QueryInfo.js.map +1 -1
  92. package/core/QueryManager.js +49 -27
  93. package/core/QueryManager.js.map +1 -1
  94. package/core/RefetchEventManager.js +3 -3
  95. package/core/dataStateErrorCache.d.ts +3 -0
  96. package/core/dataStateErrorCache.js +9 -0
  97. package/core/dataStateErrorCache.js.map +1 -0
  98. package/core/types.d.ts +2 -0
  99. package/core/types.js.map +1 -1
  100. package/incremental/handlers/graphql17Alpha9.d.ts +26 -3
  101. package/incremental/handlers/graphql17Alpha9.js +35 -12
  102. package/incremental/handlers/graphql17Alpha9.js.map +1 -1
  103. package/incremental/types.d.ts +41 -3
  104. package/incremental/types.js.map +1 -1
  105. package/invariantErrorCodes.js +50 -31
  106. package/package.json +1 -1
  107. package/react/hooks/useBackgroundQuery.d.ts +100 -47
  108. package/react/hooks/useBackgroundQuery.js.map +1 -1
  109. package/react/hooks/useLazyQuery.d.ts +26 -20
  110. package/react/hooks/useLazyQuery.js.map +1 -1
  111. package/react/hooks/useLoadableQuery.d.ts +23 -15
  112. package/react/hooks/useLoadableQuery.js.map +1 -1
  113. package/react/hooks/useMutation.d.ts +10 -7
  114. package/react/hooks/useMutation.js.map +1 -1
  115. package/react/hooks/useQuery.d.ts +74 -32
  116. package/react/hooks/useQuery.js.map +1 -1
  117. package/react/hooks/useQueryRefHandlers.js.map +1 -1
  118. package/react/hooks/useSuspenseQuery.d.ts +83 -34
  119. package/react/hooks/useSuspenseQuery.js.map +1 -1
  120. package/react/hooks-compiled/useBackgroundQuery.d.ts +100 -47
  121. package/react/hooks-compiled/useBackgroundQuery.js.map +1 -1
  122. package/react/hooks-compiled/useLazyQuery.d.ts +26 -20
  123. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  124. package/react/hooks-compiled/useLoadableQuery.d.ts +23 -15
  125. package/react/hooks-compiled/useLoadableQuery.js.map +1 -1
  126. package/react/hooks-compiled/useMutation.d.ts +10 -7
  127. package/react/hooks-compiled/useMutation.js.map +1 -1
  128. package/react/hooks-compiled/useQuery.d.ts +74 -32
  129. package/react/hooks-compiled/useQuery.js.map +1 -1
  130. package/react/hooks-compiled/useQueryRefHandlers.js.map +1 -1
  131. package/react/hooks-compiled/useSuspenseQuery.d.ts +83 -34
  132. package/react/hooks-compiled/useSuspenseQuery.js.map +1 -1
  133. package/react/internal/cache/QueryReference.js.map +1 -1
  134. package/react/internal/types.d.ts +15 -4
  135. package/react/internal/types.js.map +1 -1
  136. package/react/query-preloader/createQueryPreloader.d.ts +20 -5
  137. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  138. package/testing/core/mocking/mockLink.d.ts +1 -2
  139. package/testing/core/mocking/mockLink.js +21 -1
  140. package/testing/core/mocking/mockLink.js.map +1 -1
  141. package/utilities/internal/addDeferFragmentLabels.d.ts +3 -0
  142. package/utilities/internal/addDeferFragmentLabels.js +30 -0
  143. package/utilities/internal/addDeferFragmentLabels.js.map +1 -0
  144. package/utilities/internal/getDirectiveArgValue.d.ts +9 -0
  145. package/utilities/internal/getDirectiveArgValue.js +16 -0
  146. package/utilities/internal/getDirectiveArgValue.js.map +1 -0
  147. package/utilities/internal/index.d.ts +2 -0
  148. package/utilities/internal/index.js +2 -0
  149. package/utilities/internal/index.js.map +1 -1
  150. package/version.js +1 -1
@@ -11,6 +11,7 @@ import { cacheSizes, DocumentTransform, isNetworkRequestInFlight, print, } from
11
11
  import { __DEV__ } from "@apollo/client/utilities/environment";
12
12
  import { AutoCleanedWeakCache, checkDocument, extensionsSymbol, filterMap, getDefaultValues, getOperationDefinition, getOperationName, graphQLResultHasError, hasDirectives, hasForcedResolvers, isDocumentNode, isNonNullObject, makeUniqueId, mergeOptions, removeDirectivesFromDocument, streamInfoSymbol, toQueryResult, } from "@apollo/client/utilities/internal";
13
13
  import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant";
14
+ import { dataStateErrorCache } from "./dataStateErrorCache.js";
14
15
  import { NetworkStatus } from "./networkStatus.js";
15
16
  import { logMissingFieldErrors, ObservableQuery } from "./ObservableQuery.js";
16
17
  import { QueryInfo } from "./QueryInfo.js";
@@ -75,6 +76,12 @@ export class QueryManager {
75
76
  // selections and fragments from the fragment registry.
76
77
  .concat(defaultDocumentTransform)
77
78
  : defaultDocumentTransform;
79
+ // Put the incremental transform last so custom document transforms don't
80
+ // revert changes made to the document accidentally. Adding last also
81
+ // ensures `@defer` fragments added by the fragment registry are labeled.
82
+ if (this.incrementalHandler.documentTransform) {
83
+ this.documentTransform = this.documentTransform.concat(this.incrementalHandler.documentTransform);
84
+ }
78
85
  this.defaultContext = options.defaultContext || {};
79
86
  if ((this.onBroadcast = options.onBroadcast)) {
80
87
  this.mutationStore = {};
@@ -92,7 +99,7 @@ export class QueryManager {
92
99
  */
93
100
  stop() {
94
101
  this.obsQueries.forEach((oq) => oq.stop());
95
- this.cancelPendingFetches(newInvariantError(89));
102
+ this.cancelPendingFetches(newInvariantError(90));
96
103
  }
97
104
  cancelPendingFetches(error) {
98
105
  this.fetchCancelFns.forEach((cancel) => cancel(error));
@@ -105,7 +112,7 @@ export class QueryManager {
105
112
  variables = this.getVariables(mutation, variables);
106
113
  if (hasClientExports) {
107
114
  if (__DEV__) {
108
- invariant(this.localState, 90, getOperationName(mutation, "(anonymous)"));
115
+ invariant(this.localState, 91, getOperationName(mutation, "(anonymous)"));
109
116
  }
110
117
  variables = await this.localState.getExportedVariables({
111
118
  client: this.client,
@@ -338,7 +345,7 @@ export class QueryManager {
338
345
  // depend on values that previously existed in the data portion of the
339
346
  // store. So, we cancel the promises and observers that we have issued
340
347
  // so far and not yet resolved (in the case of queries).
341
- this.cancelPendingFetches(newInvariantError(91));
348
+ this.cancelPendingFetches(newInvariantError(92));
342
349
  this.obsQueries.forEach((observableQuery) => {
343
350
  // Set loading to true so listeners don't trigger unless they want
344
351
  // results with partial data.
@@ -408,10 +415,10 @@ export class QueryManager {
408
415
  if (!included) {
409
416
  const queryName = queryNames.get(nameOrQueryString);
410
417
  if (queryName) {
411
- __DEV__ && invariant.warn(92, queryName);
418
+ __DEV__ && invariant.warn(93, queryName);
412
419
  }
413
420
  else {
414
- __DEV__ && invariant.warn(93);
421
+ __DEV__ && invariant.warn(94);
415
422
  }
416
423
  }
417
424
  });
@@ -440,7 +447,7 @@ export class QueryManager {
440
447
  if (__DEV__) {
441
448
  invariant(
442
449
  !this.getDocumentInfo(query).hasClientExports || this.localState,
443
- 94,
450
+ 95,
444
451
  getOperationName(query, "(anonymous)")
445
452
  );
446
453
  }
@@ -585,14 +592,14 @@ export class QueryManager {
585
592
  if (__DEV__) {
586
593
  invariant(
587
594
  this.localState,
588
- 95,
595
+ 96,
589
596
  operation[0].toUpperCase() + operation.slice(1),
590
597
  operationName ?? "(anonymous)"
591
598
  );
592
599
  }
593
600
  invariant(
594
601
  !hasIncrementalDirective,
595
- 96,
602
+ 97,
596
603
  operation[0].toUpperCase() + operation.slice(1),
597
604
  operationName ?? "(anonymous)"
598
605
  );
@@ -616,7 +623,7 @@ export class QueryManager {
616
623
  })),
617
624
  };
618
625
  }
619
- getResultsFromLink(options, { queryInfo, cacheWriteBehavior, observableQuery, exposeExtensions, }) {
626
+ getResultsFromLink(options, { queryInfo, cacheWriteBehavior, observableQuery, exposeExtensions, prunePendingDeferFragments, }) {
620
627
  const { errorPolicy } = options;
621
628
  // Performing transformForLink here gives this.cache a chance to fill in
622
629
  // missing fragment definitions (for example) before sending this document
@@ -631,12 +638,15 @@ export class QueryManager {
631
638
  document: linkDocument,
632
639
  cacheWriteBehavior,
633
640
  returnPartialData: options.returnPartialData,
641
+ prunePendingDeferFragments,
634
642
  });
635
643
  const hasErrors = graphQLResultHasError(result);
636
644
  if (hasErrors && errorPolicy === "none") {
637
645
  queryInfo.resetLastWrite();
638
646
  observableQuery?.["resetNotifications"]();
639
- throw new CombinedGraphQLErrors(removeStreamDetailsFromExtensions(result));
647
+ const error = new CombinedGraphQLErrors(removeStreamDetailsFromExtensions(result));
648
+ dataStateErrorCache.set(error, dataState);
649
+ throw error;
640
650
  }
641
651
  const partial = dataState !== "complete";
642
652
  const aqr = {
@@ -658,16 +668,10 @@ export class QueryManager {
658
668
  if (exposeExtensions && "extensions" in result) {
659
669
  aqr[extensionsSymbol] = result.extensions;
660
670
  }
661
- if (hasErrors) {
662
- if (errorPolicy === "none") {
663
- aqr.data = void 0;
664
- aqr.dataState = "empty";
665
- }
666
- if (errorPolicy !== "ignore") {
667
- aqr.error = new CombinedGraphQLErrors(removeStreamDetailsFromExtensions(result));
668
- if (aqr.networkStatus !== NetworkStatus.streaming) {
669
- aqr.networkStatus = NetworkStatus.error;
670
- }
671
+ if (hasErrors && errorPolicy !== "ignore") {
672
+ aqr.error = new CombinedGraphQLErrors(removeStreamDetailsFromExtensions(result));
673
+ if (aqr.networkStatus !== NetworkStatus.streaming) {
674
+ aqr.networkStatus = NetworkStatus.error;
671
675
  }
672
676
  }
673
677
  return aqr;
@@ -765,7 +769,7 @@ export class QueryManager {
765
769
  // we deprecated and removed LocalState.
766
770
  if (this.getDocumentInfo(normalized.query).hasClientExports) {
767
771
  if (__DEV__) {
768
- invariant(this.localState, 97, getOperationName(normalized.query, "(anonymous)"));
772
+ invariant(this.localState, 98, getOperationName(normalized.query, "(anonymous)"));
769
773
  }
770
774
  observable = from(this.localState.getExportedVariables({
771
775
  client: this.client,
@@ -938,7 +942,7 @@ export class QueryManager {
938
942
  !isFullyUnmaskedOperation(document) &&
939
943
  !this.noCacheWarningsByCause.has(cause)) {
940
944
  this.noCacheWarningsByCause.add(cause);
941
- __DEV__ && invariant.warn(98, getOperationName(document, `Unnamed ${operationType ?? "operation"}`));
945
+ __DEV__ && invariant.warn(99, getOperationName(document, `Unnamed ${operationType ?? "operation"}`));
942
946
  }
943
947
  }
944
948
  return (this.dataMasking ?
@@ -986,6 +990,10 @@ export class QueryManager {
986
990
  return of({
987
991
  kind: "N",
988
992
  value: toResult(data),
993
+ // Always attach the variables used for this fetch so @export
994
+ // resolution can update ObservableQuery.options.variables and
995
+ // resubscribe the cache watch under the correct variable set.
996
+ resolvedVariables: variables,
989
997
  source: "cache",
990
998
  });
991
999
  };
@@ -996,7 +1004,7 @@ export class QueryManager {
996
1004
  (diff.complete || returnPartialData) &&
997
1005
  this.getDocumentInfo(query).hasForcedResolvers) {
998
1006
  if (__DEV__) {
999
- invariant(this.localState, 99, getOperationName(query, "(anonymous)"));
1007
+ invariant(this.localState, 100, getOperationName(query, "(anonymous)"));
1000
1008
  }
1001
1009
  onCacheHit();
1002
1010
  return from(this.localState.execute({
@@ -1011,6 +1019,10 @@ export class QueryManager {
1011
1019
  }).then((resolved) => ({
1012
1020
  kind: "N",
1013
1021
  value: toResult(resolved.data || void 0),
1022
+ // Always attach the variables used for this fetch so @export
1023
+ // resolution can update ObservableQuery.options.variables and
1024
+ // resubscribe the cache watch under the correct variable set.
1025
+ resolvedVariables: variables,
1014
1026
  source: "cache",
1015
1027
  })));
1016
1028
  }
@@ -1025,7 +1037,7 @@ export class QueryManager {
1025
1037
  }
1026
1038
  return fromData(data || undefined);
1027
1039
  };
1028
- const resultsFromLink = () => this.getResultsFromLink({
1040
+ const resultsFromLink = ({ prunePendingDeferFragments = true, } = {}) => this.getResultsFromLink({
1029
1041
  query,
1030
1042
  variables,
1031
1043
  context,
@@ -1038,8 +1050,13 @@ export class QueryManager {
1038
1050
  queryInfo,
1039
1051
  observableQuery,
1040
1052
  exposeExtensions,
1053
+ prunePendingDeferFragments,
1041
1054
  }).pipe(validateDidEmitValue(), materialize(), map((result) => ({
1042
1055
  ...result,
1056
+ // Always attach the variables used for this fetch so @export
1057
+ // resolution can update ObservableQuery.options.variables and
1058
+ // resubscribe the cache watch under the correct variable set.
1059
+ resolvedVariables: variables,
1043
1060
  source: "network",
1044
1061
  })));
1045
1062
  switch (fetchPolicy) {
@@ -1065,7 +1082,7 @@ export class QueryManager {
1065
1082
  if (diff.complete || returnPartialData) {
1066
1083
  return {
1067
1084
  fromLink: true,
1068
- observable: concat(resultsFromCache(diff, NetworkStatus.loading), resultsFromLink()),
1085
+ observable: concat(resultsFromCache(diff, NetworkStatus.loading), resultsFromLink({ prunePendingDeferFragments: false })),
1069
1086
  };
1070
1087
  }
1071
1088
  return { fromLink: true, observable: resultsFromLink() };
@@ -1076,7 +1093,12 @@ export class QueryManager {
1076
1093
  observable: concat(resultsFromCache(readCache(), NetworkStatus.ready)),
1077
1094
  };
1078
1095
  case "network-only":
1079
- return { fromLink: true, observable: resultsFromLink() };
1096
+ return {
1097
+ fromLink: true,
1098
+ observable: resultsFromLink({
1099
+ prunePendingDeferFragments: networkStatus !== NetworkStatus.refetch,
1100
+ }),
1101
+ };
1080
1102
  case "no-cache":
1081
1103
  return { fromLink: true, observable: resultsFromLink() };
1082
1104
  case "standby":
@@ -1091,7 +1113,7 @@ function validateDidEmitValue() {
1091
1113
  didEmitValue = true;
1092
1114
  },
1093
1115
  complete() {
1094
- invariant(didEmitValue, 100);
1116
+ invariant(didEmitValue, 101);
1095
1117
  },
1096
1118
  });
1097
1119
  }