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

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 (214) hide show
  1. package/CHANGELOG.md +113 -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 +22 -19
  10. package/__cjs/core/ApolloClient.cjs.map +1 -1
  11. package/__cjs/core/ApolloClient.d.cts +7 -8
  12. package/__cjs/core/ObservableQuery.cjs +15 -11
  13. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  14. package/__cjs/core/ObservableQuery.d.cts +1 -2
  15. package/__cjs/core/QueryInfo.cjs +223 -34
  16. package/__cjs/core/QueryInfo.cjs.map +1 -1
  17. package/__cjs/core/QueryInfo.d.cts +34 -24
  18. package/__cjs/core/QueryManager.cjs +89 -265
  19. package/__cjs/core/QueryManager.cjs.map +1 -1
  20. package/__cjs/core/QueryManager.d.cts +13 -38
  21. package/__cjs/core/index.cjs.map +1 -1
  22. package/__cjs/core/index.d.cts +1 -1
  23. package/__cjs/core/networkStatus.cjs +7 -1
  24. package/__cjs/core/networkStatus.cjs.map +1 -1
  25. package/__cjs/core/networkStatus.d.cts +7 -1
  26. package/__cjs/core/types.d.cts +5 -4
  27. package/__cjs/invariantErrorCodes.cjs +99 -104
  28. package/__cjs/link/batch-http/batchHttpLink.cjs +14 -14
  29. package/__cjs/link/batch-http/batchHttpLink.cjs.map +1 -1
  30. package/__cjs/link/batch-http/batchHttpLink.d.cts +4 -0
  31. package/__cjs/link/batch-http/index.cjs +2 -1
  32. package/__cjs/link/batch-http/index.cjs.map +1 -1
  33. package/__cjs/link/batch-http/index.d.cts +1 -1
  34. package/__cjs/link/client-awareness/ClientAwarenessLink.cjs +45 -0
  35. package/__cjs/link/client-awareness/ClientAwarenessLink.cjs.map +1 -0
  36. package/__cjs/link/client-awareness/ClientAwarenessLink.d.cts +62 -0
  37. package/__cjs/link/client-awareness/index.cjs +6 -0
  38. package/__cjs/link/client-awareness/index.cjs.map +1 -0
  39. package/__cjs/link/client-awareness/index.d.cts +2 -0
  40. package/__cjs/link/core/ApolloLink.cjs +2 -2
  41. package/__cjs/link/core/types.d.cts +1 -6
  42. package/__cjs/link/http/BaseHttpLink.cjs +117 -0
  43. package/__cjs/link/http/BaseHttpLink.cjs.map +1 -0
  44. package/__cjs/link/http/BaseHttpLink.d.cts +6 -0
  45. package/__cjs/link/http/HttpLink.cjs +8 -5
  46. package/__cjs/link/http/HttpLink.cjs.map +1 -1
  47. package/__cjs/link/http/HttpLink.d.cts +16 -4
  48. package/__cjs/link/http/index.cjs +4 -3
  49. package/__cjs/link/http/index.cjs.map +1 -1
  50. package/__cjs/link/http/index.d.cts +2 -2
  51. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
  52. package/__cjs/link/http/selectHttpOptionsAndBody.cjs +6 -5
  53. package/__cjs/link/http/selectHttpOptionsAndBody.cjs.map +1 -1
  54. package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
  55. package/__cjs/link/persisted-queries/index.cjs +7 -5
  56. package/__cjs/link/persisted-queries/index.cjs.map +1 -1
  57. package/__cjs/local-state/LocalState.cjs +0 -8
  58. package/__cjs/local-state/LocalState.cjs.map +1 -1
  59. package/__cjs/masking/maskDefinition.cjs +2 -2
  60. package/__cjs/masking/maskDefinition.cjs.map +1 -1
  61. package/__cjs/masking/maskFragment.cjs +2 -10
  62. package/__cjs/masking/maskFragment.cjs.map +1 -1
  63. package/__cjs/masking/maskOperation.cjs +1 -9
  64. package/__cjs/masking/maskOperation.cjs.map +1 -1
  65. package/__cjs/masking/utils.cjs +0 -8
  66. package/__cjs/masking/utils.cjs.map +1 -1
  67. package/__cjs/masking/utils.d.cts +0 -1
  68. package/__cjs/react/context/ApolloConsumer.cjs +1 -1
  69. package/__cjs/react/context/ApolloContext.cjs +1 -1
  70. package/__cjs/react/context/ApolloProvider.cjs +1 -1
  71. package/__cjs/react/hooks/internal/validateSuspenseHookOptions.cjs +2 -2
  72. package/__cjs/react/hooks/useApolloClient.cjs +1 -1
  73. package/__cjs/react/hooks/useLazyQuery.cjs +3 -4
  74. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  75. package/__cjs/react/hooks/useLazyQuery.d.cts +0 -8
  76. package/__cjs/react/hooks/useLoadableQuery.cjs +2 -2
  77. package/__cjs/react/hooks/useSubscription.cjs +1 -1
  78. package/__cjs/react/hooks/useSyncExternalStore.cjs +1 -1
  79. package/__cjs/react/internal/cache/QueryReference.cjs +1 -1
  80. package/__cjs/react/ssr/prerenderStatic.cjs +2 -2
  81. package/__cjs/testing/core/mocking/mockLink.cjs +4 -4
  82. package/__cjs/utilities/caching/sizes.cjs.map +1 -1
  83. package/__cjs/utilities/caching/sizes.d.cts +6 -0
  84. package/__cjs/utilities/graphql/DocumentTransform.cjs +1 -1
  85. package/__cjs/utilities/index.cjs +1 -16
  86. package/__cjs/utilities/index.cjs.map +1 -1
  87. package/__cjs/utilities/index.d.cts +0 -13
  88. package/__cjs/utilities/internal/checkDocument.cjs +47 -13
  89. package/__cjs/utilities/internal/checkDocument.cjs.map +1 -1
  90. package/__cjs/utilities/internal/checkDocument.d.cts +1 -1
  91. package/__cjs/utilities/internal/getFragmentDefinition.cjs +3 -3
  92. package/__cjs/utilities/internal/getFragmentFromSelection.cjs +1 -1
  93. package/__cjs/utilities/internal/getFragmentQueryDocument.cjs +2 -2
  94. package/__cjs/utilities/internal/getMainDefinition.cjs +1 -1
  95. package/__cjs/utilities/internal/getMemoryInternals.cjs +1 -0
  96. package/__cjs/utilities/internal/getMemoryInternals.cjs.map +1 -1
  97. package/__cjs/utilities/internal/getMemoryInternals.d.cts +0 -1
  98. package/__cjs/utilities/internal/getQueryDefinition.cjs +1 -1
  99. package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs +1 -1
  100. package/__cjs/utilities/internal/shouldInclude.cjs +4 -4
  101. package/__cjs/utilities/internal/valueToObjectRepresentation.cjs +1 -1
  102. package/__cjs/version.cjs +1 -1
  103. package/cache/core/cache.js +1 -1
  104. package/cache/inmemory/entityStore.js +3 -3
  105. package/cache/inmemory/key-extractor.js +1 -1
  106. package/cache/inmemory/policies.js +4 -4
  107. package/cache/inmemory/policies.js.map +1 -1
  108. package/cache/inmemory/readFromStore.js +2 -2
  109. package/cache/inmemory/writeToStore.js +4 -4
  110. package/core/ApolloClient.d.ts +7 -8
  111. package/core/ApolloClient.js +23 -20
  112. package/core/ApolloClient.js.map +1 -1
  113. package/core/ObservableQuery.d.ts +1 -2
  114. package/core/ObservableQuery.js +15 -11
  115. package/core/ObservableQuery.js.map +1 -1
  116. package/core/QueryInfo.d.ts +34 -24
  117. package/core/QueryInfo.js +221 -34
  118. package/core/QueryInfo.js.map +1 -1
  119. package/core/QueryManager.d.ts +13 -38
  120. package/core/QueryManager.js +91 -267
  121. package/core/QueryManager.js.map +1 -1
  122. package/core/index.d.ts +1 -1
  123. package/core/index.js.map +1 -1
  124. package/core/networkStatus.d.ts +7 -1
  125. package/core/networkStatus.js +7 -1
  126. package/core/networkStatus.js.map +1 -1
  127. package/core/types.d.ts +5 -4
  128. package/invariantErrorCodes.js +99 -104
  129. package/legacyEntryPoints/link/client-awareness/client-awareness.cjs +1 -0
  130. package/legacyEntryPoints/link/client-awareness/client-awareness.d.cts +1 -0
  131. package/legacyEntryPoints/link/client-awareness/index.d.ts +1 -0
  132. package/legacyEntryPoints/link/client-awareness/index.js +1 -0
  133. package/link/batch-http/batchHttpLink.d.ts +4 -0
  134. package/link/batch-http/batchHttpLink.js +11 -12
  135. package/link/batch-http/batchHttpLink.js.map +1 -1
  136. package/link/batch-http/index.d.ts +1 -1
  137. package/link/batch-http/index.js +1 -1
  138. package/link/batch-http/index.js.map +1 -1
  139. package/link/client-awareness/ClientAwarenessLink.d.ts +62 -0
  140. package/link/client-awareness/ClientAwarenessLink.js +41 -0
  141. package/link/client-awareness/ClientAwarenessLink.js.map +1 -0
  142. package/link/client-awareness/index.d.ts +2 -0
  143. package/link/client-awareness/index.js +2 -0
  144. package/link/client-awareness/index.js.map +1 -0
  145. package/link/core/ApolloLink.js +2 -2
  146. package/link/core/types.d.ts +1 -6
  147. package/link/http/BaseHttpLink.d.ts +6 -0
  148. package/link/http/BaseHttpLink.js +113 -0
  149. package/link/http/BaseHttpLink.js.map +1 -0
  150. package/link/http/HttpLink.d.ts +16 -4
  151. package/link/http/HttpLink.js +6 -4
  152. package/link/http/HttpLink.js.map +1 -1
  153. package/link/http/index.d.ts +2 -2
  154. package/link/http/index.js +2 -2
  155. package/link/http/index.js.map +1 -1
  156. package/link/http/parseAndCheckHttpResponse.js +1 -1
  157. package/link/http/selectHttpOptionsAndBody.js +6 -5
  158. package/link/http/selectHttpOptionsAndBody.js.map +1 -1
  159. package/link/http/serializeFetchParameter.js +1 -1
  160. package/link/persisted-queries/index.js +7 -5
  161. package/link/persisted-queries/index.js.map +1 -1
  162. package/local-state/LocalState.js +0 -8
  163. package/local-state/LocalState.js.map +1 -1
  164. package/masking/maskDefinition.js +2 -2
  165. package/masking/maskDefinition.js.map +1 -1
  166. package/masking/maskFragment.js +2 -10
  167. package/masking/maskFragment.js.map +1 -1
  168. package/masking/maskOperation.js +1 -9
  169. package/masking/maskOperation.js.map +1 -1
  170. package/masking/utils.d.ts +0 -1
  171. package/masking/utils.js +0 -7
  172. package/masking/utils.js.map +1 -1
  173. package/package.json +7 -1
  174. package/react/context/ApolloConsumer.js +1 -1
  175. package/react/context/ApolloContext.js +1 -1
  176. package/react/context/ApolloProvider.js +1 -1
  177. package/react/hooks/internal/validateSuspenseHookOptions.js +2 -2
  178. package/react/hooks/useApolloClient.js +1 -1
  179. package/react/hooks/useLazyQuery.d.ts +0 -8
  180. package/react/hooks/useLazyQuery.js +3 -4
  181. package/react/hooks/useLazyQuery.js.map +1 -1
  182. package/react/hooks/useLoadableQuery.js +2 -2
  183. package/react/hooks/useSubscription.js +1 -1
  184. package/react/hooks/useSyncExternalStore.js +1 -1
  185. package/react/internal/cache/QueryReference.js +1 -1
  186. package/react/ssr/prerenderStatic.js +2 -2
  187. package/testing/core/mocking/mockLink.js +4 -4
  188. package/utilities/caching/sizes.d.ts +6 -0
  189. package/utilities/caching/sizes.js.map +1 -1
  190. package/utilities/graphql/DocumentTransform.js +1 -1
  191. package/utilities/index.d.ts +0 -13
  192. package/utilities/index.js +0 -12
  193. package/utilities/index.js.map +1 -1
  194. package/utilities/internal/checkDocument.d.ts +1 -1
  195. package/utilities/internal/checkDocument.js +46 -12
  196. package/utilities/internal/checkDocument.js.map +1 -1
  197. package/utilities/internal/getFragmentDefinition.js +3 -3
  198. package/utilities/internal/getFragmentFromSelection.js +1 -1
  199. package/utilities/internal/getFragmentQueryDocument.js +2 -2
  200. package/utilities/internal/getMainDefinition.js +1 -1
  201. package/utilities/internal/getMemoryInternals.d.ts +0 -1
  202. package/utilities/internal/getMemoryInternals.js +1 -0
  203. package/utilities/internal/getMemoryInternals.js.map +1 -1
  204. package/utilities/internal/getQueryDefinition.js +1 -1
  205. package/utilities/internal/removeDirectivesFromDocument.js +1 -1
  206. package/utilities/internal/shouldInclude.js +4 -4
  207. package/utilities/internal/valueToObjectRepresentation.js +1 -1
  208. package/version.js +1 -1
  209. package/__cjs/link/http/createHttpLink.cjs +0 -151
  210. package/__cjs/link/http/createHttpLink.cjs.map +0 -1
  211. package/__cjs/link/http/createHttpLink.d.cts +0 -4
  212. package/link/http/createHttpLink.d.ts +0 -4
  213. package/link/http/createHttpLink.js +0 -143
  214. package/link/http/createHttpLink.js.map +0 -1
