@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
@@ -21,11 +21,13 @@ const invariant_1 = require("@apollo/client/utilities/invariant");
21
21
  const networkStatus_js_1 = require("./networkStatus.cjs");
22
22
  const ObservableQuery_js_1 = require("./ObservableQuery.cjs");
23
23
  const QueryInfo_js_1 = require("./QueryInfo.cjs");
24
- const { hasOwnProperty } = Object.prototype;
25
- const IGNORE = {};
26
24
  class QueryManager {
27
25
  defaultOptions;
28
26
  client;
27
+ /**
28
+ * The options that were passed to the ApolloClient constructor.
29
+ */
30
+ clientOptions;
29
31
  assumeImmutableResults;
30
32
  documentTransform;
31
33
  ssrMode;
@@ -33,7 +35,6 @@ class QueryManager {
33
35
  dataMasking;
34
36
  localState;
35
37
  queryDeduplication;
36
- clientAwareness = {};
37
38
  /**
38
39
  * Whether to prioritize cache values over network results when
39
40
  * `fetchObservableWithInfo` is called.
@@ -51,7 +52,7 @@ class QueryManager {
51
52
  * All ObservableQueries that currently have at least one subscriber.
52
53
  */
53
54
  obsQueries = new Set();
54
- // Maps from queryId strings to Promise rejection functions for
55
+ // Maps from queryInfo.id strings to Promise rejection functions for
55
56
  // currently active queries and fetches.
56
57
  // Use protected instead of private field so
57
58
  // @apollo/experimental-nextjs-app-support can access type info.
@@ -63,7 +64,7 @@ class QueryManager {
63
64
  this.client = options.client;
64
65
  this.defaultOptions = options.defaultOptions;
65
66
  this.queryDeduplication = options.queryDeduplication;
66
- this.clientAwareness = options.clientAwareness;
67
+ this.clientOptions = options.clientOptions;
67
68
  this.ssrMode = options.ssrMode;
68
69
  this.assumeImmutableResults = options.assumeImmutableResults;
69
70
  this.dataMasking = options.dataMasking;
@@ -96,17 +97,14 @@ class QueryManager {
96
97
  */
97
98
  stop() {
98
99
  this.obsQueries.forEach((oq) => oq.stop());
99
- this.cancelPendingFetches((0, invariant_1.newInvariantError)(82));
100
+ this.cancelPendingFetches((0, invariant_1.newInvariantError)(83));
100
101
  }
101
102
  cancelPendingFetches(error) {
102
103
  this.fetchCancelFns.forEach((cancel) => cancel(error));
103
104
  this.fetchCancelFns.clear();
104
105
  }
105
- 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, }) {
106
- (0, invariant_1.invariant)(mutation, 83);
107
- (0, internal_1.checkDocument)(mutation, graphql_1.OperationTypeNode.MUTATION);
108
- (0, invariant_1.invariant)(fetchPolicy === "network-only" || fetchPolicy === "no-cache", 84);
109
- const mutationId = this.generateMutationId();
106
+ async mutate({ mutation, variables, optimisticResponse, updateQueries, refetchQueries = [], awaitRefetchQueries = false, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }) {
107
+ const queryInfo = new QueryInfo_js_1.QueryInfo(this);
110
108
  mutation = this.cache.transformForLink(this.transform(mutation));
111
109
  const { hasClientExports } = this.getDocumentInfo(mutation);
112
110
  variables = this.getVariables(mutation, variables);
@@ -114,7 +112,7 @@ class QueryManager {
114
112
  if (environment_1.__DEV__) {
115
113
  (0, invariant_1.invariant)(
116
114
  this.localState,
117
- 85,
115
+ 84,
118
116
  (0, internal_1.getOperationName)(mutation, "(anonymous)")
119
117
  );
120
118
  }
@@ -126,18 +124,19 @@ class QueryManager {
126
124
  });
127
125
  }
128
126
  const mutationStoreValue = this.mutationStore &&
129
- (this.mutationStore[mutationId] = {
127
+ (this.mutationStore[queryInfo.id] = {
130
128
  mutation,
131
129
  variables,
132
130
  loading: true,
133
131
  error: null,
134
132
  });
135
133
  const isOptimistic = optimisticResponse &&
136
- this.markMutationOptimistic(optimisticResponse, {
137
- mutationId,
134
+ queryInfo.markMutationOptimistic(optimisticResponse, {
138
135
  document: mutation,
139
136
  variables,
140
- fetchPolicy,
137
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
138
+ 0 /* CacheWriteBehavior.FORBID */
139
+ : 2 /* CacheWriteBehavior.MERGE */,
141
140
  errorPolicy,
142
141
  context,
143
142
  updateQueries,
@@ -167,19 +166,19 @@ class QueryManager {
167
166
  if (errorPolicy === "ignore" && hasErrors) {
168
167
  delete storeResult.errors;
169
168
  }
170
- return (0, rxjs_2.from)(this.markMutationResult({
171
- mutationId,
172
- result: storeResult,
169
+ return (0, rxjs_2.from)(queryInfo.markMutationResult(storeResult, {
173
170
  document: mutation,
174
171
  variables,
175
- fetchPolicy,
172
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
173
+ 0 /* CacheWriteBehavior.FORBID */
174
+ : 2 /* CacheWriteBehavior.MERGE */,
176
175
  errorPolicy,
177
176
  context,
178
177
  update: updateWithProxyFn,
179
178
  updateQueries,
180
179
  awaitRefetchQueries,
181
180
  refetchQueries,
182
- removeOptimistic: isOptimistic ? mutationId : void 0,
181
+ removeOptimistic: isOptimistic ? queryInfo.id : void 0,
183
182
  onQueryUpdated,
184
183
  keepRootFields,
185
184
  }));
@@ -216,7 +215,7 @@ class QueryManager {
216
215
  mutationStoreValue.error = error;
217
216
  }
218
217
  if (isOptimistic) {
219
- this.cache.removeOptimistic(mutationId);
218
+ this.cache.removeOptimistic(queryInfo.id);
220
219
  }
221
220
  this.broadcastQueries();
222
221
  if (errorPolicy === "ignore") {
@@ -230,190 +229,11 @@ class QueryManager {
230
229
  });
231
230
  });
232
231
  }
233
- markMutationResult(mutation, cache = this.cache) {
234
- let { result } = mutation;
235
- const cacheWrites = [];
236
- const skipCache = mutation.fetchPolicy === "no-cache";
237
- if (!skipCache && (0, QueryInfo_js_1.shouldWriteResult)(result, mutation.errorPolicy)) {
238
- if (!(0, internal_1.isExecutionPatchIncrementalResult)(result)) {
239
- cacheWrites.push({
240
- result: result.data,
241
- dataId: "ROOT_MUTATION",
242
- query: mutation.document,
243
- variables: mutation.variables,
244
- });
245
- }
246
- if ((0, internal_1.isExecutionPatchIncrementalResult)(result) &&
247
- (0, internal_1.isNonEmptyArray)(result.incremental)) {
248
- const diff = cache.diff({
249
- id: "ROOT_MUTATION",
250
- // The cache complains if passed a mutation where it expects a
251
- // query, so we transform mutations and subscriptions to queries
252
- // (only once, thanks to this.transformCache).
253
- query: this.getDocumentInfo(mutation.document).asQuery,
254
- variables: mutation.variables,
255
- optimistic: false,
256
- returnPartialData: true,
257
- });
258
- let mergedData;
259
- if (diff.result) {
260
- mergedData = (0, internal_1.mergeIncrementalData)(diff.result, result);
261
- }
262
- if (typeof mergedData !== "undefined") {
263
- // cast the ExecutionPatchResult to FetchResult here since
264
- // ExecutionPatchResult never has `data` when returned from the server
265
- result.data = mergedData;
266
- cacheWrites.push({
267
- result: mergedData,
268
- dataId: "ROOT_MUTATION",
269
- query: mutation.document,
270
- variables: mutation.variables,
271
- });
272
- }
273
- }
274
- const { updateQueries } = mutation;
275
- if (updateQueries) {
276
- this.obsQueries.forEach((observableQuery) => {
277
- const queryName = observableQuery && observableQuery.queryName;
278
- if (!queryName || !hasOwnProperty.call(updateQueries, queryName)) {
279
- return;
280
- }
281
- const updater = updateQueries[queryName];
282
- const { query: document, variables } = observableQuery;
283
- // Read the current query result from the store.
284
- const { result: currentQueryResult, complete } = observableQuery.getCacheDiff({ optimistic: false });
285
- if (complete && currentQueryResult) {
286
- // Run our reducer using the current query result and the mutation result.
287
- const nextQueryResult = updater(currentQueryResult, {
288
- mutationResult: result,
289
- queryName: (document && (0, internal_1.getOperationName)(document)) || void 0,
290
- queryVariables: variables,
291
- });
292
- // Write the modified result back into the store if we got a new result.
293
- if (nextQueryResult) {
294
- cacheWrites.push({
295
- result: nextQueryResult,
296
- dataId: "ROOT_QUERY",
297
- query: document,
298
- variables,
299
- });
300
- }
301
- }
302
- });
303
- }
304
- }
305
- if (cacheWrites.length > 0 ||
306
- (mutation.refetchQueries || "").length > 0 ||
307
- mutation.update ||
308
- mutation.onQueryUpdated ||
309
- mutation.removeOptimistic) {
310
- const results = [];
311
- this.refetchQueries({
312
- updateCache: (cache) => {
313
- if (!skipCache) {
314
- cacheWrites.forEach((write) => cache.write(write));
315
- }
316
- // If the mutation has some writes associated with it then we need to
317
- // apply those writes to the store by running this reducer again with
318
- // a write action.
319
- const { update } = mutation;
320
- // Determine whether result is a SingleExecutionResult,
321
- // or the final ExecutionPatchResult.
322
- const isFinalResult = !(0, internal_1.isExecutionPatchResult)(result) ||
323
- ((0, internal_1.isExecutionPatchIncrementalResult)(result) && !result.hasNext);
324
- if (update) {
325
- if (!skipCache) {
326
- // Re-read the ROOT_MUTATION data we just wrote into the cache
327
- // (the first cache.write call in the cacheWrites.forEach loop
328
- // above), so field read functions have a chance to run for
329
- // fields within mutation result objects.
330
- const diff = cache.diff({
331
- id: "ROOT_MUTATION",
332
- // The cache complains if passed a mutation where it expects a
333
- // query, so we transform mutations and subscriptions to queries
334
- // (only once, thanks to this.transformCache).
335
- query: this.getDocumentInfo(mutation.document).asQuery,
336
- variables: mutation.variables,
337
- optimistic: false,
338
- returnPartialData: true,
339
- });
340
- if (diff.complete) {
341
- result = { ...result, data: diff.result };
342
- if ("incremental" in result) {
343
- delete result.incremental;
344
- }
345
- if ("hasNext" in result) {
346
- delete result.hasNext;
347
- }
348
- }
349
- }
350
- // If we've received the whole response,
351
- // either a SingleExecutionResult or the final ExecutionPatchResult,
352
- // call the update function.
353
- if (isFinalResult) {
354
- update(cache, result, {
355
- context: mutation.context,
356
- variables: mutation.variables,
357
- });
358
- }
359
- }
360
- // TODO Do this with cache.evict({ id: 'ROOT_MUTATION' }) but make it
361
- // shallow to allow rolling back optimistic evictions.
362
- if (!skipCache && !mutation.keepRootFields && isFinalResult) {
363
- cache.modify({
364
- id: "ROOT_MUTATION",
365
- fields(value, { fieldName, DELETE }) {
366
- return fieldName === "__typename" ? value : DELETE;
367
- },
368
- });
369
- }
370
- },
371
- include: mutation.refetchQueries,
372
- // Write the final mutation.result to the root layer of the cache.
373
- optimistic: false,
374
- // Remove the corresponding optimistic layer at the same time as we
375
- // write the final non-optimistic result.
376
- removeOptimistic: mutation.removeOptimistic,
377
- // Let the caller of client.mutate optionally determine the refetching
378
- // behavior for watched queries after the mutation.update function runs.
379
- // If no onQueryUpdated function was provided for this mutation, pass
380
- // null instead of undefined to disable the default refetching behavior.
381
- onQueryUpdated: mutation.onQueryUpdated || null,
382
- }).forEach((result) => results.push(result));
383
- if (mutation.awaitRefetchQueries || mutation.onQueryUpdated) {
384
- // Returning a promise here makes the mutation await that promise, so we
385
- // include results in that promise's work if awaitRefetchQueries or an
386
- // onQueryUpdated function was specified.
387
- return Promise.all(results).then(() => result);
388
- }
389
- }
390
- return Promise.resolve(result);
391
- }
392
- markMutationOptimistic(optimisticResponse, mutation) {
393
- const data = typeof optimisticResponse === "function" ?
394
- optimisticResponse(mutation.variables, { IGNORE })
395
- : optimisticResponse;
396
- if (data === IGNORE) {
397
- return false;
398
- }
399
- this.cache.recordOptimisticTransaction((cache) => {
400
- try {
401
- this.markMutationResult({
402
- ...mutation,
403
- result: { data },
404
- }, cache);
405
- }
406
- catch (error) {
407
- __DEV__ && invariant_1.invariant.error(error);
408
- }
409
- }, mutation.mutationId);
410
- return true;
411
- }
412
232
  fetchQuery(options, networkStatus) {
413
- const queryInfo = new QueryInfo_js_1.QueryInfo({
414
- queryManager: this,
415
- });
416
- return (0, rxjs_2.lastValueFrom)(this.fetchObservableWithInfo(queryInfo, options, {
233
+ (0, internal_1.checkDocument)(options.query, graphql_1.OperationTypeNode.QUERY);
234
+ // do the rest asynchronously to keep the same rejection timing as
235
+ // checks further in `.mutate`
236
+ return (async () => (0, rxjs_2.lastValueFrom)(this.fetchObservableWithInfo(options, {
417
237
  networkStatus,
418
238
  }).observable.pipe((0, internal_1.filterMap)((value) => {
419
239
  switch (value.kind) {
@@ -428,7 +248,7 @@ class QueryManager {
428
248
  // This default is needed when a `standby` fetch policy is used to avoid
429
249
  // an EmptyError from rejecting this promise.
430
250
  defaultValue: { data: undefined },
431
- });
251
+ }))();
432
252
  }
433
253
  transform(document) {
434
254
  return this.documentTransform.transformDocument(document);
@@ -473,7 +293,11 @@ class QueryManager {
473
293
  };
474
294
  transformCache.set(document, cacheEntry);
475
295
  }
476
- return transformCache.get(document);
296
+ const entry = transformCache.get(document);
297
+ if (entry.violation) {
298
+ throw entry.violation;
299
+ }
300
+ return entry;
477
301
  }
478
302
  getVariables(document, variables) {
479
303
  const defaultVars = this.getDocumentInfo(document).defaultVars;
@@ -503,7 +327,7 @@ class QueryManager {
503
327
  });
504
328
  return observable;
505
329
  }
506
- async query(options) {
330
+ query(options) {
507
331
  const query = this.transform(options.query);
508
332
  return this.fetchQuery({
509
333
  ...options,
@@ -517,18 +341,10 @@ class QueryManager {
517
341
  }),
518
342
  }));
519
343
  }
520
- queryIdCounter = 1;
521
- generateQueryId() {
522
- return String(this.queryIdCounter++);
523
- }
524
344
  requestIdCounter = 1;
525
345
  generateRequestId() {
526
346
  return this.requestIdCounter++;
527
347
  }
528
- mutationIdCounter = 1;
529
- generateMutationId() {
530
- return String(this.mutationIdCounter++);
531
- }
532
348
  clearStore(options = {
533
349
  discardWatches: true,
534
350
  }) {
@@ -537,7 +353,7 @@ class QueryManager {
537
353
  // depend on values that previously existed in the data portion of the
538
354
  // store. So, we cancel the promises and observers that we have issued
539
355
  // so far and not yet resolved (in the case of queries).
540
- this.cancelPendingFetches((0, invariant_1.newInvariantError)(86));
356
+ this.cancelPendingFetches((0, invariant_1.newInvariantError)(85));
541
357
  this.obsQueries.forEach((observableQuery) => {
542
358
  // Set loading to true so listeners don't trigger unless they want
543
359
  // results with partial data.
@@ -607,10 +423,10 @@ class QueryManager {
607
423
  if (!included) {
608
424
  const queryName = queryNames.get(nameOrQueryString);
609
425
  if (queryName) {
610
- __DEV__ && invariant_1.invariant.warn(87, queryName);
426
+ __DEV__ && invariant_1.invariant.warn(86, queryName);
611
427
  }
612
428
  else {
613
- __DEV__ && invariant_1.invariant.warn(88);
429
+ __DEV__ && invariant_1.invariant.warn(87);
614
430
  }
615
431
  }
616
432
  });
@@ -639,7 +455,7 @@ class QueryManager {
639
455
  if (environment_1.__DEV__) {
640
456
  (0, invariant_1.invariant)(
641
457
  !this.getDocumentInfo(query).hasClientExports || this.localState,
642
- 89,
458
+ 88,
643
459
  (0, internal_1.getOperationName)(query, "(anonymous)")
644
460
  );
645
461
  }
@@ -652,21 +468,17 @@ class QueryManager {
652
468
  }))
653
469
  : (0, rxjs_2.of)(variables)).pipe((0, rxjs_2.mergeMap)((variables) => {
654
470
  const { observable, restart: res } = this.getObservableFromLink(query, context, variables, extensions);
471
+ const queryInfo = new QueryInfo_js_1.QueryInfo(this);
655
472
  restart = res;
656
473
  return observable.pipe((0, rxjs_2.map)((rawResult) => {
657
- if (fetchPolicy !== "no-cache") {
658
- // the subscription interface should handle not sending us results we no longer subscribe to.
659
- // XXX I don't think we ever send in an object with errors, but we might in the future...
660
- if ((0, QueryInfo_js_1.shouldWriteResult)(rawResult, errorPolicy)) {
661
- this.cache.write({
662
- query,
663
- result: rawResult.data,
664
- dataId: "ROOT_SUBSCRIPTION",
665
- variables: variables,
666
- });
667
- }
668
- this.broadcastQueries();
669
- }
474
+ queryInfo.markSubscriptionResult(rawResult, {
475
+ document: query,
476
+ variables,
477
+ errorPolicy,
478
+ cacheWriteBehavior: fetchPolicy === "no-cache" ?
479
+ 0 /* CacheWriteBehavior.FORBID */
480
+ : 2 /* CacheWriteBehavior.MERGE */,
481
+ });
670
482
  const result = {
671
483
  data: rawResult.data ?? undefined,
672
484
  };
@@ -726,7 +538,6 @@ class QueryManager {
726
538
  ...this.defaultContext,
727
539
  ...context,
728
540
  queryDeduplication: deduplication,
729
- clientAwareness: this.clientAwareness,
730
541
  },
731
542
  extensions,
732
543
  };
@@ -782,7 +593,7 @@ class QueryManager {
782
593
  const { operation } = (0, internal_1.getOperationDefinition)(query);
783
594
  (0, invariant_1.invariant)(
784
595
  this.localState,
785
- 90,
596
+ 89,
786
597
  operation[0].toUpperCase() + operation.slice(1),
787
598
  operationName ?? "(anonymous)"
788
599
  );
@@ -806,7 +617,7 @@ class QueryManager {
806
617
  })),
807
618
  };
808
619
  }
809
- getResultsFromLink(queryInfo, cacheWriteBehavior, options) {
620
+ getResultsFromLink(options, { queryInfo, cacheWriteBehavior, observableQuery, }) {
810
621
  const requestId = (queryInfo.lastRequestId = this.generateRequestId());
811
622
  const { errorPolicy } = options;
812
623
  // Performing transformForLink here gives this.cache a chance to fill in
@@ -821,26 +632,35 @@ class QueryManager {
821
632
  if (requestId >= queryInfo.lastRequestId) {
822
633
  if (hasErrors && errorPolicy === "none") {
823
634
  queryInfo.resetLastWrite();
824
- queryInfo.observableQuery?.["resetNotifications"]();
635
+ observableQuery?.["resetNotifications"]();
825
636
  // Throwing here effectively calls observer.error.
826
637
  throw new errors_1.CombinedGraphQLErrors(result);
827
638
  }
828
639
  // Use linkDocument rather than queryInfo.document so the
829
640
  // operation/fragments used to write the result are the same as the
830
641
  // ones used to obtain it from the link.
831
- queryInfo.markResult(result, linkDocument, options, cacheWriteBehavior);
642
+ queryInfo.markQueryResult(result, {
643
+ ...options,
644
+ document: linkDocument,
645
+ cacheWriteBehavior,
646
+ });
832
647
  }
833
648
  const aqr = {
834
649
  data: result.data,
835
- dataState: result.data ? "complete" : "empty",
836
- loading: false,
837
- networkStatus: networkStatus_js_1.NetworkStatus.ready,
838
- partial: !result.data,
650
+ ...((0, internal_1.isExecutionPatchResult)(result) && result.hasNext ?
651
+ {
652
+ loading: true,
653
+ networkStatus: networkStatus_js_1.NetworkStatus.streaming,
654
+ dataState: "streaming",
655
+ partial: true,
656
+ }
657
+ : {
658
+ dataState: result.data ? "complete" : "empty",
659
+ loading: false,
660
+ networkStatus: networkStatus_js_1.NetworkStatus.ready,
661
+ partial: !result.data,
662
+ }),
839
663
  };
840
- if ((0, internal_1.isExecutionPatchResult)(result) && result.hasNext) {
841
- aqr.dataState = "streaming";
842
- aqr.partial = true;
843
- }
844
664
  // In the case we start multiple network requests simulatenously, we
845
665
  // want to ensure we properly set `data` if we're reporting on an old
846
666
  // result which will not be caught by the conditional above that ends up
@@ -861,7 +681,7 @@ class QueryManager {
861
681
  // Avoid storing errors from older interrupted queries.
862
682
  if (requestId >= queryInfo.lastRequestId && errorPolicy === "none") {
863
683
  queryInfo.resetLastWrite();
864
- queryInfo.observableQuery?.["resetNotifications"]();
684
+ observableQuery?.["resetNotifications"]();
865
685
  throw error;
866
686
  }
867
687
  const aqr = {
@@ -878,11 +698,11 @@ class QueryManager {
878
698
  return (0, rxjs_2.of)(aqr);
879
699
  }));
880
700
  }
881
- fetchObservableWithInfo(queryInfo, options, {
701
+ fetchObservableWithInfo(options, {
882
702
  // The initial networkStatus for this fetch, most often
883
703
  // NetworkStatus.loading, but also possibly fetchMore, poll, refetch,
884
704
  // or setVariables.
885
- networkStatus = networkStatus_js_1.NetworkStatus.loading, query = options.query, fetchQueryOperator = (x) => x, onCacheHit = () => { }, }) {
705
+ networkStatus = networkStatus_js_1.NetworkStatus.loading, query = options.query, fetchQueryOperator = (x) => x, onCacheHit = () => { }, observableQuery, }) {
886
706
  const variables = this.getVariables(query, options.variables);
887
707
  const defaults = this.defaultOptions.watchQuery;
888
708
  let { fetchPolicy = (defaults && defaults.fetchPolicy) || "cache-first", errorPolicy = (defaults && defaults.errorPolicy) || "none", returnPartialData = false, notifyOnNetworkStatusChange = true, context = {}, } = options;
@@ -899,6 +719,7 @@ class QueryManager {
899
719
  notifyOnNetworkStatusChange,
900
720
  context,
901
721
  });
722
+ const queryInfo = new QueryInfo_js_1.QueryInfo(this, observableQuery);
902
723
  const fromVariables = (variables) => {
903
724
  // Since normalized is always a fresh copy of options, it's safe to
904
725
  // modify its properties here, rather than creating yet another new
@@ -911,24 +732,23 @@ class QueryManager {
911
732
  normalized.refetchWritePolicy !== "merge") ?
912
733
  1 /* CacheWriteBehavior.OVERWRITE */
913
734
  : 2 /* CacheWriteBehavior.MERGE */;
914
- const observableWithInfo = this.fetchQueryByPolicy(queryInfo, normalized, cacheWriteBehavior, onCacheHit);
735
+ const observableWithInfo = this.fetchQueryByPolicy(normalized, { queryInfo, cacheWriteBehavior, onCacheHit, observableQuery });
915
736
  observableWithInfo.observable =
916
737
  observableWithInfo.observable.pipe(fetchQueryOperator);
917
738
  if (
918
739
  // If we're in standby, postpone advancing options.fetchPolicy using
919
740
  // applyNextFetchPolicy.
920
- normalized.fetchPolicy !== "standby" &&
921
- queryInfo.observableQuery) {
922
- queryInfo.observableQuery["applyNextFetchPolicy"]("after-fetch", options);
741
+ normalized.fetchPolicy !== "standby") {
742
+ observableQuery?.["applyNextFetchPolicy"]("after-fetch", options);
923
743
  }
924
744
  return observableWithInfo;
925
745
  };
926
746
  // This cancel function needs to be set before the concast is created,
927
747
  // in case concast creation synchronously cancels the request.
928
748
  const cleanupCancelFn = () => {
929
- this.fetchCancelFns.delete(queryInfo.queryId);
749
+ this.fetchCancelFns.delete(queryInfo.id);
930
750
  };
931
- this.fetchCancelFns.set(queryInfo.queryId, (error) => {
751
+ this.fetchCancelFns.set(queryInfo.id, (error) => {
932
752
  fetchCancelSubject.next({
933
753
  kind: "E",
934
754
  error,
@@ -948,7 +768,7 @@ class QueryManager {
948
768
  if (environment_1.__DEV__) {
949
769
  (0, invariant_1.invariant)(
950
770
  this.localState,
951
- 91,
771
+ 90,
952
772
  (0, internal_1.getOperationName)(normalized.query, "(anonymous)")
953
773
  );
954
774
  }
@@ -1120,7 +940,7 @@ class QueryManager {
1120
940
  !this.noCacheWarningsByCause.has(cause)) {
1121
941
  this.noCacheWarningsByCause.add(cause);
1122
942
  __DEV__ && invariant_1.invariant.warn(
1123
- 92,
943
+ 91,
1124
944
  (0, internal_1.getOperationName)(document, `Unnamed ${operationType ?? "operation"}`)
1125
945
  );
1126
946
  }
@@ -1135,12 +955,13 @@ class QueryManager {
1135
955
  (0, masking_1.maskFragment)(data, fragment, this.cache, fragmentName)
1136
956
  : data;
1137
957
  }
1138
- fetchQueryByPolicy(queryInfo, { query, variables, fetchPolicy, errorPolicy, returnPartialData, context, }, cacheWriteBehavior, onCacheHit) {
1139
- queryInfo.init({
1140
- document: query,
958
+ fetchQueryByPolicy({ query, variables, fetchPolicy, errorPolicy, returnPartialData, context, }, { cacheWriteBehavior, onCacheHit, queryInfo, observableQuery, }) {
959
+ const readCache = () => this.cache.diff({
960
+ query,
1141
961
  variables,
962
+ returnPartialData: true,
963
+ optimistic: true,
1142
964
  });
1143
- const readCache = () => this.cache.diff(queryInfo.getDiffOptions());
1144
965
  const resultsFromCache = (diff, networkStatus) => {
1145
966
  const data = diff.result;
1146
967
  if (environment_1.__DEV__ && !returnPartialData && data !== null) {
@@ -1181,7 +1002,7 @@ class QueryManager {
1181
1002
  if (environment_1.__DEV__) {
1182
1003
  (0, invariant_1.invariant)(
1183
1004
  this.localState,
1184
- 93,
1005
+ 92,
1185
1006
  (0, internal_1.getOperationName)(query, "(anonymous)")
1186
1007
  );
1187
1008
  }
@@ -1211,12 +1032,16 @@ class QueryManager {
1211
1032
  }
1212
1033
  return fromData(data || undefined);
1213
1034
  };
1214
- const resultsFromLink = () => this.getResultsFromLink(queryInfo, cacheWriteBehavior, {
1035
+ const resultsFromLink = () => this.getResultsFromLink({
1215
1036
  query,
1216
1037
  variables,
1217
1038
  context,
1218
1039
  fetchPolicy,
1219
1040
  errorPolicy,
1041
+ }, {
1042
+ cacheWriteBehavior,
1043
+ queryInfo,
1044
+ observableQuery,
1220
1045
  }).pipe(validateDidEmitValue(), (0, rxjs_2.materialize)(), (0, rxjs_2.map)((result) => ({
1221
1046
  ...result,
1222
1047
  source: "network",
@@ -1271,7 +1096,7 @@ function validateDidEmitValue() {
1271
1096
  didEmitValue = true;
1272
1097
  },
1273
1098
  complete() {
1274
- (0, invariant_1.invariant)(didEmitValue, 94);
1099
+ (0, invariant_1.invariant)(didEmitValue, 93);
1275
1100
  },
1276
1101
  });
1277
1102
  }
@@ -1290,7 +1115,6 @@ function isFullyUnmaskedOperation(document) {
1290
1115
  return isUnmasked;
1291
1116
  }
1292
1117
  function addNonReactiveToNamedFragments(document) {
1293
- (0, internal_1.checkDocument)(document);
1294
1118
  return (0, graphql_1.visit)(document, {
1295
1119
  FragmentSpread: (node) => {
1296
1120
  // Do not add `@nonreactive` if the fragment is marked with `@unmask`