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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/README.md +3 -7
  3. package/__cjs/cache/core/cache.cjs +1 -1
  4. package/__cjs/cache/core/cache.cjs.map +1 -1
  5. package/__cjs/cache/core/cache.d.cts +5 -1
  6. package/__cjs/cache/core/types/Cache.d.cts +25 -0
  7. package/__cjs/cache/index.cjs.map +1 -1
  8. package/__cjs/cache/index.d.cts +1 -1
  9. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  10. package/__cjs/cache/inmemory/inMemoryCache.cjs.map +1 -1
  11. package/__cjs/cache/inmemory/inMemoryCache.d.cts +6 -3
  12. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  13. package/__cjs/cache/inmemory/policies.cjs +4 -4
  14. package/__cjs/cache/inmemory/readFromStore.cjs +477 -49
  15. package/__cjs/cache/inmemory/readFromStore.cjs.map +1 -1
  16. package/__cjs/cache/inmemory/readFromStore.d.cts +11 -2
  17. package/__cjs/cache/inmemory/types.d.cts +10 -1
  18. package/__cjs/cache/inmemory/writeToStore.cjs +5 -9
  19. package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
  20. package/__cjs/core/ApolloClient.cjs +21 -22
  21. package/__cjs/core/ApolloClient.cjs.map +1 -1
  22. package/__cjs/core/ApolloClient.d.cts +44 -10
  23. package/__cjs/core/ObservableQuery.cjs +46 -5
  24. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  25. package/__cjs/core/ObservableQuery.d.cts +12 -1
  26. package/__cjs/core/QueryInfo.cjs +206 -269
  27. package/__cjs/core/QueryInfo.cjs.map +1 -1
  28. package/__cjs/core/QueryInfo.d.cts +20 -4
  29. package/__cjs/core/QueryManager.cjs +52 -28
  30. package/__cjs/core/QueryManager.cjs.map +1 -1
  31. package/__cjs/core/RefetchEventManager.cjs +3 -3
  32. package/__cjs/core/dataStateErrorCache.cjs +12 -0
  33. package/__cjs/core/dataStateErrorCache.cjs.map +1 -0
  34. package/__cjs/core/dataStateErrorCache.d.cts +3 -0
  35. package/__cjs/core/types.d.cts +2 -0
  36. package/__cjs/incremental/handlers/defer20220824.cjs.map +1 -1
  37. package/__cjs/incremental/handlers/defer20220824.d.cts +2 -4
  38. package/__cjs/incremental/handlers/graphql17Alpha9.cjs +46 -15
  39. package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -1
  40. package/__cjs/incremental/handlers/graphql17Alpha9.d.cts +29 -6
  41. package/__cjs/incremental/types.d.cts +55 -4
  42. package/__cjs/invariantErrorCodes.cjs +50 -31
  43. package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
  44. package/__cjs/react/hooks/useBackgroundQuery.d.cts +100 -47
  45. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  46. package/__cjs/react/hooks/useLazyQuery.d.cts +26 -20
  47. package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
  48. package/__cjs/react/hooks/useLoadableQuery.d.cts +23 -15
  49. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  50. package/__cjs/react/hooks/useMutation.d.cts +10 -7
  51. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  52. package/__cjs/react/hooks/useQuery.d.cts +74 -32
  53. package/__cjs/react/hooks/useQueryRefHandlers.cjs.map +1 -1
  54. package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
  55. package/__cjs/react/hooks/useSuspenseQuery.d.cts +83 -34
  56. package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
  57. package/__cjs/react/internal/types.d.cts +14 -3
  58. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  59. package/__cjs/react/query-preloader/createQueryPreloader.d.cts +20 -5
  60. package/__cjs/testing/core/mocking/mockLink.cjs +21 -1
  61. package/__cjs/testing/core/mocking/mockLink.cjs.map +1 -1
  62. package/__cjs/testing/core/mocking/mockLink.d.cts +1 -2
  63. package/__cjs/utilities/caching/sizes.cjs.map +1 -1
  64. package/__cjs/utilities/caching/sizes.d.cts +39 -1
  65. package/__cjs/utilities/internal/StreamArrayState.cjs +27 -0
  66. package/__cjs/utilities/internal/StreamArrayState.cjs.map +1 -0
  67. package/__cjs/utilities/internal/StreamArrayState.d.cts +12 -0
  68. package/__cjs/utilities/internal/addDeferFragmentLabels.cjs +33 -0
  69. package/__cjs/utilities/internal/addDeferFragmentLabels.cjs.map +1 -0
  70. package/__cjs/utilities/internal/addDeferFragmentLabels.d.cts +3 -0
  71. package/__cjs/utilities/internal/constants.cjs +20 -1
  72. package/__cjs/utilities/internal/constants.cjs.map +1 -1
  73. package/__cjs/utilities/internal/constants.d.cts +19 -0
  74. package/__cjs/utilities/internal/getDirectiveArgValue.cjs +19 -0
  75. package/__cjs/utilities/internal/getDirectiveArgValue.cjs.map +1 -0
  76. package/__cjs/utilities/internal/getDirectiveArgValue.d.cts +9 -0
  77. package/__cjs/utilities/internal/getFragmentFromSelection.cjs +1 -1
  78. package/__cjs/utilities/internal/getFragmentFromSelection.cjs.map +1 -1
  79. package/__cjs/utilities/internal/getMemoryInternals.cjs +6 -0
  80. package/__cjs/utilities/internal/getMemoryInternals.cjs.map +1 -1
  81. package/__cjs/utilities/internal/getMemoryInternals.d.cts +4 -0
  82. package/__cjs/utilities/internal/index.cjs +13 -4
  83. package/__cjs/utilities/internal/index.cjs.map +1 -1
  84. package/__cjs/utilities/internal/index.d.cts +7 -3
  85. package/__cjs/utilities/internal/isDeferredFragment.cjs +19 -4
  86. package/__cjs/utilities/internal/isDeferredFragment.cjs.map +1 -1
  87. package/__cjs/utilities/internal/isDeferredFragment.d.cts +6 -1
  88. package/__cjs/utilities/internal/isStreamField.cjs +37 -0
  89. package/__cjs/utilities/internal/isStreamField.cjs.map +1 -0
  90. package/__cjs/utilities/internal/isStreamField.d.cts +9 -0
  91. package/__cjs/utilities/internal/makeStreamInfoTrie.cjs +17 -0
  92. package/__cjs/utilities/internal/makeStreamInfoTrie.cjs.map +1 -0
  93. package/__cjs/utilities/internal/makeStreamInfoTrie.d.cts +8 -0
  94. package/__cjs/utilities/internal/types/DeferInfo.cjs +3 -0
  95. package/__cjs/utilities/internal/types/DeferInfo.cjs.map +1 -0
  96. package/__cjs/utilities/internal/types/DeferInfo.d.cts +10 -0
  97. package/__cjs/utilities/internal/types/StreamInfoTrie.d.cts +2 -0
  98. package/__cjs/version.cjs +1 -1
  99. package/cache/core/cache.d.ts +5 -1
  100. package/cache/core/cache.js +1 -1
  101. package/cache/core/cache.js.map +1 -1
  102. package/cache/core/types/Cache.d.ts +25 -0
  103. package/cache/core/types/Cache.js.map +1 -1
  104. package/cache/index.d.ts +1 -1
  105. package/cache/index.js.map +1 -1
  106. package/cache/inmemory/entityStore.js +3 -3
  107. package/cache/inmemory/inMemoryCache.d.ts +6 -3
  108. package/cache/inmemory/inMemoryCache.js.map +1 -1
  109. package/cache/inmemory/key-extractor.js +1 -1
  110. package/cache/inmemory/policies.js +4 -4
  111. package/cache/inmemory/readFromStore.d.ts +11 -2
  112. package/cache/inmemory/readFromStore.js +478 -50
  113. package/cache/inmemory/readFromStore.js.map +1 -1
  114. package/cache/inmemory/types.d.ts +11 -2
  115. package/cache/inmemory/types.js.map +1 -1
  116. package/cache/inmemory/writeToStore.js +6 -10
  117. package/cache/inmemory/writeToStore.js.map +1 -1
  118. package/core/ApolloClient.d.ts +44 -10
  119. package/core/ApolloClient.js +21 -22
  120. package/core/ApolloClient.js.map +1 -1
  121. package/core/ObservableQuery.d.ts +12 -1
  122. package/core/ObservableQuery.js +46 -5
  123. package/core/ObservableQuery.js.map +1 -1
  124. package/core/QueryInfo.d.ts +20 -4
  125. package/core/QueryInfo.js +202 -269
  126. package/core/QueryInfo.js.map +1 -1
  127. package/core/QueryManager.js +52 -28
  128. package/core/QueryManager.js.map +1 -1
  129. package/core/RefetchEventManager.js +3 -3
  130. package/core/dataStateErrorCache.d.ts +3 -0
  131. package/core/dataStateErrorCache.js +9 -0
  132. package/core/dataStateErrorCache.js.map +1 -0
  133. package/core/types.d.ts +2 -0
  134. package/core/types.js.map +1 -1
  135. package/incremental/handlers/defer20220824.d.ts +2 -4
  136. package/incremental/handlers/defer20220824.js.map +1 -1
  137. package/incremental/handlers/graphql17Alpha9.d.ts +29 -6
  138. package/incremental/handlers/graphql17Alpha9.js +47 -16
  139. package/incremental/handlers/graphql17Alpha9.js.map +1 -1
  140. package/incremental/types.d.ts +55 -4
  141. package/incremental/types.js.map +1 -1
  142. package/invariantErrorCodes.js +50 -31
  143. package/package.json +1 -1
  144. package/react/hooks/useBackgroundQuery.d.ts +100 -47
  145. package/react/hooks/useBackgroundQuery.js.map +1 -1
  146. package/react/hooks/useLazyQuery.d.ts +26 -20
  147. package/react/hooks/useLazyQuery.js.map +1 -1
  148. package/react/hooks/useLoadableQuery.d.ts +23 -15
  149. package/react/hooks/useLoadableQuery.js.map +1 -1
  150. package/react/hooks/useMutation.d.ts +10 -7
  151. package/react/hooks/useMutation.js.map +1 -1
  152. package/react/hooks/useQuery.d.ts +74 -32
  153. package/react/hooks/useQuery.js.map +1 -1
  154. package/react/hooks/useQueryRefHandlers.js.map +1 -1
  155. package/react/hooks/useSuspenseQuery.d.ts +83 -34
  156. package/react/hooks/useSuspenseQuery.js.map +1 -1
  157. package/react/hooks-compiled/useBackgroundQuery.d.ts +100 -47
  158. package/react/hooks-compiled/useBackgroundQuery.js.map +1 -1
  159. package/react/hooks-compiled/useLazyQuery.d.ts +26 -20
  160. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  161. package/react/hooks-compiled/useLoadableQuery.d.ts +23 -15
  162. package/react/hooks-compiled/useLoadableQuery.js.map +1 -1
  163. package/react/hooks-compiled/useMutation.d.ts +10 -7
  164. package/react/hooks-compiled/useMutation.js.map +1 -1
  165. package/react/hooks-compiled/useQuery.d.ts +74 -32
  166. package/react/hooks-compiled/useQuery.js.map +1 -1
  167. package/react/hooks-compiled/useQueryRefHandlers.js.map +1 -1
  168. package/react/hooks-compiled/useSuspenseQuery.d.ts +83 -34
  169. package/react/hooks-compiled/useSuspenseQuery.js.map +1 -1
  170. package/react/internal/cache/QueryReference.js.map +1 -1
  171. package/react/internal/types.d.ts +15 -4
  172. package/react/internal/types.js.map +1 -1
  173. package/react/query-preloader/createQueryPreloader.d.ts +20 -5
  174. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  175. package/testing/core/mocking/mockLink.d.ts +1 -2
  176. package/testing/core/mocking/mockLink.js +21 -1
  177. package/testing/core/mocking/mockLink.js.map +1 -1
  178. package/utilities/caching/sizes.d.ts +39 -1
  179. package/utilities/caching/sizes.js.map +1 -1
  180. package/utilities/internal/StreamArrayState.d.ts +12 -0
  181. package/utilities/internal/StreamArrayState.js +23 -0
  182. package/utilities/internal/StreamArrayState.js.map +1 -0
  183. package/utilities/internal/addDeferFragmentLabels.d.ts +3 -0
  184. package/utilities/internal/addDeferFragmentLabels.js +30 -0
  185. package/utilities/internal/addDeferFragmentLabels.js.map +1 -0
  186. package/utilities/internal/constants.d.ts +19 -0
  187. package/utilities/internal/constants.js +19 -0
  188. package/utilities/internal/constants.js.map +1 -1
  189. package/utilities/internal/getDirectiveArgValue.d.ts +9 -0
  190. package/utilities/internal/getDirectiveArgValue.js +16 -0
  191. package/utilities/internal/getDirectiveArgValue.js.map +1 -0
  192. package/utilities/internal/getFragmentFromSelection.js +1 -1
  193. package/utilities/internal/getFragmentFromSelection.js.map +1 -1
  194. package/utilities/internal/getMemoryInternals.d.ts +4 -0
  195. package/utilities/internal/getMemoryInternals.js +6 -0
  196. package/utilities/internal/getMemoryInternals.js.map +1 -1
  197. package/utilities/internal/index.d.ts +7 -3
  198. package/utilities/internal/index.js +6 -2
  199. package/utilities/internal/index.js.map +1 -1
  200. package/utilities/internal/isDeferredFragment.d.ts +7 -2
  201. package/utilities/internal/isDeferredFragment.js +19 -4
  202. package/utilities/internal/isDeferredFragment.js.map +1 -1
  203. package/utilities/internal/isStreamField.d.ts +9 -0
  204. package/utilities/internal/isStreamField.js +34 -0
  205. package/utilities/internal/isStreamField.js.map +1 -0
  206. package/utilities/internal/makeStreamInfoTrie.d.ts +8 -0
  207. package/utilities/internal/makeStreamInfoTrie.js +14 -0
  208. package/utilities/internal/makeStreamInfoTrie.js.map +1 -0
  209. package/utilities/internal/types/DeferInfo.d.ts +10 -0
  210. package/utilities/internal/types/DeferInfo.js +2 -0
  211. package/utilities/internal/types/DeferInfo.js.map +1 -0
  212. package/utilities/internal/types/StreamInfoTrie.d.ts +2 -0
  213. package/utilities/internal/types/StreamInfoTrie.js.map +1 -1
  214. package/version.js +1 -1
  215. package/__cjs/utilities/internal/collectSiblingFields.cjs +0 -61
  216. package/__cjs/utilities/internal/collectSiblingFields.cjs.map +0 -1
  217. package/__cjs/utilities/internal/collectSiblingFields.d.cts +0 -21
  218. package/utilities/internal/collectSiblingFields.d.ts +0 -21
  219. package/utilities/internal/collectSiblingFields.js +0 -58
  220. package/utilities/internal/collectSiblingFields.js.map +0 -1