@@ -9,16 +9,18 @@ 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;
20
+ /**
21
+ * The options that were passed to the ApolloClient constructor.
22
+ */
23
+ clientOptions;
22
24
  assumeImmutableResults;
23
25
  documentTransform;
24
26
  ssrMode;
@@ -26,7 +28,6 @@ export class QueryManager {
26
28
  dataMasking;
27
29
  localState;
28
30
  queryDeduplication;
29
- clientAwareness = {};
30
31
  /**
31
32
  * Whether to prioritize cache values over network results when
32
33
  * `fetchObservableWithInfo` is called.
@@ -44,7 +45,7 @@ export class QueryManager {
44
45
  * All ObservableQueries that currently have at least one subscriber.
45
46
  */
46
47
  obsQueries = new Set();
47
- // Maps from queryId strings to Promise rejection functions for
48
+ // Maps from queryInfo.id strings to Promise rejection functions for
48
49
  // currently active queries and fetches.
49
50
  // Use protected instead of private field so
50
51
  // @apollo/experimental-nextjs-app-support can access type info.
@@ -56,7 +57,7 @@ export class QueryManager {
56
57
  this.client = options.client;
57
58
  this.defaultOptions = options.defaultOptions;
58
59
  this.queryDeduplication = options.queryDeduplication;
59
- this.clientAwareness = options.clientAwareness;
60
+ this.clientOptions = options.clientOptions;
60
61
  this.ssrMode = options.ssrMode;
61
62
  this.assumeImmutableResults = options.assumeImmutableResults;
62
63
  this.dataMasking = options.dataMasking;
@@ -89,23 +90,20 @@ export class QueryManager {
89
90
  */
90
91
  stop() {
91
92
  this.obsQueries.forEach((oq) => oq.stop());
92
- this.cancelPendingFetches(newInvariantError(82));
93
+ this.cancelPendingFetches(newInvariantError(83));
93
94
  }
94
95
  cancelPendingFetches(error) {
95
96
  this.fetchCancelFns.forEach((cancel) => cancel(error));
96
97
  this.fetchCancelFns.clear();
97
98
  }
98
- 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);
100
- checkDocument(mutation, OperationTypeNode.MUTATION);
101
- invariant(fetchPolicy === "network-only" || fetchPolicy === "no-cache", 84);
102
- const mutationId = this.generateMutationId();
99
+ async mutate({ mutation, variables, optimisticResponse, updateQueries, refetchQueries = [], awaitRefetchQueries = false, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }) {
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,11 @@ 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
+ checkDocument(options.query, OperationTypeNode.QUERY);
223
+ // do the rest asynchronously to keep the same rejection timing as
224
+ // checks further in `.mutate`
225
+ return (async () => lastValueFrom(this.fetchObservableWithInfo(options, {
406
226
  networkStatus,
407
227
  }).observable.pipe(filterMap((value) => {
408
228
  switch (value.kind) {
@@ -417,7 +237,7 @@ export class QueryManager {
417
237
  // This default is needed when a `standby` fetch policy is used to avoid
418
238
  // an EmptyError from rejecting this promise.
419
239
  defaultValue: { data: undefined },
420
- });
240
+ }))();
421
241
  }
422
242
  transform(document) {
423
243
  return this.documentTransform.transformDocument(document);
@@ -462,7 +282,11 @@ export class QueryManager {
462
282
  };
463
283
  transformCache.set(document, cacheEntry);
464
284
  }
465
- return transformCache.get(document);
285
+ const entry = transformCache.get(document);
286
+ if (entry.violation) {
287
+ throw entry.violation;
288
+ }
289
+ return entry;
466
290
  }
467
291
  getVariables(document, variables) {
468
292
  const defaultVars = this.getDocumentInfo(document).defaultVars;
@@ -492,7 +316,7 @@ export class QueryManager {
492
316
  });
493
317
  return observable;
494
318
  }
495
- async query(options) {
319
+ query(options) {
496
320
  const query = this.transform(options.query);
497
321
  return this.fetchQuery({
498
322
  ...options,
@@ -506,18 +330,10 @@ export class QueryManager {
506
330
  }),
507
331
  }));
508
332
  }
509
- queryIdCounter = 1;
510
- generateQueryId() {
511
- return String(this.queryIdCounter++);
512
- }
513
333
  requestIdCounter = 1;
514
334
  generateRequestId() {
515
335
  return this.requestIdCounter++;
516
336
  }
517
- mutationIdCounter = 1;
518
- generateMutationId() {
519
- return String(this.mutationIdCounter++);
520
- }
521
337
  clearStore(options = {
522
338
  discardWatches: true,
523
339
  }) {
@@ -526,7 +342,7 @@ export class QueryManager {
526
342
  // depend on values that previously existed in the data portion of the
527
343
  // store. So, we cancel the promises and observers that we have issued
528
344
  // so far and not yet resolved (in the case of queries).
529
- this.cancelPendingFetches(newInvariantError(86));
345
+ this.cancelPendingFetches(newInvariantError(85));
530
346
  this.obsQueries.forEach((observableQuery) => {
531
347
  // Set loading to true so listeners don't trigger unless they want
532
348
  // results with partial data.
@@ -596,10 +412,10 @@ export class QueryManager {
596
412
  if (!included) {
597
413
  const queryName = queryNames.get(nameOrQueryString);
598
414
  if (queryName) {
599
- __DEV__ && invariant.warn(87, queryName);
415
+ __DEV__ && invariant.warn(86, queryName);
600
416
  }
601
417
  else {
602
- __DEV__ && invariant.warn(88);
418
+ __DEV__ && invariant.warn(87);
603
419
  }
604
420
  }
605
421
  });
@@ -628,7 +444,7 @@ export class QueryManager {
628
444
  if (__DEV__) {
629
445
  invariant(
630
446
  !this.getDocumentInfo(query).hasClientExports || this.localState,
631
- 89,
447
+ 88,
632
448
  getOperationName(query, "(anonymous)")
633
449
  );
634
450
  }
@@ -641,21 +457,17 @@ export class QueryManager {
641
457
  }))
642
458
  : of(variables)).pipe(mergeMap((variables) => {
643
459
  const { observable, restart: res } = this.getObservableFromLink(query, context, variables, extensions);
460
+ const queryInfo = new QueryInfo(this);
644
461
  restart = res;
645
462
  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
- }
463
+ queryInfo.markSubscriptionResult(rawResult, {
464
+ document: query,
465
+ variables,
466
+ errorPolicy,
467
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
468
+ 0 /* CacheWriteBehavior.FORBID */
469
+ : 2 /* CacheWriteBehavior.MERGE */,
470
+ });
659
471
  const result = {
660
472
  data: rawResult.data ?? undefined,
661
473
  };
@@ -715,7 +527,6 @@ export class QueryManager {
715
527
  ...this.defaultContext,
716
528
  ...context,
717
529
  queryDeduplication: deduplication,
718
- clientAwareness: this.clientAwareness,
719
530
  },
720
531
  extensions,
721
532
  };
@@ -771,7 +582,7 @@ export class QueryManager {
771
582
  const { operation } = getOperationDefinition(query);
772
583
  invariant(
773
584
  this.localState,
774
- 90,
585
+ 89,
775
586
  operation[0].toUpperCase() + operation.slice(1),
776
587
  operationName ?? "(anonymous)"
777
588
  );
@@ -795,7 +606,7 @@ export class QueryManager {
795
606
  })),
796
607
  };
797
608
  }
