@apollo/client 4.0.0-alpha.4 → 4.0.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 (168) hide show
  1. package/.changeset/eleven-kangaroos-jump.md +5 -0
  2. package/.changeset/forty-shrimps-fry.md +5 -0
  3. package/.changeset/grumpy-vans-type.md +5 -0
  4. package/.changeset/little-spoons-kick.md +7 -0
  5. package/.changeset/loud-cows-raise.md +7 -0
  6. package/.changeset/many-buses-allow.md +5 -0
  7. package/.changeset/poor-eels-punch.md +5 -0
  8. package/.changeset/pre.json +13 -0
  9. package/.changeset/real-teachers-peel.md +5 -0
  10. package/.changeset/slimy-chicken-melt.md +5 -0
  11. package/.changeset/thick-books-grin.md +5 -0
  12. package/.changeset/tough-rockets-allow.md +5 -0
  13. package/.changeset/tricky-tables-shave.md +5 -0
  14. package/.changeset/warm-ties-sit.md +7 -0
  15. package/CHANGELOG.md +40 -0
  16. package/__cjs/cache/core/cache.cjs +1 -1
  17. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  18. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  19. package/__cjs/cache/inmemory/policies.cjs +4 -4
  20. package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
  21. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  22. package/__cjs/core/ApolloClient.cjs +3 -3
  23. package/__cjs/core/ApolloClient.cjs.map +1 -1
  24. package/__cjs/core/ApolloClient.d.cts +4 -4
  25. package/__cjs/core/LocalState.cjs +2 -2
  26. package/__cjs/core/ObservableQuery.cjs +15 -12
  27. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  28. package/__cjs/core/QueryManager.cjs +57 -35
  29. package/__cjs/core/QueryManager.cjs.map +1 -1
  30. package/__cjs/core/QueryManager.d.cts +3 -3
  31. package/__cjs/core/types.d.cts +32 -0
  32. package/__cjs/invariantErrorCodes.cjs +57 -77
  33. package/__cjs/link/core/ApolloLink.cjs +2 -2
  34. package/__cjs/link/http/checkFetcher.cjs +1 -1
  35. package/__cjs/link/http/createHttpLink.cjs +1 -1
  36. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
  37. package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
  38. package/__cjs/link/persisted-queries/index.cjs +2 -2
  39. package/__cjs/link/utils/validateOperation.cjs +1 -1
  40. package/__cjs/masking/maskDefinition.cjs +2 -2
  41. package/__cjs/masking/maskFragment.cjs +2 -2
  42. package/__cjs/masking/maskOperation.cjs +1 -1
  43. package/__cjs/masking/utils.cjs +1 -1
  44. package/__cjs/react/context/ApolloConsumer.cjs +1 -1
  45. package/__cjs/react/context/ApolloContext.cjs +1 -1
  46. package/__cjs/react/context/ApolloContext.cjs.map +1 -1
  47. package/__cjs/react/context/ApolloContext.d.cts +2 -2
  48. package/__cjs/react/context/ApolloProvider.cjs +1 -1
  49. package/__cjs/react/hooks/internal/wrapHook.cjs +26 -5
  50. package/__cjs/react/hooks/internal/wrapHook.cjs.map +1 -1
  51. package/__cjs/react/hooks/internal/wrapHook.d.cts +3 -1
  52. package/__cjs/react/hooks/useMutation.cjs +14 -19
  53. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  54. package/__cjs/react/hooks/useMutation.d.cts +4 -4
  55. package/__cjs/react/hooks/useQuery.cjs +82 -187
  56. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  57. package/__cjs/react/hooks/useQuery.d.cts +4 -0
  58. package/__cjs/react/hooks/useSubscription.cjs +8 -57
  59. package/__cjs/react/hooks/useSubscription.cjs.map +1 -1
  60. package/__cjs/react/hooks/useSubscription.d.cts +6 -42
  61. package/__cjs/react/hooks/useSuspenseQuery.cjs +2 -2
  62. package/__cjs/react/hooks/useSyncExternalStore.cjs +1 -1
  63. package/__cjs/react/internal/index.cjs +3 -1
  64. package/__cjs/react/internal/index.cjs.map +1 -1
  65. package/__cjs/react/internal/index.d.cts +1 -0
  66. package/__cjs/react/ssr/getDataFromTree.cjs +43 -12
  67. package/__cjs/react/ssr/getDataFromTree.cjs.map +1 -1
  68. package/__cjs/react/ssr/getDataFromTree.d.cts +5 -0
  69. package/__cjs/react/ssr/index.cjs +1 -3
  70. package/__cjs/react/ssr/index.cjs.map +1 -1
  71. package/__cjs/react/ssr/index.d.cts +0 -1
  72. package/__cjs/react/ssr/useSSRQuery.cjs +60 -0
  73. package/__cjs/react/ssr/useSSRQuery.cjs.map +1 -0
  74. package/__cjs/react/ssr/useSSRQuery.d.cts +5 -0
  75. package/__cjs/react/types/deprecated.d.cts +1 -1
  76. package/__cjs/react/types/types.documentation.d.cts +4 -12
  77. package/__cjs/testing/matchers/index.cjs +2 -0
  78. package/__cjs/testing/matchers/index.cjs.map +1 -1
  79. package/__cjs/testing/matchers/toEmitStrictTyped.cjs +42 -0
  80. package/__cjs/testing/matchers/toEmitStrictTyped.cjs.map +1 -0
  81. package/__cjs/testing/matchers/toEmitStrictTyped.d.cts +7 -0
  82. package/__cjs/testing/matchers/toEqualStrictTyped.cjs +2 -20
  83. package/__cjs/testing/matchers/toEqualStrictTyped.cjs.map +1 -1
  84. package/__cjs/testing/matchers/utils/getSerializableProperties.cjs +23 -0
  85. package/__cjs/testing/matchers/utils/getSerializableProperties.cjs.map +1 -0
  86. package/__cjs/testing/matchers/utils/getSerializableProperties.d.cts +2 -0
  87. package/__cjs/version.cjs +1 -1
  88. package/__cjs/version.d.cts +1 -1
  89. package/cache/core/cache.js +1 -1
  90. package/cache/inmemory/entityStore.js +3 -3
  91. package/cache/inmemory/key-extractor.js +1 -1
  92. package/cache/inmemory/policies.js +4 -4
  93. package/cache/inmemory/readFromStore.js +2 -2
  94. package/cache/inmemory/writeToStore.js +4 -4
  95. package/core/ApolloClient.d.ts +4 -4
  96. package/core/ApolloClient.js +3 -3
  97. package/core/ApolloClient.js.map +1 -1
  98. package/core/LocalState.js +2 -2
  99. package/core/ObservableQuery.js +15 -12
  100. package/core/ObservableQuery.js.map +1 -1
  101. package/core/QueryManager.d.ts +3 -3
  102. package/core/QueryManager.js +58 -36
  103. package/core/QueryManager.js.map +1 -1
  104. package/core/types.d.ts +32 -0
  105. package/invariantErrorCodes.js +57 -77
  106. package/link/core/ApolloLink.js +2 -2
  107. package/link/http/checkFetcher.js +1 -1
  108. package/link/http/createHttpLink.js +1 -1
  109. package/link/http/parseAndCheckHttpResponse.js +1 -1
  110. package/link/http/serializeFetchParameter.js +1 -1
  111. package/link/persisted-queries/index.js +2 -2
  112. package/link/utils/validateOperation.js +1 -1
  113. package/masking/maskDefinition.js +2 -2
  114. package/masking/maskFragment.js +2 -2
  115. package/masking/maskOperation.js +1 -1
  116. package/masking/utils.js +1 -1
  117. package/package.json +1 -1
  118. package/react/context/ApolloConsumer.js +1 -1
  119. package/react/context/ApolloContext.d.ts +2 -2
  120. package/react/context/ApolloContext.js +1 -1
  121. package/react/context/ApolloContext.js.map +1 -1
  122. package/react/context/ApolloProvider.js +1 -1
  123. package/react/hooks/internal/wrapHook.d.ts +3 -1
  124. package/react/hooks/internal/wrapHook.js +24 -5
  125. package/react/hooks/internal/wrapHook.js.map +1 -1
  126. package/react/hooks/useMutation.d.ts +4 -4
  127. package/react/hooks/useMutation.js +14 -19
  128. package/react/hooks/useMutation.js.map +1 -1
  129. package/react/hooks/useQuery.d.ts +4 -0
  130. package/react/hooks/useQuery.js +83 -188
  131. package/react/hooks/useQuery.js.map +1 -1
  132. package/react/hooks/useSubscription.d.ts +6 -42
  133. package/react/hooks/useSubscription.js +7 -53
  134. package/react/hooks/useSubscription.js.map +1 -1
  135. package/react/hooks/useSuspenseQuery.js +2 -2
  136. package/react/hooks/useSyncExternalStore.js +1 -1
  137. package/react/internal/index.d.ts +1 -0
  138. package/react/internal/index.js +2 -1
  139. package/react/internal/index.js.map +1 -1
  140. package/react/ssr/getDataFromTree.d.ts +5 -0
  141. package/react/ssr/getDataFromTree.js +43 -12
  142. package/react/ssr/getDataFromTree.js.map +1 -1
  143. package/react/ssr/index.d.ts +0 -1
  144. package/react/ssr/index.js +0 -1
  145. package/react/ssr/index.js.map +1 -1
  146. package/react/ssr/useSSRQuery.d.ts +5 -0
  147. package/react/ssr/useSSRQuery.js +56 -0
  148. package/react/ssr/useSSRQuery.js.map +1 -0
  149. package/react/types/deprecated.d.ts +1 -1
  150. package/react/types/types.documentation.d.ts +4 -12
  151. package/testing/matchers/index.js +2 -0
  152. package/testing/matchers/index.js.map +1 -1
  153. package/testing/matchers/toEmitStrictTyped.d.ts +7 -0
  154. package/testing/matchers/toEmitStrictTyped.js +38 -0
  155. package/testing/matchers/toEmitStrictTyped.js.map +1 -0
  156. package/testing/matchers/toEqualStrictTyped.js +1 -19
  157. package/testing/matchers/toEqualStrictTyped.js.map +1 -1
  158. package/testing/matchers/utils/getSerializableProperties.d.ts +2 -0
  159. package/testing/matchers/utils/getSerializableProperties.js +20 -0
  160. package/testing/matchers/utils/getSerializableProperties.js.map +1 -0
  161. package/version.d.ts +1 -1
  162. package/version.js +1 -1
  163. package/__cjs/react/ssr/RenderPromises.cjs +0 -104
  164. package/__cjs/react/ssr/RenderPromises.cjs.map +0 -1
  165. package/__cjs/react/ssr/RenderPromises.d.cts +0 -25
  166. package/react/ssr/RenderPromises.d.ts +0 -25
  167. package/react/ssr/RenderPromises.js +0 -100
  168. package/react/ssr/RenderPromises.js.map +0 -1