@@ -372,7 +372,18 @@ export declare class ObservableQuery<TData = unknown, TVariables extends Operati
372
372
  /**
373
373
  * A function that helps you fetch the next set of results for a [paginated list field](https://www.apollographql.com/docs/react/pagination/core-api/).
374
374
  */
375
- fetchMore<TFetchData = TData, TFetchVars extends OperationVariables = TVariables>(options: ObservableQuery.FetchMoreOptions<TData, TVariables, TFetchData, TFetchVars>): Promise<ApolloClient.QueryResult<TFetchData>>;
375
+ fetchMore<TFetchData = TData, TFetchVars extends OperationVariables = TVariables, TErrorPolicy extends ErrorPolicy = "none">(options: ObservableQuery.FetchMoreOptions<TData, TVariables, TFetchData, TFetchVars> & {
376
+ /**
377
+ * Specifies how the query handles a response that returns both GraphQL errors and partial results.
378
+ *
379
+ * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
380
+ *
381
+ * The default value is `none`, meaning that the query result includes error details but not partial results.
382
+ *
383
+ * @docGroup 1. Operation options
384
+ */
385
+ errorPolicy?: TErrorPolicy;
386
+ }): Promise<ApolloClient.QueryResult<TFetchData, TErrorPolicy>>;
376
387
  /**
377
388
  * A function that enables you to execute a [subscription](https://www.apollographql.com/docs/react/data/subscriptions/), usually to subscribe to specific fields that were included in the query.
378
389
  *
@@ -1,9 +1,16 @@
1
- "use strict";
1
+ "use strict";;
2
+ const {
3
+ __DEV__
4
+ } = require("@apollo/client/utilities/environment");
5
+
2
6
  Object.defineProperty(exports, "__esModule", { value: true });
3
7
  exports.QueryInfo = void 0;
4
8
  const equality_1 = require("@wry/equality");
9
+ const trie_1 = require("@wry/trie");
10
+ const environment_1 = require("@apollo/client/utilities/environment");
5
11
  const internal_1 = require("@apollo/client/utilities/internal");
6
12
  const invariant_1 = require("@apollo/client/utilities/invariant");
13
+ const networkStatus_js_1 = require("./networkStatus.cjs");
7
14
  const IGNORE = {};
8
15
  const destructiveMethodCounts = new WeakMap();
9
16
  function wrapDestructiveCacheMethod(cache, methodName) {
@@ -42,10 +49,10 @@ class QueryInfo {
42
49
  this.observableQuery = observableQuery;
43
50
  this.queryManager = queryManager;
44
51
  // Track how often cache.evict is called, since we want eviction to
45
- // override the feud-stopping logic in the markQueryResult method, by
46
- // causing shouldWrite to return true. Wrapping the cache.evict method
47
- // is a bit of a hack, but it saves us from having to make eviction
48
- // counting an official part of the ApolloCache API.
52
+ // override the write-skipping logic in `shouldWrite`, by causing it to
53
+ // return true. Wrapping the cache.evict method is a bit of a hack, but it
54
+ // saves us from having to make eviction counting an official part of the
55
+ // ApolloCache API.
49
56
  if (!destructiveMethodCounts.has(cache)) {
50
57
  destructiveMethodCounts.set(cache, 0);
51
58
  wrapDestructiveCacheMethod(cache, "evict");
@@ -55,8 +62,14 @@ class QueryInfo {
55
62
  }
56
63
  /**
57
64
  * @internal
58
- * For feud-preventing behaviour, `lastWrite` should be shared by all `QueryInfo` instances of an `ObservableQuery`.
59
- * In the case of a standalone `QueryInfo`, we will keep a local version.
65
+ * Tracks the last result written to the cache so that `shouldWrite` can skip
66
+ * an identical write. Since a `QueryInfo` only ever represents a single
67
+ * network request, this is shared by all `QueryInfo` instances of an
68
+ * `ObservableQuery`. A standalone `QueryInfo` keeps a local version.
69
+ *
70
+ * A network result that was explicitly asked for always takes precedence over
71
+ * what is already cached, so `ObservableQuery.refetch` and polling clear this
72
+ * value before starting their request.
60
73
  *
61
74
  * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
62
75
  */