798
- getResultsFromLink(queryInfo, cacheWriteBehavior, options) {
609
+ getResultsFromLink(options, { queryInfo, cacheWriteBehavior, observableQuery, }) {
799
610
  const requestId = (queryInfo.lastRequestId = this.generateRequestId());
800
611
  const { errorPolicy } = options;
801
612
  // Performing transformForLink here gives this.cache a chance to fill in
@@ -810,26 +621,35 @@ export class QueryManager {
810
621
  if (requestId >= queryInfo.lastRequestId) {
811
622
  if (hasErrors && errorPolicy === "none") {
812
623
  queryInfo.resetLastWrite();
813
- queryInfo.observableQuery?.["resetNotifications"]();
624
+ observableQuery?.["resetNotifications"]();
814
625
  // Throwing here effectively calls observer.error.
815
626
  throw new CombinedGraphQLErrors(result);
816
627
  }
817
628
  // Use linkDocument rather than queryInfo.document so the
818
629
  // operation/fragments used to write the result are the same as the
819
630
  // ones used to obtain it from the link.
820
- queryInfo.markResult(result, linkDocument, options, cacheWriteBehavior);
631
+ queryInfo.markQueryResult(result, {
632
+ ...options,
633
+ document: linkDocument,
634
+ cacheWriteBehavior,
635
+ });
821
636
  }
822
637
  const aqr = {
823
638
  data: result.data,
824
- dataState: result.data ? "complete" : "empty",
825
- loading: false,
826
- networkStatus: NetworkStatus.ready,
827
- partial: !result.data,
639
+ ...(isExecutionPatchResult(result) && result.hasNext ?
640
+ {
641
+ loading: true,
642
+ networkStatus: NetworkStatus.streaming,
643
+ dataState: "streaming",
644
+ partial: true,
645
+ }
646
+ : {
647
+ dataState: result.data ? "complete" : "empty",
648
+ loading: false,
649
+ networkStatus: NetworkStatus.ready,
650
+ partial: !result.data,
651
+ }),
828
652
  };
829
- if (isExecutionPatchResult(result) && result.hasNext) {
830
- aqr.dataState = "streaming";
831
- aqr.partial = true;
832
- }
833
653
  // In the case we start multiple network requests simulatenously, we
834
654
  // want to ensure we properly set `data` if we're reporting on an old
835
655
  // result which will not be caught by the conditional above that ends up
@@ -850,7 +670,7 @@ export class QueryManager {
850
670
  // Avoid storing errors from older interrupted queries.
851
671
  if (requestId >= queryInfo.lastRequestId && errorPolicy === "none") {
852
672
  queryInfo.resetLastWrite();
853
- queryInfo.observableQuery?.["resetNotifications"]();
673
+ observableQuery?.["resetNotifications"]();
854
674
  throw error;
855
675
  }
856
676
  const aqr = {
@@ -867,11 +687,11 @@ export class QueryManager {
867
687
  return of(aqr);
868
688
  }));
869
689
  }
870
- fetchObservableWithInfo(queryInfo, options, {
690
+ fetchObservableWithInfo(options, {
871
691
  // The initial networkStatus for this fetch, most often
872
692
  // NetworkStatus.loading, but also possibly fetchMore, poll, refetch,
873
693
  // or setVariables.
874
- networkStatus = NetworkStatus.loading, query = options.query, fetchQueryOperator = (x) => x, onCacheHit = () => { }, }) {
694
+ networkStatus = NetworkStatus.loading, query = options.query, fetchQueryOperator = (x) => x, onCacheHit = () => { }, observableQuery, }) {
875
695
  const variables = this.getVariables(query, options.variables);
876
696
  const defaults = this.defaultOptions.watchQuery;
877
697
  let { fetchPolicy = (defaults && defaults.fetchPolicy) || "cache-first", errorPolicy = (defaults && defaults.errorPolicy) || "none", returnPartialData = false, notifyOnNetworkStatusChange = true, context = {}, } = options;
@@ -888,6 +708,7 @@ export class QueryManager {
888
708
  notifyOnNetworkStatusChange,
889
709
  context,
890
710
  });
711
+ const queryInfo = new QueryInfo(this, observableQuery);
891
712
  const fromVariables = (variables) => {
892
713
  // Since normalized is always a fresh copy of options, it's safe to
893
714
  // modify its properties here, rather than creating yet another new
@@ -900,24 +721,23 @@ export class QueryManager {
900
721
  normalized.refetchWritePolicy !== "merge") ?
901
722
  1 /* CacheWriteBehavior.OVERWRITE */
902
723
  : 2 /* CacheWriteBehavior.MERGE */;
903
- const observableWithInfo = this.fetchQueryByPolicy(queryInfo, normalized, cacheWriteBehavior, onCacheHit);
724
+ const observableWithInfo = this.fetchQueryByPolicy(normalized, { queryInfo, cacheWriteBehavior, onCacheHit, observableQuery });
904
725
  observableWithInfo.observable =
905
726
  observableWithInfo.observable.pipe(fetchQueryOperator);
906
727
  if (
907
728
  // If we're in standby, postpone advancing options.fetchPolicy using
908
729
  // applyNextFetchPolicy.
909
- normalized.fetchPolicy !== "standby" &&
910
- queryInfo.observableQuery) {
911
- queryInfo.observableQuery["applyNextFetchPolicy"]("after-fetch", options);
730
+ normalized.fetchPolicy !== "standby") {
731
+ observableQuery?.["applyNextFetchPolicy"]("after-fetch", options);
912
732
  }
913
733
  return observableWithInfo;
914
734
  };
915
735
  // This cancel function needs to be set before the concast is created,
916
736
  // in case concast creation synchronously cancels the request.
917
737
  const cleanupCancelFn = () => {
918
- this.fetchCancelFns.delete(queryInfo.queryId);
738
+ this.fetchCancelFns.delete(queryInfo.id);
919
739
  };
920
- this.fetchCancelFns.set(queryInfo.queryId, (error) => {
740
+ this.fetchCancelFns.set(queryInfo.id, (error) => {
921
741
  fetchCancelSubject.next({
922
742
  kind: "E",
923
743
  error,
@@ -935,7 +755,7 @@ export class QueryManager {
935
755
  // we deprecated and removed LocalState.
936
756
  if (this.getDocumentInfo(normalized.query).hasClientExports) {
937
757
  if (__DEV__) {
938
- invariant(this.localState, 91, getOperationName(normalized.query, "(anonymous)"));
758
+ invariant(this.localState, 90, getOperationName(normalized.query, "(anonymous)"));
939
759
  }
940
760
  observable = from(this.localState.getExportedVariables({
941
761
  client: this.client,
@@ -1104,7 +924,7 @@ export class QueryManager {
1104
924
  !isFullyUnmaskedOperation(document) &&
1105
925
  !this.noCacheWarningsByCause.has(cause)) {
1106
926
  this.noCacheWarningsByCause.add(cause);
1107
- __DEV__ && invariant.warn(92, getOperationName(document, `Unnamed ${operationType ?? "operation"}`));
927
+ __DEV__ && invariant.warn(91, getOperationName(document, `Unnamed ${operationType ?? "operation"}`));
1108
928
  }
1109
929
  }
1110
930
  return (this.dataMasking ?
@@ -1117,12 +937,13 @@ export class QueryManager {
1117
937
  maskFragment(data, fragment, this.cache, fragmentName)
1118
938
  : data;
1119
939
  }
1120
- fetchQueryByPolicy(queryInfo, { query, variables, fetchPolicy, errorPolicy, returnPartialData, context, }, cacheWriteBehavior, onCacheHit) {
1121
- queryInfo.init({
1122
- document: query,
940
+ fetchQueryByPolicy({ query, variables, fetchPolicy, errorPolicy, returnPartialData, context, }, { cacheWriteBehavior, onCacheHit, queryInfo, observableQuery, }) {
941
+ const readCache = () => this.cache.diff({
942
+ query,
1123
943
  variables,
944
+ returnPartialData: true,
945
+ optimistic: true,
1124
946
  });
1125
- const readCache = () => this.cache.diff(queryInfo.getDiffOptions());
1126
947
  const resultsFromCache = (diff, networkStatus) => {
1127
948
  const data = diff.result;
1128
949
  if (__DEV__ && !returnPartialData && data !== null) {
@@ -1161,7 +982,7 @@ export class QueryManager {
1161
982
  (diff.complete || returnPartialData) &&
1162
983
  this.getDocumentInfo(query).hasForcedResolvers) {
1163
984
  if (__DEV__) {
1164
- invariant(this.localState, 93, getOperationName(query, "(anonymous)"));
985
+ invariant(this.localState, 92, getOperationName(query, "(anonymous)"));
1165
986
  }
1166
987
  onCacheHit();
1167
988
  return from(this.localState.execute({
@@ -1189,12 +1010,16 @@ export class QueryManager {
1189
1010
  }
1190
1011
  return fromData(data || undefined);
1191
1012
  };
1192
- const resultsFromLink = () => this.getResultsFromLink(queryInfo, cacheWriteBehavior, {
1013
+ const resultsFromLink = () => this.getResultsFromLink({
1193
1014
  query,
1194
1015
  variables,
1195
1016
  context,
1196
1017
  fetchPolicy,
1197
1018
  errorPolicy,
1019
+ }, {
1020
+ cacheWriteBehavior,
1021
+ queryInfo,
1022
+ observableQuery,
1198
1023
  }).pipe(validateDidEmitValue(), materialize(), map((result) => ({
1199
1024
  ...result,
1200
1025
  source: "network",
@@ -1248,7 +1073,7 @@ function validateDidEmitValue() {
1248
1073
  didEmitValue = true;
1249
1074
  },
1250
1075
  complete() {
1251
- invariant(didEmitValue, 94);
1076
+ invariant(didEmitValue, 93);
1252
1077
  },
1253
1078
  });
1254
1079
  }
@@ -1267,7 +1092,6 @@ function isFullyUnmaskedOperation(document) {
1267
1092
  return isUnmasked;
1268
1093
  }
1269
1094
  function addNonReactiveToNamedFragments(document) {
1270
- checkDocument(document);
1271
1095
  return visit(document, {
1272
1096
  FragmentSpread: (node) => {
1273
1097
  // Do not add `@nonreactive` if the fragment is marked with `@unmask`