@@ -95,15 +95,15 @@ class QueryManager {
95
95
  this.queries.forEach((_info, queryId) => {
96
96
  this.removeQuery(queryId);
97
97
  });
98
- this.cancelPendingFetches((0, invariant_1.newInvariantError)(72));
98
+ this.cancelPendingFetches((0, invariant_1.newInvariantError)(68));
99
99
  }
100
100
  cancelPendingFetches(error) {
101
101
  this.fetchCancelFns.forEach((cancel) => cancel(error));
102
102
  this.fetchCancelFns.clear();
103
103
  }
104
104
  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, }) {
105
- (0, invariant_1.invariant)(mutation, 73);
106
- (0, invariant_1.invariant)(fetchPolicy === "network-only" || fetchPolicy === "no-cache", 74);
105
+ (0, invariant_1.invariant)(mutation, 69);
106
+ (0, invariant_1.invariant)(fetchPolicy === "network-only" || fetchPolicy === "no-cache", 70);
107
107
  const mutationId = this.generateMutationId();
108
108
  mutation = this.cache.transformForLink(this.transform(mutation));
109
109
  const { hasClientExports } = this.getDocumentInfo(mutation);
@@ -137,7 +137,8 @@ class QueryManager {
137
137
  optimisticResponse: isOptimistic ? optimisticResponse : void 0,
138
138
  }, variables, {}, false)