@@ -72,178 +85,218 @@ class QueryInfo {
72
85
  }
73
86
  shouldWrite(result, variables) {
74
87
  const { lastWrite } = this;
75
- return !(lastWrite &&
88
+ return (!lastWrite ||
76
89
  // If cache.evict has been called since the last time we wrote this
77
90
  // data into the cache, there's a chance writing this result into
78
91
  // the cache will repair what was evicted.
79
- lastWrite.dmCount === destructiveMethodCounts.get(this.cache) &&
80
- (0, equality_1.equal)(variables, lastWrite.variables) &&
81
- (0, equality_1.equal)(result.data, lastWrite.result.data) &&
92
+ lastWrite.dmCount !== destructiveMethodCounts.get(this.cache) ||
93
+ !(0, equality_1.equal)(variables, lastWrite.variables) ||
94
+ !(0, equality_1.equal)(result.data, lastWrite.result.data) ||
82
95
  // We have to compare these values because its possible the final chunk
83
96
  // emitted in the incremental result is just `hasNext: false`. This
84
97
  // ensures we trigger a cache write when we get `isLastChunk: true`.
85
- result.extensions?.[internal_1.streamInfoSymbol] ===
86
- lastWrite.result.extensions?.[internal_1.streamInfoSymbol]);
98
+ lastWrite.hasNext !== this.hasNext);
87
99
  }
88
100
  get hasNext() {
89
101
  return this.incremental ? this.incremental.hasNext : false;
90
102
  }
103
+ get incrementalHandler() {
104
+ return this.queryManager.incrementalHandler;
105
+ }
91
106
  maybeHandleIncrementalResult(cacheData, incoming, query) {
92
- const { incrementalHandler } = this.queryManager;
93
- if (incrementalHandler.isIncrementalResult(incoming)) {
94
- this.incremental ||= incrementalHandler.startRequest({
107
+ if (this.incrementalHandler.isIncrementalResult(incoming)) {
108
+ this.incremental ||= this.incrementalHandler.startRequest({
95
109
  query,
96
110
  });
97
111
  return this.incremental.handle(cacheData, incoming);
98
112
  }
99
113
  return incoming;
100
114
  }
101
- markQueryResult(incoming, { document: query, variables, errorPolicy, cacheWriteBehavior, returnPartialData, }) {
115
+ markQueryResult(incoming, { document: query, variables, errorPolicy, cacheWriteBehavior, returnPartialData, fetchPolicy, networkStatus, prunePendingDeferFragments: prune, }) {
102
116
  const diffOptions = {
103
117
  query,
104
118
  variables,
105
- returnPartialData: true,
106
119
  optimistic: true,
107
120
  };
108
121
  // Cancel the pending notify timeout (if it exists) to prevent extraneous network
109
122
  // requests. To allow future notify timeouts, diff and dirty are reset as well.
110
123
  this.observableQuery?.["resetNotifications"]();
111
124
  const skipCache = cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */;
112
- const lastDiff = skipCache ? undefined : this.cache.diff(diffOptions);
113
- const incrementalResult = this.maybeHandleIncrementalResult(lastDiff?.result, incoming, query);
125
+ const diff = skipCache ? undefined : (this.getDiff({
126
+ ...diffOptions,
127
+ // We usually request partial data to ensure the network incremental
128
+ // result is merged with all existing data (especially true to
129
+ // maintain @stream arrays with partial list items in the right order
130
+ // or when chunk might otherwise replace a partial non-normalized
131
+ // object), but if we are about to throw away the result anyways due
132
+ // to the error policy (which early returns below), prune any
133
+ // pending boundaries so that CombinedGraphQLErrors contains the
134
+ // right `data` value.
135
+ returnPartialData: errorPolicy !== "none" ||
136
+ !this.incrementalHandler.extractErrors(incoming)?.length,
137
+ }, this.getIncrementalInfo({ prune })));
138
+ const incrementalResult = this.maybeHandleIncrementalResult(diff?.result, incoming, query);
114
139
  let result = {
115
140
  ...incrementalResult,
116
- dataState: incrementalResult.data == null ? "empty"
117
- : this.hasNext ? "streaming"
118
- : "complete",
141
+ dataState: incrementalResult.data == null ? "empty" : "complete",
119
142
  };
120
- if (skipCache) {
121
- return {
122
- ...result,
123
- dataState: result.data == null ? "empty"
124
- // Ww can simplify the checks for no-cache queries because the
125
- // result is purely server driven which means we can assume a
126
- // well-formed GraphQL response. In this case, `streaming` only
127
- // makes sense if we are using the `@defer` directive and there are
128
- // more chunks still streaming (i.e. this.hasNext is true). Purely
129
- // `@stream` queries should always be complete.
130
- : this.hasNext && (0, internal_1.hasDirectives)(["defer"], query) ? "streaming"
131
- : "complete",
132
- };
143
+ const hasPendingDefer = this.incremental
144
+ ?.getPendingWithInfo?.()
145
+ .some((pending) => pending.type === "defer" && !pending.delivered);
146
+ if (hasPendingDefer ||
147
+ // The Defer20220824Handler cannot track pending/completed incremental
148
+ // chunks due to its data format so we naively set dataState to
149
+ // streaming if we are still processing chunks. The only case where
150
+ // streaming is incorrect and should actually be complete is when
151
+ // both a @defer and @stream boundary is present and the @defer chunk
152
+ // has completed before the `@stream` array.
153
+ //
154
+ // Assigning the naive "streaming" value avoids a much more expensive
155
+ // pass over `result.data` that would otherwise need to traverse the
156
+ // selection sets and evaluate the data object at each defer boundary
157
+ // to see if it fulfills the selection set. For such a narrow case where
158
+ // its incorrect on a format that is now outdated is not worth the
159
+ // fix so we are ok with reporting a `streaming` here.
160
+ (!this.incremental?.getPendingWithInfo &&
161
+ this.hasNext &&
162
+ (0, internal_1.hasDirectives)(["defer"], query))) {
163
+ result.dataState = "streaming";
133
164
  }
134
- if (shouldWriteResult(result, errorPolicy)) {
135
- // Using a transaction here so we have a chance to read the result
136
- // back from the cache before the watch callback fires as a result
137
- // of writeQuery, so we can store the new diff quietly and ignore
138
- // it when we receive it redundantly from the watch callback.
139
- this.cache.batch({
140
- onWatchUpdated: (
141
- // all additional options on ObservableQuery.CacheWatchOptions are
142
- // optional so we can use the type here
143
- watch, diff) => {
144
- if (watch.watcher === this.observableQuery) {
145
- // see comment on `lastOwnDiff` for explanation
146
- watch.lastOwnDiff = diff;
147
- }
148
- },
149
- update: (cache) => {
150
- if (this.shouldWrite(result, variables)) {
151
- cache.writeQuery({
152
- query,
153
- data: result.data,
154
- variables,
155
- overwrite: cacheWriteBehavior === 1 /* CacheWriteBehavior.OVERWRITE */,
156
- extensions: result.extensions,
157
- });
158
- this.lastWrite = {
159
- result,
160
- variables,
161
- dmCount: destructiveMethodCounts.get(this.cache),
162
- };
163
- }
164
- else {
165
- // If result is the same as the last result we received from
166
- // the network (and the variables match too), avoid writing
167
- // result into the cache again. The wisdom of skipping this
168
- // cache write is far from obvious, since any cache write
169
- // could be the one that puts the cache back into a desired
170
- // state, fixing corruption or missing data. However, if we
171
- // always write every network result into the cache, we enable
172
- // feuds between queries competing to update the same data in
173
- // incompatible ways, which can lead to an endless cycle of
174
- // cache broadcasts and useless network requests. As with any
175
- // feud, eventually one side must step back from the brink,
176
- // letting the other side(s) have the last word(s). There may
177
- // be other points where we could break this cycle, such as
178
- // silencing the broadcast for cache.writeQuery (not a good
179
- // idea, since it just delays the feud a bit) or somehow
180
- // avoiding the network request that just happened (also bad,
181
- // because the server could return useful new data). All
182
- // options considered, skipping this cache write seems to be
183
- // the least damaging place to break the cycle, because it
184
- // reflects the intuition that we recently wrote this exact
185
- // result into the cache, so the cache *should* already/still
186
- // contain this data. If some other query has clobbered that
187
- // data in the meantime, that's too bad, but there will be no
188
- // winners if every query blindly reverts to its own version
189
- // of the data. This approach also gives the network a chance
190
- // to return new data, which will be written into the cache as
191
- // usual, notifying only those queries that are directly
192
- // affected by the cache updates, as usual. In the future, an
193
- // even more sophisticated cache could perhaps prevent or
194
- // mitigate the clobbering somehow, but that would make this
195
- // particular cache write even less important, and thus
196
- // skipping it would be even safer than it is today.
197
- if (lastDiff && lastDiff.complete) {
198
- // Reuse data from the last good (complete) diff that we
199
- // received, when possible.
200
- result = {
201
- ...result,
202
- data: lastDiff.result,
203
- dataState: "complete",
204
- };
205
- return;
206
- }
207
- // If the previous this.diff was incomplete, fall through to
208
- // re-reading the latest data with cache.diff, below.
209
- }
210
- const diff = cache.diff(diffOptions);
211
- // If we're allowed to write to the cache, and we can read a
212
- // complete result from the cache, update result.data to be the
213
- // result from the cache, rather than the raw network result.
214
- // Set without setDiff to avoid triggering a notify call, since
215
- // we have other ways of notifying for this result.
216
- if (diff.complete) {
217
- result = {
218
- ...result,
219
- data: diff.result,
220
- dataState: "complete",
221
- };
222
- }
223
- else if (this.hasNext && diff.result !== null) {
224
- const isPartial = isStreamingPartial(diff, query, variables);
225
- // If we tolerate partial results always apply `diff.result` to
226
- // ensure we return the result of any transforms in cache read
227
- // functions or custom scalars. If we don't tolerate partial
228
- // results, we only want to apply the diff result if the only hole
229
- // in the data is at a defer boundary (e.g.
230
- // `diff.complete === false && isStreamingPartial === false`)
231
- if (returnPartialData || !isPartial) {
232
- result = {
233
- ...result,
234
- data: diff.result,
235
- dataState: isPartial ? "partial" : "streaming",
236
- };
237
- }
238
- }
239
- },
240
- });
165
+ if (skipCache) {
166
+ return result;
241
167
  }
242
- else {
168
+ if (!shouldWriteResult(result, errorPolicy)) {
243
169
  this.lastWrite = void 0;
170
+ return result;
244
171
  }
172
+ let written = false;
173
+ // Using a transaction here so we have a chance to read the result
174
+ // back from the cache before the watch callback fires as a result
175
+ // of writeQuery, so we can store the new diff quietly and ignore
176
+ // it when we receive it redundantly from the watch callback.
177
+ this.cache.batch({
178
+ onWatchUpdated: (
179
+ // all additional options on ObservableQuery.CacheWatchOptions are
180
+ // optional so we can use the type here
181
+ watch, diff) => {
182
+ if (watch.watcher === this.observableQuery) {
183
+ // see comment on `lastOwnDiff` for explanation
184
+ watch.lastOwnDiff = diff;
185
+ }
186
+ },
187
+ update: (cache) => {
188
+ const shouldWrite = this.shouldWrite(result, variables);
189
+ // If result is the same as the last result we received from
190
+ // the network (and the variables match too), avoid writing
191
+ // result into the cache again. The wisdom of skipping this
192
+ // cache write is far from obvious, since any cache write
193
+ // could be the one that puts the cache back into a desired
194
+ // state, fixing corruption or missing data. However, if we
195
+ // always write every network result into the cache, we enable
196
+ // feuds between queries competing to update the same data in
197
+ // incompatible ways, which can lead to an endless cycle of
198
+ // cache broadcasts and useless network requests. As with any
199
+ // feud, eventually one side must step back from the brink,
200
+ // letting the other side(s) have the last word(s). There may
201
+ // be other points where we could break this cycle, such as
202
+ // silencing the broadcast for cache.writeQuery (not a good
203
+ // idea, since it just delays the feud a bit) or somehow
204
+ // avoiding the network request that just happened (also bad,
205
+ // because the server could return useful new data). All
206
+ // options considered, skipping this cache write seems to be
207
+ // the least damaging place to break the cycle, because it
208
+ // reflects the intuition that we recently wrote this exact
209
+ // result into the cache, so the cache *should* already/still
210
+ // contain this data. If some other query has clobbered that
211
+ // data in the meantime, that's too bad, but there will be no
212
+ // winners if every query blindly reverts to its own version
213
+ // of the data. This approach also gives the network a chance
214
+ // to return new data, which will be written into the cache as
215
+ // usual, notifying only those queries that are directly
216
+ // affected by the cache updates, as usual. In the future, an
217
+ // even more sophisticated cache could perhaps prevent or
218
+ // mitigate the clobbering somehow, but that would make this
219
+ // particular cache write even less important, and thus
220
+ // skipping it would be even safer than it is today.
221
+ if (shouldWrite) {
222
+ cache.writeQuery({
223
+ query,
224
+ data: result.data,
225
+ variables,
226
+ overwrite: cacheWriteBehavior === 1 /* CacheWriteBehavior.OVERWRITE */,
227
+ extensions: result.extensions,
228
+ });
229
+ this.lastWrite = {
230
+ result,
231
+ variables,
232
+ dmCount: destructiveMethodCounts.get(this.cache),
233
+ hasNext: this.hasNext,
234
+ };
235
+ written = true;
236
+ }
237
+ const { dataState, result: diffResult } = this.getDiff({
238
+ ...diffOptions,
239
+ returnPartialData: returnPartialData &&
240
+ // Never deliver partial data for network-only requests
241
+ (fetchPolicy !== "network-only" ||
242
+ networkStatus === networkStatus_js_1.NetworkStatus.refetch),
243
+ }, this.getIncrementalInfo({ prune }));
244
+ if (dataState === "complete" ||
245
+ dataState === "streaming" ||
246
+ (returnPartialData && dataState === "partial" && shouldWrite)) {
247
+ result = { ...result, data: diffResult, dataState };
248
+ }
249
+ else if (environment_1.__DEV__ &&
250
+ written &&
251
+ // A result that is still streaming is expected to read back
252
+ // incomplete until the remaining chunks arrive.
253
+ !this.hasNext) {
254
+ warnAboutPartialCacheResult(query, result.data,
255
+ // Always show the partial result for debugging, otherwise the user
256
+ // sees `null` when `returnPartialData` is false which isn't helpful
257
+ // for figuring out where the problem is.
258
+ cache.diff({ ...diffOptions, returnPartialData: true }));
259
+ }
260
+ },
261
+ });
245
262
  return result;
246
263
  }
264
+ getIncrementalInfo({ prune }) {
265
+ const pending = this.incremental?.getPendingWithInfo?.() ?? [];
266
+ const streamInfo = this.incremental?.streamInfo;
267
+ const incrementalInfo = { streamInfo };
268
+ // We don't want to deliver stream items or complete defer boundaries
269
+ // for a network-only request if they haven't yet streamed from the
270
+ // network. We record all the still-pending paths so that cache.diff
271
+ // can prune complete defer/stream boundaries at those paths.
272
+ if (prune) {
273
+ for (const item of pending) {
274
+ if (item.type === "defer" && !item.delivered) {
275
+ incrementalInfo.deferInfo ||= new trie_1.Trie(true, () => true);
276
+ incrementalInfo.deferInfo.lookupArray(item.path.concat(item.label || []));
277
+ }
278
+ else if (streamInfo && item.type === "stream") {
279
+ streamInfo.lookupArray(item.path).state.truncate = true;
280
+ }
281
+ }
282
+ }
283
+ return incrementalInfo;
284
+ }
285
+ getDiff(options, incrementalInfo) {
286
+ const diff = this.cache.diff({
287
+ ...options,
288
+ [internal_1.handleIncrementalSymbol]: incrementalInfo,
289
+ });
290
+ if ("dataState" in diff) {
291
+ return diff;
292
+ }
293
+ return {
294
+ ...diff,
295
+ dataState: diff.complete ? "complete"
296
+ : diff.result === null ? "empty"
297
+ : "partial",
298
+ };
299
+ }
247
300
  markMutationResult(incoming, mutation, cache = this.cache) {
248
301
  const cacheWrites = [];
249
302
  const skipCache = mutation.cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */;
@@ -439,6 +492,15 @@ class QueryInfo {
439
492
  }
440
493
  }
441
494
  exports.QueryInfo = QueryInfo;
495
+ function warnAboutPartialCacheResult(query, networkResult, diff) {
496
+ __DEV__ && invariant_1.invariant.warn(
497
+ 89,
498
+ (0, internal_1.getOperationName)(query, "(anonymous)"),
499
+ diff.missing?.missing,
500
+ networkResult,
501
+ diff.result
502
+ );
503
+ }
442
504
  function shouldWriteResult(result, errorPolicy = "none") {
443
505
  const ignoreErrors = errorPolicy === "ignore" || errorPolicy === "all";
444
506
  let writeWithErrors = !(0, internal_1.graphQLResultHasError)(result);
@@ -447,129 +509,4 @@ function shouldWriteResult(result, errorPolicy = "none") {
447
509
  }
448
510
  return writeWithErrors;
449
511
  }
450
- function isStreamingPartial(diff, document, variables) {
451
- if (!diff.missing)
452
- return false;
453
- const fragmentMap = (0, internal_1.createFragmentMap)((0, internal_1.getFragmentDefinitions)(document));
454
- return isPartialInSelectionSet((0, internal_1.getMainDefinition)(document).selectionSet, diff.missing.missing, { fragmentMap, variables });
455
- }
456
- function isPartialInSelectionSet(selectionSet, missingTree, context) {
457
- if (typeof missingTree === "string")
458
- return true;
459
- if (missingTree === undefined)
460
- return false;
461
- const missingKeys = Object.keys(missingTree);
462
- if (missingKeys.length > 0 && missingKeys.every(isArrayIndex)) {
463
- return missingKeys.some((key) => {
464
- if (typeof missingTree[key] === "string")
465
- return true;
466
- return isPartialInSelectionSet(selectionSet, missingTree[key], context);
467
- });
468
- }
469
- const { fragmentMap, variables } = context;
470
- for (const selection of selectionSet.selections) {
471
- if ((0, internal_1.isField)(selection)) {
472
- if ((0, internal_1.isTypenameField)(selection))
473
- continue;
474
- const missing = missingTree[(0, internal_1.resultKeyNameFromField)(selection)];
475
- // If a missing entry is absent, we have a value for this field
476
- if (missing === undefined)
477
- continue;
478
- if (!selection.selectionSet ||
479
- isPartialInSelectionSet(selection.selectionSet, missing, context)) {
480
- return true;
481
- }
482
- }
483
- else {
484
- const fragment = (0, internal_1.getFragmentFromSelection)(selection, fragmentMap);
485
- if (!fragment)
486
- continue;
487
- if ((0, internal_1.isDeferredFragment)(selection, variables)) {
488
- // We need to know the non-deferred fields that might overlap with the
489
- // deferred selection set so that we accurately report the right
490
- // dataState when all the non-deferred fields are satisfied and only the
491
- // selections inside the fragment are missing.
492
- // For example:
493
- //
494
- // {
495
- // recipient {
496
- // name
497
- // }
498
- // ... @defer {
499
- // recipient {
500
- // name
501
- // email
502
- // }
503
- // }
504
- // }
505
- //
506
- // dataState should be `streaming`, not `partial` if `name` is present,
507
- // but `email` is absent since `name` is not deferred.
508
- if (isPartialDeferBoundary(fragment.selectionSet, missingTree, (0, internal_1.collectSiblingFields)(selectionSet, {
509
- ...context,
510
- exclude: selection,
511
- }), fragmentMap)) {
512
- return true;
513
- }
514
- }
515
- else if (isPartialInSelectionSet(fragment.selectionSet, missingTree, context)) {
516
- return true;
517
- }
518
- }
519
- }
520
- return false;
521
- }
522
- function isPartialDeferBoundary(selectionSet, missing, nonDeferredFields, fragmentMap) {
523
- // This flag tracks whether all fields in this selection set should contain
524
- // values or should all be missing. A defer boundary is only partial if some
525
- // fields are missing. The first selection node will set this value and all
526
- // sibling fields should match after that. As soon as we get a mismatch, we
527
- // have a partial defer boundary.
528
- let shouldContainValues;
529
- for (const selection of selectionSet.selections) {
530
- if ((0, internal_1.isField)(selection)) {
531
- if ((0, internal_1.isTypenameField)(selection))
532
- continue;
533
- const name = (0, internal_1.resultKeyNameFromField)(selection);
534
- const nonDeferredField = nonDeferredFields?.[name];
535
- // If this field is not exclusive to this selection set, we don't care
536
- // what the value is as far as the defer boundary is concerned. Ignore it
537
- // and continue checking other siblings.
538
- if (nonDeferredField === true)
539
- continue;
540
- // The value of missing means different things:
541
- // - undefined: The field is fully satisfied (i.e. it has a value for the field)
542
- // - string: the field is fully unsatisfied (no scalar value, or object is
543
- // missing all fields)
544
- // - object: The field has a selection set and is partially satisfied.
545
- const missingField = missing[name];
546
- if (typeof missingField !== "object") {
547
- const hasValue = missingField === undefined;
548
- if (shouldContainValues === undefined) {
549
- shouldContainValues = hasValue;
550
- }
551
- if (hasValue !== shouldContainValues) {
552
- return true;
553
- }
554
- continue;
555
- }
556
- if (selection.selectionSet &&
557
- isPartialDeferBoundary(selection.selectionSet, missingField, nonDeferredField, fragmentMap)) {
558
- return true;
559
- }
560
- }
561
- else {
562
- const fragment = (0, internal_1.getFragmentFromSelection)(selection, fragmentMap);
563
- if (!fragment)
564
- continue;
565
- if (isPartialDeferBoundary(fragment.selectionSet, missing, nonDeferredFields, fragmentMap)) {
566
- return true;
567
- }
568
- }
569
- }
570
- return false;
571
- }
572
- function isArrayIndex(key) {
573
- return /^\d+$/.test(key);
574
- }
575
512
  //# sourceMappingURL=QueryInfo.cjs.map