139
139
  .pipe((0, rxjs_1.mergeMap)((result) => {
140
- if ((0, utilities_4.graphQLResultHasError)(result) && errorPolicy === "none") {
140
+ const hasErrors = (0, utilities_4.graphQLResultHasError)(result);
141
+ if (hasErrors && errorPolicy === "none") {
141
142
  throw new errors_1.CombinedGraphQLErrors((0, utilities_4.getGraphQLErrorsFromResult)(result));
142
143
  }
143
144
  if (mutationStoreValue) {
@@ -148,8 +149,7 @@ class QueryManager {
148
149
  if (typeof refetchQueries === "function") {
149
150
  refetchQueries = refetchQueries(storeResult);
150
151
  }
151
- if (errorPolicy === "ignore" &&
152
- (0, utilities_4.graphQLResultHasError)(storeResult)) {
152
+ if (errorPolicy === "ignore" && hasErrors) {
153
153
  delete storeResult.errors;
154
154
  }
155
155
  return (0, rxjs_1.from)(this.markMutationResult({
@@ -178,28 +178,40 @@ class QueryManager {
178
178
  // ExecutionPatchResult has arrived and we have assembled the
179
179
  // multipart response into a single result.
180
180
  if (!("hasNext" in storeResult) || storeResult.hasNext === false) {
181
- resolve({
182
- ...storeResult,
181
+ const result = {
183
182
  data: this.maskOperation({
184
183
  document: mutation,
185
184
  data: storeResult.data,
186
185
  fetchPolicy,
187
186
  id: mutationId,
188
187
  }),
189
- });
188
+ };
189
+ if ((0, utilities_4.graphQLResultHasError)(storeResult)) {
190
+ result.error = new errors_1.CombinedGraphQLErrors((0, utilities_4.getGraphQLErrorsFromResult)(storeResult));
191
+ }
192
+ if (storeResult.extensions) {
193
+ result.extensions = storeResult.extensions;
194
+ }
195
+ resolve(result);
190
196
  }
191
197
  },
192
198
  error: (err) => {
193
- err = maybeWrapError(err);
199
+ const error = maybeWrapError(err);
194
200
  if (mutationStoreValue) {
195
201
  mutationStoreValue.loading = false;
196
- mutationStoreValue.error = err;
202
+ mutationStoreValue.error = error;
197
203
  }
198
204
  if (isOptimistic) {
199
205
  this.cache.removeOptimistic(mutationId);
200
206
  }
201
207
  this.broadcastQueries();
202
- reject(err);
208
+ if (errorPolicy === "ignore") {
209
+ return resolve({ data: undefined });
210
+ }
211
+ if (errorPolicy === "all") {
212
+ return resolve({ data: undefined, error });
213
+ }
214
+ reject(error);
203
215
  },
204
216
  });
205
217
  });
@@ -472,10 +484,10 @@ class QueryManager {
472
484
  // TODO: catch `EmptyError` and rethrow as network error if `complete`
473
485
  // notification is emitted without a value.
474
486
  query(options, queryId = this.generateQueryId()) {
475
- (0, invariant_1.invariant)(options.query, 75);
476
- (0, invariant_1.invariant)(options.query.kind === "Document", 76);
477
- (0, invariant_1.invariant)(!options.returnPartialData, 77);
478
- (0, invariant_1.invariant)(!options.pollInterval, 78);
487
+ (0, invariant_1.invariant)(options.query, 71);
488
+ (0, invariant_1.invariant)(options.query.kind === "Document", 72);
489
+ (0, invariant_1.invariant)(!options.returnPartialData, 73);
490
+ (0, invariant_1.invariant)(!options.pollInterval, 74);
479
491
  const query = this.transform(options.query);
480
492
  return this.fetchQuery(queryId, { ...options, query })
481
493
  .then((result) => result && {
@@ -509,7 +521,7 @@ class QueryManager {
509
521
  // depend on values that previously existed in the data portion of the
510
522
  // store. So, we cancel the promises and observers that we have issued
511
523
  // so far and not yet resolved (in the case of queries).
512
- this.cancelPendingFetches((0, invariant_1.newInvariantError)(79));
524
+ this.cancelPendingFetches((0, invariant_1.newInvariantError)(75));
513
525
  this.queries.forEach((queryInfo) => {
514
526
  if (queryInfo.observableQuery) {
515
527
  // Set loading to true so listeners don't trigger unless they want
@@ -597,10 +609,10 @@ class QueryManager {
597
609
  if (!included) {
598
610
  const queryName = queryNames.get(nameOrQueryString);
599
611
  if (queryName) {
600
- __DEV__ && invariant_1.invariant.warn(80, queryName);
612
+ __DEV__ && invariant_1.invariant.warn(76, queryName);
601
613
  }
602
614
  else {
603
- __DEV__ && invariant_1.invariant.warn(81);
615
+ __DEV__ && invariant_1.invariant.warn(77);
604
616
  }
605
617
  }
606
618
  });
@@ -629,38 +641,48 @@ class QueryManager {
629
641
  const { fetchPolicy, errorPolicy = "none", context = {}, extensions = {}, } = options;
630
642
  query = this.transform(query);
631
643
  variables = this.getVariables(query, variables);
632
- const makeObservable = (variables) => this.getObservableFromLink(query, context, variables, extensions).pipe((0, rxjs_1.map)((result) => {
644
+ const makeObservable = (variables) => this.getObservableFromLink(query, context, variables, extensions).pipe((0, rxjs_1.map)((rawResult) => {
633
645
  if (fetchPolicy !== "no-cache") {
634
646
  // the subscription interface should handle not sending us results we no longer subscribe to.
635
647
  // XXX I don't think we ever send in an object with errors, but we might in the future...
636
- if ((0, QueryInfo_js_1.shouldWriteResult)(result, errorPolicy)) {
648
+ if ((0, QueryInfo_js_1.shouldWriteResult)(rawResult, errorPolicy)) {
637
649
  this.cache.write({
638
650
  query,
639
- result: result.data,
651
+ result: rawResult.data,
640
652
  dataId: "ROOT_SUBSCRIPTION",
641
653
  variables: variables,
642
654
  });
643
655
  }
644
656
  this.broadcastQueries();
645
657
  }
646
- const hasErrors = (0, utilities_4.graphQLResultHasError)(result);
647
- const hasProtocolErrors = (0, errors_1.graphQLResultHasProtocolErrors)(result);
648
- if (hasErrors && errorPolicy === "none") {
649
- throw new errors_1.CombinedGraphQLErrors(result.errors);
658
+ const result = {
659
+ data: rawResult.data ?? undefined,
660
+ };
661
+ if ((0, utilities_4.graphQLResultHasError)(rawResult)) {
662
+ result.error = new errors_1.CombinedGraphQLErrors(rawResult.errors);
663
+ }
664
+ else if ((0, errors_1.graphQLResultHasProtocolErrors)(rawResult)) {
665
+ result.error = rawResult.extensions[errors_2.PROTOCOL_ERRORS_SYMBOL];
666
+ // Don't emit protocol errors added by HttpLink
667
+ delete rawResult.extensions[errors_2.PROTOCOL_ERRORS_SYMBOL];
668
+ }
669
+ if (rawResult.extensions &&
670
+ Object.keys(rawResult.extensions).length) {
671
+ result.extensions = rawResult.extensions;
650
672
  }
651
- if (hasProtocolErrors) {
652
- // `errorPolicy` is a mechanism for handling GraphQL errors, according
653
- // to our documentation, so we throw protocol errors regardless of the
654
- // set error policy.
655
- throw result.extensions[errors_2.PROTOCOL_ERRORS_SYMBOL];
673
+ if (result.error && errorPolicy === "none") {
674
+ result.data = undefined;
656
675
  }
657
676
  if (errorPolicy === "ignore") {
658
- delete result.errors;
677
+ delete result.error;
659
678
  }
660
679
  return result;
661
680
  }), (0, rxjs_1.catchError)((error) => {
662
- throw maybeWrapError(error);
663
- }));
681
+ if (errorPolicy === "ignore") {
682
+ return (0, rxjs_1.of)({ data: undefined });
683
+ }
684
+ return (0, rxjs_1.of)({ data: undefined, error: maybeWrapError(error) });
685
+ }), (0, rxjs_1.filter)((result) => !!(result.data || result.error)));
664
686
  if (this.getDocumentInfo(query).hasClientExports) {
665
687
  const observablePromise = this.localState
666
688
  .addExportedVariables(query, variables, context)
@@ -1031,7 +1053,7 @@ class QueryManager {
1031
1053
  !(0, utilities_3.isFullyUnmaskedOperation)(document) &&
1032
1054
  !this.noCacheWarningsByQueryId.has(operationId)) {
1033
1055
  this.noCacheWarningsByQueryId.add(operationId);
1034
- __DEV__ && invariant_1.invariant.warn(82, (0, utilities_4.getOperationName)(document) ??
1056
+ __DEV__ && invariant_1.invariant.warn(78, (0, utilities_4.getOperationName)(document) ??
1035
1057
  `Unnamed ${operationType ?? "operation"}`);
1036
1058
  }
1037
1059
  }