@apollo/client 4.0.10 → 4.0.12-beta.0

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 (319) hide show
  1. package/CHANGELOG.md +217 -0
  2. package/__cjs/cache/core/cache.cjs +216 -52
  3. package/__cjs/cache/core/cache.cjs.map +1 -1
  4. package/__cjs/cache/core/cache.d.cts +141 -25
  5. package/__cjs/cache/core/types/Cache.d.cts +77 -17
  6. package/__cjs/cache/index.cjs.map +1 -1
  7. package/__cjs/cache/index.d.cts +1 -1
  8. package/__cjs/cache/inmemory/entityStore.cjs +6 -4
  9. package/__cjs/cache/inmemory/entityStore.cjs.map +1 -1
  10. package/__cjs/cache/inmemory/helpers.d.cts +1 -1
  11. package/__cjs/cache/inmemory/inMemoryCache.cjs +62 -1
  12. package/__cjs/cache/inmemory/inMemoryCache.cjs.map +1 -1
  13. package/__cjs/cache/inmemory/inMemoryCache.d.cts +48 -0
  14. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  15. package/__cjs/cache/inmemory/key-extractor.cjs.map +1 -1
  16. package/__cjs/cache/inmemory/policies.cjs +56 -17
  17. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  18. package/__cjs/cache/inmemory/policies.d.cts +30 -6
  19. package/__cjs/cache/inmemory/readFromStore.cjs +3 -3
  20. package/__cjs/cache/inmemory/readFromStore.cjs.map +1 -1
  21. package/__cjs/cache/inmemory/types.d.cts +2 -0
  22. package/__cjs/cache/inmemory/writeToStore.cjs +24 -10
  23. package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
  24. package/__cjs/cache/inmemory/writeToStore.d.cts +2 -1
  25. package/__cjs/core/ApolloClient.cjs +31 -49
  26. package/__cjs/core/ApolloClient.cjs.map +1 -1
  27. package/__cjs/core/ApolloClient.d.cts +159 -18
  28. package/__cjs/core/ObservableQuery.cjs +173 -128
  29. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  30. package/__cjs/core/ObservableQuery.d.cts +2 -0
  31. package/__cjs/core/QueryInfo.cjs +9 -1
  32. package/__cjs/core/QueryInfo.cjs.map +1 -1
  33. package/__cjs/core/QueryInfo.d.cts +2 -1
  34. package/__cjs/core/QueryManager.cjs +47 -25
  35. package/__cjs/core/QueryManager.cjs.map +1 -1
  36. package/__cjs/core/QueryManager.d.cts +18 -2
  37. package/__cjs/incremental/handlers/defer20220824.cjs +44 -19
  38. package/__cjs/incremental/handlers/defer20220824.cjs.map +1 -1
  39. package/__cjs/incremental/handlers/defer20220824.d.cts +15 -7
  40. package/__cjs/incremental/handlers/graphql17Alpha9.cjs +222 -0
  41. package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -0
  42. package/__cjs/incremental/handlers/graphql17Alpha9.d.cts +98 -0
  43. package/__cjs/incremental/handlers/notImplemented.cjs +1 -1
  44. package/__cjs/incremental/handlers/notImplemented.cjs.map +1 -1
  45. package/__cjs/incremental/index.cjs +3 -1
  46. package/__cjs/incremental/index.cjs.map +1 -1
  47. package/__cjs/incremental/index.d.cts +1 -0
  48. package/__cjs/incremental/types.d.cts +9 -0
  49. package/__cjs/invariantErrorCodes.cjs +83 -61
  50. package/__cjs/link/client-awareness/ClientAwarenessLink.cjs +13 -1
  51. package/__cjs/link/client-awareness/ClientAwarenessLink.cjs.map +1 -1
  52. package/__cjs/link/client-awareness/ClientAwarenessLink.d.cts +1 -1
  53. package/__cjs/link/core/ApolloLink.cjs +3 -3
  54. package/__cjs/link/error/index.cjs +1 -1
  55. package/__cjs/link/error/index.cjs.map +1 -1
  56. package/__cjs/link/error/index.d.cts +1 -1
  57. package/__cjs/link/http/checkFetcher.cjs +1 -1
  58. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +2 -2
  59. package/__cjs/link/http/parseAndCheckHttpResponse.cjs.map +1 -1
  60. package/__cjs/link/persisted-queries/index.cjs +2 -2
  61. package/__cjs/link/ws/index.cjs +1 -1
  62. package/__cjs/local-state/LocalState.cjs +28 -14
  63. package/__cjs/local-state/LocalState.cjs.map +1 -1
  64. package/__cjs/local-state/LocalState.d.cts +3 -2
  65. package/__cjs/masking/types.d.cts +2 -1
  66. package/__cjs/react/hooks/internal/useDeepMemo.cjs +2 -0
  67. package/__cjs/react/hooks/internal/useDeepMemo.cjs.map +1 -1
  68. package/__cjs/react/hooks/useBackgroundQuery.cjs +1 -3
  69. package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
  70. package/__cjs/react/hooks/useFragment.cjs +38 -87
  71. package/__cjs/react/hooks/useFragment.cjs.map +1 -1
  72. package/__cjs/react/hooks/useFragment.d.cts +65 -6
  73. package/__cjs/react/hooks/useLazyQuery.cjs +3 -0
  74. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  75. package/__cjs/react/hooks/useMutation.cjs +7 -1
  76. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  77. package/__cjs/react/hooks/useMutation.d.cts +13 -1
  78. package/__cjs/react/hooks/useQuery.cjs +8 -8
  79. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  80. package/__cjs/react/hooks/useQuery.d.cts +1 -1
  81. package/__cjs/react/hooks/useQueryRefHandlers.cjs +1 -3
  82. package/__cjs/react/hooks/useQueryRefHandlers.cjs.map +1 -1
  83. package/__cjs/react/hooks/useReadQuery.cjs +1 -3
  84. package/__cjs/react/hooks/useReadQuery.cjs.map +1 -1
  85. package/__cjs/react/hooks/useSubscription.cjs +1 -1
  86. package/__cjs/react/hooks/useSubscription.cjs.map +1 -1
  87. package/__cjs/react/hooks/useSuspenseFragment.cjs +16 -16
  88. package/__cjs/react/hooks/useSuspenseFragment.cjs.map +1 -1
  89. package/__cjs/react/hooks/useSuspenseFragment.d.cts +33 -7
  90. package/__cjs/react/hooks/useSuspenseQuery.cjs +6 -4
  91. package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
  92. package/__cjs/react/internal/cache/FragmentReference.cjs +3 -22
  93. package/__cjs/react/internal/cache/FragmentReference.cjs.map +1 -1
  94. package/__cjs/react/internal/cache/FragmentReference.d.cts +2 -4
  95. package/__cjs/react/internal/cache/QueryReference.cjs +16 -0
  96. package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
  97. package/__cjs/react/internal/cache/QueryReference.d.cts +5 -0
  98. package/__cjs/react/internal/cache/SuspenseCache.cjs.map +1 -1
  99. package/__cjs/react/internal/cache/SuspenseCache.d.cts +1 -1
  100. package/__cjs/react/internal/cache/types.d.cts +2 -2
  101. package/__cjs/react/query-preloader/createQueryPreloader.cjs +41 -1
  102. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  103. package/__cjs/react/ssr/useSSRQuery.cjs +1 -0
  104. package/__cjs/react/ssr/useSSRQuery.cjs.map +1 -1
  105. package/__cjs/utilities/DeepPartial.cjs.map +1 -1
  106. package/__cjs/utilities/DeepPartial.d.cts +1 -1
  107. package/__cjs/utilities/internal/DeepMerger.cjs +36 -4
  108. package/__cjs/utilities/internal/DeepMerger.cjs.map +1 -1
  109. package/__cjs/utilities/internal/DeepMerger.d.cts +20 -4
  110. package/__cjs/utilities/internal/combineLatestBatched.cjs +71 -0
  111. package/__cjs/utilities/internal/combineLatestBatched.cjs.map +1 -0
  112. package/__cjs/utilities/internal/combineLatestBatched.d.cts +14 -0
  113. package/__cjs/utilities/internal/constants.cjs +29 -0
  114. package/__cjs/utilities/internal/constants.cjs.map +1 -0
  115. package/__cjs/utilities/internal/constants.d.cts +26 -0
  116. package/__cjs/utilities/internal/getStoreKeyName.cjs +1 -0
  117. package/__cjs/utilities/internal/getStoreKeyName.cjs.map +1 -1
  118. package/__cjs/utilities/internal/index.cjs +10 -2
  119. package/__cjs/utilities/internal/index.cjs.map +1 -1
  120. package/__cjs/utilities/internal/index.d.cts +4 -0
  121. package/__cjs/utilities/internal/mapObservableFragment.cjs +27 -0
  122. package/__cjs/utilities/internal/mapObservableFragment.cjs.map +1 -0
  123. package/__cjs/utilities/internal/mapObservableFragment.d.cts +3 -0
  124. package/__cjs/utilities/internal/memoize.cjs +2 -2
  125. package/__cjs/utilities/internal/memoize.cjs.map +1 -1
  126. package/__cjs/utilities/internal/memoize.d.cts +2 -1
  127. package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.cjs +61 -0
  128. package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.cjs.map +1 -0
  129. package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.d.cts +11 -0
  130. package/__cjs/utilities/internal/ponyfills/index.cjs +6 -0
  131. package/__cjs/utilities/internal/ponyfills/index.cjs.map +1 -0
  132. package/__cjs/utilities/internal/ponyfills/index.d.cts +3 -0
  133. package/__cjs/utilities/internal/ponyfills/index.react-native.cjs +6 -0
  134. package/__cjs/utilities/internal/ponyfills/index.react-native.cjs.map +1 -0
  135. package/__cjs/utilities/internal/ponyfills/index.react-native.d.cts +2 -0
  136. package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.cjs +3 -0
  137. package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.cjs.map +1 -0
  138. package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.d.cts +24 -0
  139. package/__cjs/version.cjs +1 -1
  140. package/__cjs/version.cjs.map +1 -1
  141. package/cache/core/cache.d.ts +141 -25
  142. package/cache/core/cache.js +219 -55
  143. package/cache/core/cache.js.map +1 -1
  144. package/cache/core/types/Cache.d.ts +77 -17
  145. package/cache/core/types/Cache.js.map +1 -1
  146. package/cache/index.d.ts +1 -1
  147. package/cache/index.js.map +1 -1
  148. package/cache/inmemory/entityStore.js +6 -4
  149. package/cache/inmemory/entityStore.js.map +1 -1
  150. package/cache/inmemory/helpers.d.ts +1 -1
  151. package/cache/inmemory/inMemoryCache.d.ts +48 -0
  152. package/cache/inmemory/inMemoryCache.js +62 -1
  153. package/cache/inmemory/inMemoryCache.js.map +1 -1
  154. package/cache/inmemory/key-extractor.js +1 -1
  155. package/cache/inmemory/key-extractor.js.map +1 -1
  156. package/cache/inmemory/policies.d.ts +30 -6
  157. package/cache/inmemory/policies.js +45 -7
  158. package/cache/inmemory/policies.js.map +1 -1
  159. package/cache/inmemory/readFromStore.js +3 -3
  160. package/cache/inmemory/readFromStore.js.map +1 -1
  161. package/cache/inmemory/types.d.ts +2 -0
  162. package/cache/inmemory/types.js.map +1 -1
  163. package/cache/inmemory/writeToStore.d.ts +2 -1
  164. package/cache/inmemory/writeToStore.js +26 -12
  165. package/cache/inmemory/writeToStore.js.map +1 -1
  166. package/core/ApolloClient.d.ts +159 -18
  167. package/core/ApolloClient.js +32 -50
  168. package/core/ApolloClient.js.map +1 -1
  169. package/core/ObservableQuery.d.ts +2 -0
  170. package/core/ObservableQuery.js +176 -131
  171. package/core/ObservableQuery.js.map +1 -1
  172. package/core/QueryInfo.d.ts +2 -1
  173. package/core/QueryInfo.js +10 -2
  174. package/core/QueryInfo.js.map +1 -1
  175. package/core/QueryManager.d.ts +18 -2
  176. package/core/QueryManager.js +48 -26
  177. package/core/QueryManager.js.map +1 -1
  178. package/incremental/handlers/defer20220824.d.ts +15 -7
  179. package/incremental/handlers/defer20220824.js +44 -19
  180. package/incremental/handlers/defer20220824.js.map +1 -1
  181. package/incremental/handlers/graphql17Alpha9.d.ts +98 -0
  182. package/incremental/handlers/graphql17Alpha9.js +218 -0
  183. package/incremental/handlers/graphql17Alpha9.js.map +1 -0
  184. package/incremental/handlers/notImplemented.js +1 -1
  185. package/incremental/handlers/notImplemented.js.map +1 -1
  186. package/incremental/index.d.ts +1 -0
  187. package/incremental/index.js +3 -2
  188. package/incremental/index.js.map +1 -1
  189. package/incremental/types.d.ts +9 -0
  190. package/incremental/types.js.map +1 -1
  191. package/invariantErrorCodes.js +83 -61
  192. package/legacyEntryPoints/utilities/internal/ponyfills/index.d.ts +1 -0
  193. package/legacyEntryPoints/utilities/internal/ponyfills/index.js +1 -0
  194. package/legacyEntryPoints/utilities/internal/ponyfills/ponyfills.cjs +1 -0
  195. package/legacyEntryPoints/utilities/internal/ponyfills/ponyfills.d.cts +1 -0
  196. package/link/client-awareness/ClientAwarenessLink.d.ts +1 -1
  197. package/link/client-awareness/ClientAwarenessLink.js +13 -1
  198. package/link/client-awareness/ClientAwarenessLink.js.map +1 -1
  199. package/link/core/ApolloLink.js +3 -3
  200. package/link/error/index.d.ts +1 -1
  201. package/link/error/index.js +1 -1
  202. package/link/error/index.js.map +1 -1
  203. package/link/http/checkFetcher.js +1 -1
  204. package/link/http/parseAndCheckHttpResponse.js +2 -2
  205. package/link/http/parseAndCheckHttpResponse.js.map +1 -1
  206. package/link/persisted-queries/index.js +2 -2
  207. package/link/ws/index.js +1 -1
  208. package/local-state/LocalState.d.ts +3 -2
  209. package/local-state/LocalState.js +28 -14
  210. package/local-state/LocalState.js.map +1 -1
  211. package/masking/types.d.ts +2 -1
  212. package/masking/types.js.map +1 -1
  213. package/package.json +15 -1
  214. package/react/hooks/internal/useDeepMemo.js +2 -0
  215. package/react/hooks/internal/useDeepMemo.js.map +1 -1
  216. package/react/hooks/useBackgroundQuery.js +1 -3
  217. package/react/hooks/useBackgroundQuery.js.map +1 -1
  218. package/react/hooks/useFragment.d.ts +65 -6
  219. package/react/hooks/useFragment.js +38 -87
  220. package/react/hooks/useFragment.js.map +1 -1
  221. package/react/hooks/useLazyQuery.js +4 -1
  222. package/react/hooks/useLazyQuery.js.map +1 -1
  223. package/react/hooks/useMutation.d.ts +13 -1
  224. package/react/hooks/useMutation.js +7 -1
  225. package/react/hooks/useMutation.js.map +1 -1
  226. package/react/hooks/useQuery.js +9 -9
  227. package/react/hooks/useQuery.js.map +1 -1
  228. package/react/hooks/useQueryRefHandlers.js +1 -3
  229. package/react/hooks/useQueryRefHandlers.js.map +1 -1
  230. package/react/hooks/useReadQuery.js +1 -3
  231. package/react/hooks/useReadQuery.js.map +1 -1
  232. package/react/hooks/useSubscription.js +1 -1
  233. package/react/hooks/useSubscription.js.map +1 -1
  234. package/react/hooks/useSuspenseFragment.d.ts +33 -7
  235. package/react/hooks/useSuspenseFragment.js +17 -17
  236. package/react/hooks/useSuspenseFragment.js.map +1 -1
  237. package/react/hooks/useSuspenseQuery.js +6 -4
  238. package/react/hooks/useSuspenseQuery.js.map +1 -1
  239. package/react/hooks-compiled/internal/useDeepMemo.js +2 -0
  240. package/react/hooks-compiled/internal/useDeepMemo.js.map +1 -1
  241. package/react/hooks-compiled/internal/useSuspenseHookCacheKey.js +0 -2
  242. package/react/hooks-compiled/internal/useSuspenseHookCacheKey.js.map +1 -1
  243. package/react/hooks-compiled/useBackgroundQuery.js +1 -3
  244. package/react/hooks-compiled/useBackgroundQuery.js.map +1 -1
  245. package/react/hooks-compiled/useFragment.d.ts +65 -6
  246. package/react/hooks-compiled/useFragment.js +110 -88
  247. package/react/hooks-compiled/useFragment.js.map +1 -1
  248. package/react/hooks-compiled/useLazyQuery.js +4 -1
  249. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  250. package/react/hooks-compiled/useLoadableQuery.js +69 -75
  251. package/react/hooks-compiled/useMutation.d.ts +13 -1
  252. package/react/hooks-compiled/useMutation.js +5 -13
  253. package/react/hooks-compiled/useMutation.js.map +1 -1
  254. package/react/hooks-compiled/useQuery.js +53 -61
  255. package/react/hooks-compiled/useQuery.js.map +1 -1
  256. package/react/hooks-compiled/useQueryRefHandlers.js +1 -5
  257. package/react/hooks-compiled/useQueryRefHandlers.js.map +1 -1
  258. package/react/hooks-compiled/useReadQuery.js +77 -14
  259. package/react/hooks-compiled/useReadQuery.js.map +1 -1
  260. package/react/hooks-compiled/useSubscription.js +237 -93
  261. package/react/hooks-compiled/useSubscription.js.map +1 -1
  262. package/react/hooks-compiled/useSuspenseFragment.d.ts +33 -7
  263. package/react/hooks-compiled/useSuspenseFragment.js +17 -17
  264. package/react/hooks-compiled/useSuspenseFragment.js.map +1 -1
  265. package/react/hooks-compiled/useSuspenseQuery.js +7 -9
  266. package/react/hooks-compiled/useSuspenseQuery.js.map +1 -1
  267. package/react/index.compiled.d.ts +1 -1
  268. package/react/index.compiled.js +1 -1
  269. package/react/index.compiled.js.map +1 -1
  270. package/react/internal/cache/FragmentReference.d.ts +2 -4
  271. package/react/internal/cache/FragmentReference.js +3 -22
  272. package/react/internal/cache/FragmentReference.js.map +1 -1
  273. package/react/internal/cache/QueryReference.d.ts +5 -0
  274. package/react/internal/cache/QueryReference.js +16 -0
  275. package/react/internal/cache/QueryReference.js.map +1 -1
  276. package/react/internal/cache/SuspenseCache.d.ts +1 -1
  277. package/react/internal/cache/SuspenseCache.js.map +1 -1
  278. package/react/internal/cache/types.d.ts +2 -2
  279. package/react/internal/cache/types.js.map +1 -1
  280. package/react/query-preloader/createQueryPreloader.js +41 -1
  281. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  282. package/react/ssr/useSSRQuery.js +1 -0
  283. package/react/ssr/useSSRQuery.js.map +1 -1
  284. package/utilities/DeepPartial.d.ts +1 -1
  285. package/utilities/DeepPartial.js.map +1 -1
  286. package/utilities/internal/DeepMerger.d.ts +20 -4
  287. package/utilities/internal/DeepMerger.js +36 -4
  288. package/utilities/internal/DeepMerger.js.map +1 -1
  289. package/utilities/internal/combineLatestBatched.d.ts +14 -0
  290. package/utilities/internal/combineLatestBatched.js +68 -0
  291. package/utilities/internal/combineLatestBatched.js.map +1 -0
  292. package/utilities/internal/constants.d.ts +26 -0
  293. package/utilities/internal/constants.js +26 -0
  294. package/utilities/internal/constants.js.map +1 -0
  295. package/utilities/internal/getStoreKeyName.js +1 -0
  296. package/utilities/internal/getStoreKeyName.js.map +1 -1
  297. package/utilities/internal/index.d.ts +4 -0
  298. package/utilities/internal/index.js +3 -0
  299. package/utilities/internal/index.js.map +1 -1
  300. package/utilities/internal/mapObservableFragment.d.ts +3 -0
  301. package/utilities/internal/mapObservableFragment.js +24 -0
  302. package/utilities/internal/mapObservableFragment.js.map +1 -0
  303. package/utilities/internal/memoize.d.ts +2 -1
  304. package/utilities/internal/memoize.js +2 -2
  305. package/utilities/internal/memoize.js.map +1 -1
  306. package/utilities/internal/ponyfills/FinalizationRegistry.d.ts +11 -0
  307. package/utilities/internal/ponyfills/FinalizationRegistry.js +57 -0
  308. package/utilities/internal/ponyfills/FinalizationRegistry.js.map +1 -0
  309. package/utilities/internal/ponyfills/index.d.ts +3 -0
  310. package/utilities/internal/ponyfills/index.js +3 -0
  311. package/utilities/internal/ponyfills/index.js.map +1 -0
  312. package/utilities/internal/ponyfills/index.react-native.d.ts +2 -0
  313. package/utilities/internal/ponyfills/index.react-native.js +2 -0
  314. package/utilities/internal/ponyfills/index.react-native.js.map +1 -0
  315. package/utilities/internal/types/ExtensionsWithStreamDetails.d.ts +24 -0
  316. package/utilities/internal/types/ExtensionsWithStreamDetails.js +2 -0
  317. package/utilities/internal/types/ExtensionsWithStreamDetails.js.map +1 -0
  318. package/version.js +1 -1
  319. package/version.js.map +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,222 @@
1
1
  # @apollo/client
2
2
 
3
+ ## 4.0.12-beta.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#12884](https://github.com/apollographql/apollo-client/pull/12884) [`d329790`](https://github.com/apollographql/apollo-client/commit/d32979070381f1897c90fb276e25a0c8375cc29a) Thanks [@phryneas](https://github.com/phryneas)! - Ensure that `PreloadedQueryRef` instances are unsubscribed when garbage collected
8
+
9
+ - [#13069](https://github.com/apollographql/apollo-client/pull/13069) [`9cad04a`](https://github.com/apollographql/apollo-client/commit/9cad04a4228a5059ea330ac9d284407a363fc10d) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Truncate @stream arrays only on last chunk by default
10
+
11
+ ## 4.1.0-alpha.9
12
+
13
+ ### Minor Changes
14
+
15
+ - [#13056](https://github.com/apollographql/apollo-client/pull/13056) [`b224efc`](https://github.com/apollographql/apollo-client/commit/b224efc25515370c68b514405762e68a443e4a4a) Thanks [@jerelmiller](https://github.com/jerelmiller)! - `InMemoryCache` no longer filters out explicitly returned `undefined` items from `read` functions for array fields. This now makes it possible to create `read` functions on array fields that return partial data and trigger a fetch for the full list.
16
+
17
+ - [#13058](https://github.com/apollographql/apollo-client/pull/13058) [`121a2cb`](https://github.com/apollographql/apollo-client/commit/121a2cb68820727186ecd74ce1041ef95284682e) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add an `extensions` option to `cache.write`, `cache.writeQuery`, and `client.writeQuery`. This makes `extensions` available in cache `merge` functions which can be accessed with the other merge function options.
18
+
19
+ As a result of this change, any `extensions` returned in GraphQL operations are now available in `merge` in the cache writes for these operations.
20
+
21
+ ### Patch Changes
22
+
23
+ - [#13053](https://github.com/apollographql/apollo-client/pull/13053) [`23ca0ba`](https://github.com/apollographql/apollo-client/commit/23ca0ba895473b397805e6bcc70e0fcf987547c5) Thanks [@phryneas](https://github.com/phryneas)! - Use memoized observable mapping when using `watchFragment`, `useFragment` or `useSuspenseFragment`.
24
+
25
+ ## 4.1.0-alpha.10
26
+
27
+ ### Patch Changes
28
+
29
+ - [#13053](https://github.com/apollographql/apollo-client/pull/13053) [`23ca0ba`](https://github.com/apollographql/apollo-client/commit/23ca0ba895473b397805e6bcc70e0fcf987547c5) Thanks [@phryneas](https://github.com/phryneas)! - Use memoized observable mapping when using `watchFragment`, `useFragment` or `useSuspenseFragment`.
30
+
31
+ ## 4.1.0-alpha.9
32
+
33
+ ### Minor Changes
34
+
35
+ - [#13056](https://github.com/apollographql/apollo-client/pull/13056) [`b224efc`](https://github.com/apollographql/apollo-client/commit/b224efc25515370c68b514405762e68a443e4a4a) Thanks [@jerelmiller](https://github.com/jerelmiller)! - `InMemoryCache` no longer filters out explicitly returned `undefined` items from `read` functions for array fields. This now makes it possible to create `read` functions on array fields that return partial data and trigger a fetch for the full list.
36
+
37
+ ## 4.1.0-alpha.8
38
+
39
+ ### Minor Changes
40
+
41
+ - [#13043](https://github.com/apollographql/apollo-client/pull/13043) [`65e66ca`](https://github.com/apollographql/apollo-client/commit/65e66cafb6828b63d14b64877bbad47af95f66e4) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Support `headers` transport for enhanced client awareness.
42
+
43
+ ## 4.1.0-alpha.7
44
+
45
+ ### Minor Changes
46
+
47
+ - [#13038](https://github.com/apollographql/apollo-client/pull/13038) [`109efe7`](https://github.com/apollographql/apollo-client/commit/109efe7e4380b579c6a577982bd9a6e8c6a53892) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add the `from` option to `readFragment`, `watchFragment`, and `updateFragment`.
48
+
49
+ ## 4.1.0-alpha.6
50
+
51
+ ### Patch Changes
52
+
53
+ - [#13026](https://github.com/apollographql/apollo-client/pull/13026) [`05eee67`](https://github.com/apollographql/apollo-client/commit/05eee67e91b480252923879987534e81d2866aba) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Reduce the number of observables created by `watchFragment` by reusing existing observables as much as possible. This should improve performance when watching the same item in the cache multiple times after a cache update occurs.
54
+
55
+ ## 4.1.0-alpha.5
56
+
57
+ ### Patch Changes
58
+
59
+ - [#13010](https://github.com/apollographql/apollo-client/pull/13010) [`7627000`](https://github.com/apollographql/apollo-client/commit/76270002254b0c6acb18872a39ab180f9f1e4067) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix an issue where errors parsed from incremental chunks in `ErrorLink` might throw when using the `GraphQL17Alpha9Handler`.
60
+
61
+ - [#13010](https://github.com/apollographql/apollo-client/pull/13010) [`7627000`](https://github.com/apollographql/apollo-client/commit/76270002254b0c6acb18872a39ab180f9f1e4067) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Handle `@stream` payloads that send multiple items in the same chunk when using the `Defer20220824Handler`.
62
+
63
+ - [#13010](https://github.com/apollographql/apollo-client/pull/13010) [`7627000`](https://github.com/apollographql/apollo-client/commit/76270002254b0c6acb18872a39ab180f9f1e4067) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Handle an edge case with the `Defer20220824Handler` where an error for a `@stream` item that bubbles to the `@stream` boundary (such as an item returning `null` for a non-null array item) would write items from future chunks to the wrong array index. In these cases, the `@stream` field is no longer processed and future updates to the field are ignored. This prevents runtime errors that TypeScript would otherwise not be able to catch.
64
+
65
+ ## 4.1.0-alpha.4
66
+
67
+ ### Patch Changes
68
+
69
+ - [#13009](https://github.com/apollographql/apollo-client/pull/13009) [`259ae9b`](https://github.com/apollographql/apollo-client/commit/259ae9bafaa8122996b0a52dd99828b2261087e5) Thanks [@phryneas](https://github.com/phryneas)! - Allow `FragmentType` not only to be called as `FragmentType<TData>`, but also as `FragmentType<TypedDocumentNode>`.
70
+
71
+ - [#13012](https://github.com/apollographql/apollo-client/pull/13012) [`44706a2`](https://github.com/apollographql/apollo-client/commit/44706a2e7ae2c977fa917214a1ff5e5fe4a9b3a7) Thanks [@phryneas](https://github.com/phryneas)! - Add helper type `QueryRef.ForQuery<TypedDocumentNode>`
72
+
73
+ ## 4.1.0-alpha.3
74
+
75
+ ### Minor Changes
76
+
77
+ - [#12971](https://github.com/apollographql/apollo-client/pull/12971) [`d11eb40`](https://github.com/apollographql/apollo-client/commit/d11eb40aa41d90ac664705bac01158d58bf55e9b) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add support for `from: null` in `client.watchFragment` and `cache.watchFragment`. When `from` is `null`, the emitted result is:
78
+
79
+ ```ts
80
+ {
81
+ data: null,
82
+ dataState: "complete",
83
+ complete: true,
84
+ }
85
+ ```
86
+
87
+ - [#12971](https://github.com/apollographql/apollo-client/pull/12971) [`d11eb40`](https://github.com/apollographql/apollo-client/commit/d11eb40aa41d90ac664705bac01158d58bf55e9b) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add support for arrays with `useFragment`, `useSuspenseFragment`, and `client.watchFragment`. This allows the ability to use a fragment to watch multiple entities in the cache. Passing an array to `from` will return `data` as an array where each array index corresponds to the index in the `from` array.
88
+
89
+ ```ts
90
+ function MyComponent() {
91
+ const result = useFragment({
92
+ fragment,
93
+ from: [item1, item2, item3],
94
+ });
95
+
96
+ // `data` is an array with 3 items
97
+ console.log(result); // { data: [{...}, {...}, {...}], dataState: "complete", complete: true }
98
+ }
99
+ ```
100
+
101
+ - [#12971](https://github.com/apollographql/apollo-client/pull/12971) [`d11eb40`](https://github.com/apollographql/apollo-client/commit/d11eb40aa41d90ac664705bac01158d58bf55e9b) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add a `getCurrentResult` function to the observable returned by `client.watchFragment` and `cache.watchFragment` that returns the current value for the watched fragment.
102
+
103
+ ```ts
104
+ const observable = client.watchFragment({
105
+ fragment,
106
+ from: { __typename: "Item", id: 1 },
107
+ });
108
+
109
+ console.log(observable.getCurrentResult());
110
+ // {
111
+ // data: {...},
112
+ // dataState: "complete",
113
+ // complete: true,
114
+ // }
115
+ ```
116
+
117
+ ### Patch Changes
118
+
119
+ - [#12971](https://github.com/apollographql/apollo-client/pull/12971) [`d11eb40`](https://github.com/apollographql/apollo-client/commit/d11eb40aa41d90ac664705bac01158d58bf55e9b) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Deduplicate watches created by `useFragment`, `client.watchFragment`, and `cache.watchFragment` that contain the same fragment, variables, and identifier. This should improve performance in situations where a `useFragment` or a `client.watchFragment` is used to watch the same object in multiple places of an application.
120
+
121
+ - [#12982](https://github.com/apollographql/apollo-client/pull/12982) [`5c56b32`](https://github.com/apollographql/apollo-client/commit/5c56b3210a2c03e247ec9e600f1e27eb71df5e96) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Ignore top-level `data` values on subsequent chunks in incremental responses.
122
+
123
+ - [#12982](https://github.com/apollographql/apollo-client/pull/12982) [`5c56b32`](https://github.com/apollographql/apollo-client/commit/5c56b3210a2c03e247ec9e600f1e27eb71df5e96) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix the `Defer20220824Handler.SubsequentResult` type to match the `FormattedSubsequentIncrementalExecutionResult` type in `graphql@17.0.0-alpha.2`.
124
+
125
+ - [#12973](https://github.com/apollographql/apollo-client/pull/12973) [`072da24`](https://github.com/apollographql/apollo-client/commit/072da24a8daec3a646ef0cce30de32f95ea0bb23) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Update the `accept` header used with the `GraphQL17Alpha9Handler` to `multipart/mixed;incrementalSpec=v0.2` to ensure the newest incremental delivery format is requested.
126
+
127
+ - [#12971](https://github.com/apollographql/apollo-client/pull/12971) [`d11eb40`](https://github.com/apollographql/apollo-client/commit/d11eb40aa41d90ac664705bac01158d58bf55e9b) Thanks [@jerelmiller](https://github.com/jerelmiller)! - `DeepPartial<Array<TData>>` now returns `Array<DeepPartial<TData>>` instead of `Array<DeepPartial<TData | undefined>>`.
128
+
129
+ ## 4.1.0-alpha.2
130
+
131
+ ### Minor Changes
132
+
133
+ - [#12959](https://github.com/apollographql/apollo-client/pull/12959) [`556e837`](https://github.com/apollographql/apollo-client/commit/556e83781069d925a7e8f99e49023f6f858c6438) Thanks [@jerelmiller](https://github.com/jerelmiller)! - You can now provide a callback function as the `context` option on the `mutate` function returned by `useMutation`. The callback function is called with the value of the `context` option provided to the `useMutation` hook. This is useful if you'd like to merge the context object provided to the `useMutation` hook with a value provided to the `mutate` function.
134
+
135
+ ```ts
136
+ function MyComponent() {
137
+ const [mutate, result] = useMutation(MUTATION, {
138
+ context: { foo: true },
139
+ });
140
+
141
+ async function runMutation() {
142
+ await mutate({
143
+ // sends context as { foo: true, bar: true }
144
+ context: (hookContext) => ({ ...hookContext, bar: true }),
145
+ });
146
+ }
147
+
148
+ // ...
149
+ }
150
+ ```
151
+
152
+ ### Patch Changes
153
+
154
+ - [#12954](https://github.com/apollographql/apollo-client/pull/12954) [`1c82eaf`](https://github.com/apollographql/apollo-client/commit/1c82eafe4921a9e30128202623be6c5a3d4df803) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Ensure an error is thrown when `@stream` is detected and an `incrementalDelivery` handler is not configured.
155
+
156
+ ## 4.1.0-alpha.1
157
+
158
+ ### Minor Changes
159
+
160
+ - [#12934](https://github.com/apollographql/apollo-client/pull/12934) [`54ab6d9`](https://github.com/apollographql/apollo-client/commit/54ab6d994692dad9f06d3d0b84c84d021d126577) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Don't set the fallback value of a `@client` field to `null` when a `read` function is defined. Instead the `read` function will be called with an `existing` value of `undefined` to allow default arguments to be used to set the returned value.
161
+
162
+ When a `read` function is not defined nor is there a defined resolver for the field, warn and set the value to `null` only in that instance.
163
+
164
+ - [#12934](https://github.com/apollographql/apollo-client/pull/12934) [`54ab6d9`](https://github.com/apollographql/apollo-client/commit/54ab6d994692dad9f06d3d0b84c84d021d126577) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add an abstract `resolvesClientField` function to `ApolloCache` that can be used by caches to tell `LocalState` if it can resolve a `@client` field when a local resolver is not defined.
165
+
166
+ `LocalState` will emit a warning and set a fallback value of `null` when no local resolver is defined and `resolvesClientField` returns `false`, or isn't defined. Returning `true` from `resolvesClientField` signals that a mechanism in the cache will set the field value. In this case, `LocalState` won't set the field value.
167
+
168
+ ### Patch Changes
169
+
170
+ - [#12915](https://github.com/apollographql/apollo-client/pull/12915) [`c97b145`](https://github.com/apollographql/apollo-client/commit/c97b145188d39d754ff098ff399a80cae5b10cc0) Thanks [@phryneas](https://github.com/phryneas)! - Create mechanism to add experimental features to Apollo Client
171
+
172
+ - [#12934](https://github.com/apollographql/apollo-client/pull/12934) [`54ab6d9`](https://github.com/apollographql/apollo-client/commit/54ab6d994692dad9f06d3d0b84c84d021d126577) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Ensure `LocalState` doesn't try to read from the cache when using a `no-cache` fetch policy.
173
+
174
+ - [#12934](https://github.com/apollographql/apollo-client/pull/12934) [`54ab6d9`](https://github.com/apollographql/apollo-client/commit/54ab6d994692dad9f06d3d0b84c84d021d126577) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Warn when using a `no-cache` fetch policy without a local resolver defined. `no-cache` queries do not read or write to the cache which meant `no-cache` queries are silently incomplete when the `@client` field value was handled by a cache `read` function.
175
+
176
+ ## 4.1.0-alpha.0
177
+
178
+ ### Minor Changes
179
+
180
+ - [#12923](https://github.com/apollographql/apollo-client/pull/12923) [`2aa31c7`](https://github.com/apollographql/apollo-client/commit/2aa31c718155e88814551afb14fd7a0035acc57d) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix an issue where deferred payloads that reteurned arrays with fewer items than the original cached array would retain items from the cached array. This change includes `@stream` arrays where stream arrays replace the cached arrays.
181
+
182
+ - [#12926](https://github.com/apollographql/apollo-client/pull/12926) [`c7fba99`](https://github.com/apollographql/apollo-client/commit/c7fba99e16da522fdbc35b9c16cdb8df0dda4c2c) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Support the newer incremental delivery format for the `@defer` directive implemented in `graphql@17.0.0-alpha.9`. Import the `GraphQL17Alpha9Handler` to use the newer incremental delivery format with `@defer`.
183
+
184
+ ```ts
185
+ import { GraphQL17Alpha9Handler } from "@apollo/client/incremental";
186
+
187
+ const client = new ApolloClient({
188
+ // ...
189
+ incrementalHandler: new GraphQL17Alpha9Handler(),
190
+ });
191
+ ```
192
+
193
+ > [!NOTE]
194
+ > In order to use the `GraphQL17Alpha9Handler`, the GraphQL server MUST implement the newer incremental delivery format. You may see errors or unusual behavior if you use the wrong handler. If you are using Apollo Router, continue to use the `Defer20220824Handler` because Apollo Router does not yet support the newer incremental delivery format.
195
+
196
+ - [#12918](https://github.com/apollographql/apollo-client/pull/12918) [`562e219`](https://github.com/apollographql/apollo-client/commit/562e2191a4b38e05edb3da9074e2958db3c7b6b9) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add support for the `@stream` directive on both the `Defer20220824Handler` and the `GraphQL17Alpha2Handler`.
197
+
198
+ > [!NOTE]
199
+ > The implementations of `@stream` differ in the delivery of incremental results between the different GraphQL spec versions. If you upgrading from the older format to the newer format, expect the timing of some incremental results to change.
200
+
201
+ ### Patch Changes
202
+
203
+ - [#12925](https://github.com/apollographql/apollo-client/pull/12925) [`f538a83`](https://github.com/apollographql/apollo-client/commit/f538a83621e1d110286c056dd8e91611dfd9a1d3) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix an issue where calling `fetchMore` with `@defer` or `@stream` would not rerender incremental results as they were streamed.
204
+
205
+ - [#12923](https://github.com/apollographql/apollo-client/pull/12923) [`01cace0`](https://github.com/apollographql/apollo-client/commit/01cace0a6d4faf79e8a4188b93c7d13c4b26d6d4) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Improve the cache data loss warning message when `existing` or `incoming` is an array.
206
+
207
+ ## 4.0.11
208
+
209
+ ### Patch Changes
210
+
211
+ - [#13050](https://github.com/apollographql/apollo-client/pull/13050) [`8020829`](https://github.com/apollographql/apollo-client/commit/8020829d8a3bdb3219a37e8d1f7b89179f721037) Thanks [@phryneas](https://github.com/phryneas)! - Replace usage of `findLast` with more backwards-compatible methods.
212
+
213
+ - [#13049](https://github.com/apollographql/apollo-client/pull/13049) [`05638de`](https://github.com/apollographql/apollo-client/commit/05638deaf598c5bf5d03b82d7deaf57468546229) Thanks [@phryneas](https://github.com/phryneas)! - Fixes an issue where queries starting with `skipToken` or lazy queries from `useLazyQuery` were included in `client.refetchQueries()` before they had been executed for the first time. While generally queries with a `standby` `fetchPolicy` should be included in refetch, these queries never had `variables` passed in, so they should be excluded until they have run once and received their actual variables.
214
+
215
+ These queries are now properly excluded from refetch operations until after their initial execution.
216
+
217
+ This change adds a new hidden option to `client.watchQuery`, `[variablesUnknownSymbol]`, which may be set `true` for queries starting with a `fetchPolicy` of `standby`. It will only be applied when creating the `ObservableQuery` instance and cannot be changed later. This flag indicates that the query's variables are not yet known, and thus it should be excluded from refetch operations until they are.
218
+ **This option is not meant for everyday use and is intended for framework integrations only.**
219
+
3
220
  ## 4.0.10
4
221
 
5
222
  ### Patch Changes
@@ -6,6 +6,8 @@ const {
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.ApolloCache = void 0;
8
8
  const caches_1 = require("@wry/caches");
9
+ const equality_1 = require("@wry/equality");
10
+ const trie_1 = require("@wry/trie");
9
11
  const optimism_1 = require("optimism");
10
12
  const rxjs_1 = require("rxjs");
11
13
  const utilities_1 = require("@apollo/client/utilities");
@@ -21,11 +23,53 @@ class ApolloCache {
21
23
  return null;
22
24
  }
23
25
  // Transactional API
24
- // The batch method is intended to replace/subsume both performTransaction
25
- // and recordOptimisticTransaction, but performTransaction came first, so we
26
- // provide a default batch implementation that's just another way of calling
27
- // performTransaction. Subclasses of ApolloCache (such as InMemoryCache) can
28
- // override the batch method to do more interesting things with its options.
26
+ /**
27
+ * Executes multiple cache operations as a single batch, ensuring that
28
+ * watchers are only notified once after all operations complete. This is
29
+ * useful for improving performance when making multiple cache updates, as it
30
+ * prevents unnecessary re-renders or query refetches between individual
31
+ * operations.
32
+ *
33
+ * The `batch` method supports both optimistic and non-optimistic updates, and
34
+ * provides fine-grained control over which cache layer receives the updates
35
+ * and when watchers are notified.
36
+ *
37
+ * For usage instructions, see [Interacting with cached data: `cache.batch`](https://www.apollographql.com/docs/react/caching/cache-interaction#using-cachebatch).
38
+ *
39
+ * @example
40
+ *
41
+ * ```js
42
+ * cache.batch({
43
+ * update(cache) {
44
+ * cache.writeQuery({
45
+ * query: GET_TODOS,
46
+ * data: { todos: updatedTodos },
47
+ * });
48
+ * cache.evict({ id: "Todo:123" });
49
+ * },
50
+ * });
51
+ * ```
52
+ *
53
+ * @example
54
+ *
55
+ * ```js
56
+ * // Optimistic update with a custom layer ID
57
+ * cache.batch({
58
+ * optimistic: "add-todo-optimistic",
59
+ * update(cache) {
60
+ * cache.modify({
61
+ * fields: {
62
+ * todos(existing = []) {
63
+ * return [...existing, newTodoRef];
64
+ * },
65
+ * },
66
+ * });
67
+ * },
68
+ * });
69
+ * ```
70
+ *
71
+ * @returns The return value of the `update` function.
72
+ */
29
73
  batch(options) {
30
74
  const optimisticId = typeof options.optimistic === "string" ? options.optimistic
31
75
  : options.optimistic === false ? null
@@ -64,6 +108,7 @@ class ApolloCache {
64
108
  optimistic,
65
109
  });
66
110
  }
111
+ fragmentWatches = new trie_1.Trie(true);
67
112
  /**
68
113
  * Watches the cache store of the fragment according to the options specified
69
114
  * and returns an `Observable`. We can subscribe to this
@@ -81,61 +126,159 @@ class ApolloCache {
81
126
  * to optimistic updates.
82
127
  */
83
128
  watchFragment(options) {
84
- const { fragment, fragmentName, from, optimistic = true, ...otherOptions } = options;
129
+ const { fragment, fragmentName, from } = options;
85
130
  const query = this.getFragmentDoc(fragment, fragmentName);
86
- // While our TypeScript types do not allow for `undefined` as a valid
87
- // `from`, its possible `useFragment` gives us an `undefined` since it
88
- // calls` cache.identify` and provides that value to `from`. We are
89
- // adding this fix here however to ensure those using plain JavaScript
90
- // and using `cache.identify` themselves will avoid seeing the obscure
91
- // warning.
92
- const id = typeof from === "undefined" || typeof from === "string" ?
93
- from
94
- : this.identify(from);
95
- if (environment_1.__DEV__) {
96
- const actualFragmentName = fragmentName || (0, internal_1.getFragmentDefinition)(fragment).name.value;
97
- if (!id) {
98
- __DEV__ && invariant_1.invariant.warn(109, actualFragmentName);
131
+ const fromArray = Array.isArray(from) ? from : [from];
132
+ const ids = fromArray.map((value) => {
133
+ // While our TypeScript types do not allow for `undefined` as a valid
134
+ // `from`, its possible `useFragment` gives us an `undefined` since it
135
+ // calls` cache.identify` and provides that value to `from`. We are
136
+ // adding this fix here however to ensure those using plain JavaScript
137
+ // and using `cache.identify` themselves will avoid seeing the obscure
138
+ // warning.
139
+ const id = value == null ? value : this.toCacheId(value);
140
+ if (environment_1.__DEV__) {
141
+ const actualFragmentName = fragmentName || (0, internal_1.getFragmentDefinition)(fragment).name.value;
142
+ if (id === undefined) {
143
+ __DEV__ && invariant_1.invariant.warn(113, actualFragmentName);
144
+ }
99
145
  }
146
+ return id;
147
+ });
148
+ if (!Array.isArray(from)) {
149
+ const observable = this.watchSingleFragment(ids[0], query, options);
150
+ // Unfortunately we forgot to allow for `null` on watchFragment in 4.0
151
+ // when `from` is a single record. As such, we need to fallback to {}
152
+ // when diff.result is null to maintain backwards compatibility. We
153
+ // should plan to change this in v5. We do however support `null` if
154
+ // `from` is explicitly `null`.
155
+ //
156
+ // NOTE: Using `from` with an array will maintain `null` properly
157
+ // without the need for a similar fallback since watchFragment with
158
+ // arrays is new functionality in v4.1.
159
+ return from === null ? observable : ((0, internal_1.mapObservableFragmentMemoized)(observable, Symbol.for("apollo.transform.individualResult"), (result) => ({
160
+ ...result,
161
+ data: result.data ?? {},
162
+ })));
100
163
  }
101
- const diffOptions = {
102
- ...otherOptions,
103
- returnPartialData: true,
104
- id,
105
- query,
106
- optimistic,
107
- };
108
- let latestDiff;
109
- return new rxjs_1.Observable((observer) => {
110
- return this.watch({
111
- ...diffOptions,
112
- immediate: true,
113
- callback: (diff) => {
114
- let data = diff.result;
115
- // TODO: Remove this once `watchFragment` supports `null` as valid
116
- // value emitted
117
- if (data === null) {
118
- data = {};
119
- }
120
- if (
121
- // Always ensure we deliver the first result
122
- latestDiff &&
123
- (0, internal_1.equalByQuery)(query, { data: latestDiff.result }, { data }, options.variables)) {
124
- return;
125
- }
126
- const result = {
164
+ let currentResult;
165
+ function toResult(results) {
166
+ const result = results.reduce((memo, result, idx) => {
167
+ memo.data.push(result.data);
168
+ memo.complete &&= result.complete;
169
+ memo.dataState = memo.complete ? "complete" : "partial";
170
+ if (result.missing) {
171
+ memo.missing ||= {};
172
+ memo.missing[idx] = result.missing;
173
+ }
174
+ return memo;
175
+ }, {
176
+ data: [],
177
+ dataState: "complete",
178
+ complete: true,
179
+ });
180
+ if (!(0, equality_1.equal)(currentResult, result)) {
181
+ currentResult = result;
182
+ }
183
+ return currentResult;
184
+ }
185
+ if (ids.length === 0) {
186
+ return emptyArrayObservable;
187
+ }
188
+ let subscribed = false;
189
+ const observables = ids.map((id) => this.watchSingleFragment(id, query, options));
190
+ const observable = (0, internal_1.combineLatestBatched)(observables).pipe((0, rxjs_1.map)(toResult), (0, rxjs_1.tap)({
191
+ subscribe: () => (subscribed = true),
192
+ unsubscribe: () => (subscribed = false),
193
+ }), (0, rxjs_1.shareReplay)({ bufferSize: 1, refCount: true }));
194
+ return Object.assign(observable, {
195
+ getCurrentResult: () => {
196
+ if (subscribed && currentResult) {
197
+ return currentResult;
198
+ }
199
+ const results = observables.map((observable) => observable.getCurrentResult());
200
+ return toResult(results);
201
+ },
202
+ });
203
+ }
204
+ /**
205
+ * Can be overridden by subclasses to delay calling the provided callback
206
+ * until after all broadcasts have been completed - e.g. in a cache scenario
207
+ * where many watchers are notified in parallel.
208
+ */
209
+ onAfterBroadcast = (cb) => cb();
210
+ watchSingleFragment(id, fragmentQuery, options) {
211
+ if (id === null) {
212
+ return nullObservable;
213
+ }
214
+ const { optimistic = true, variables } = options;
215
+ const cacheKey = [
216
+ fragmentQuery,
217
+ (0, utilities_1.canonicalStringify)({ id, optimistic, variables }),
218
+ ];
219
+ const cacheEntry = this.fragmentWatches.lookupArray(cacheKey);
220
+ if (!cacheEntry.observable) {
221
+ let subscribed = false;
222
+ let currentResult;
223
+ function getNewestResult(diff) {
224
+ const data = diff.result;
225
+ if (!currentResult ||
226
+ !(0, internal_1.equalByQuery)(fragmentQuery, { data: currentResult.data }, { data }, options.variables)) {
227
+ currentResult = {
127
228
  data,
128
229
  dataState: diff.complete ? "complete" : "partial",
129
- complete: !!diff.complete,
230
+ complete: diff.complete,
130
231
  };
131
232
  if (diff.missing) {
132
- result.missing = diff.missing.missing;
233
+ currentResult.missing = diff.missing.missing;
133
234
  }
134
- latestDiff = { ...diff, result: data };
135
- observer.next(result);
235
+ }
236
+ return currentResult;
237
+ }
238
+ const observable = new rxjs_1.Observable((observer) => {
239
+ subscribed = true;
240
+ const cleanup = this.watch({
241
+ variables,
242
+ returnPartialData: true,
243
+ id,
244
+ query: fragmentQuery,
245
+ optimistic,
246
+ immediate: true,
247
+ callback: (diff) => {
248
+ observable.dirty = true;
249
+ this.onAfterBroadcast(() => {
250
+ observer.next(getNewestResult(diff));
251
+ observable.dirty = false;
252
+ });
253
+ },
254
+ });
255
+ return () => {
256
+ subscribed = false;
257
+ cleanup();
258
+ this.fragmentWatches.removeArray(cacheKey);
259
+ };
260
+ }).pipe((0, rxjs_1.distinctUntilChanged)(), (0, rxjs_1.share)({
261
+ connector: () => new rxjs_1.ReplaySubject(1),
262
+ // debounce so a synchronous unsubscribe+resubscribe doesn't tear down the watch and create a new one
263
+ resetOnRefCountZero: () => (0, rxjs_1.timer)(0),
264
+ }));
265
+ cacheEntry.observable = Object.assign(observable, {
266
+ dirty: false,
267
+ getCurrentResult: () => {
268
+ if (subscribed && currentResult) {
269
+ return currentResult;
270
+ }
271
+ return getNewestResult(this.diff({
272
+ id,
273
+ query: fragmentQuery,
274
+ returnPartialData: true,
275
+ optimistic,
276
+ variables,
277
+ }));
136
278
  },
137
279
  });
138
- });
280
+ }
281
+ return cacheEntry.observable;
139
282
  }
140
283
  // Make sure we compute the same (===) fragment query document every
141
284
  // time we receive the same fragment in readFragment.
@@ -145,10 +288,11 @@ class ApolloCache {
145
288
  cache: caches_1.WeakCache,
146
289
  });
147
290
  readFragment(options, optimistic = !!options.optimistic) {
291
+ const id = options.from !== undefined ? this.toCacheId(options.from) : options.id;
148
292
  return this.read({
149
293
  ...options,
150
294
  query: this.getFragmentDoc(options.fragment, options.fragmentName),
151
- rootId: options.id,
295
+ rootId: id,
152
296
  optimistic,
153
297
  });
154
298
  }
@@ -158,7 +302,8 @@ class ApolloCache {
158
302
  result: data,
159
303
  }));
160
304
  }
161
- writeFragment({ id, data, fragment, fragmentName, ...options }) {
305
+ writeFragment({ data, fragment, fragmentName, ...options }) {
306
+ const id = options.from !== undefined ? this.toCacheId(options.from) : options.id;
162
307
  return this.write(Object.assign(options, {
163
308
  query: this.getFragmentDoc(fragment, fragmentName),
164
309
  dataId: id,
@@ -189,9 +334,28 @@ class ApolloCache {
189
334
  },
190
335
  });
191
336
  }
337
+ toCacheId(from) {
338
+ return typeof from === "string" ? from : this.identify(from);
339
+ }
192
340
  }
193
341
  exports.ApolloCache = ApolloCache;
194
342
  if (environment_1.__DEV__) {
195
343
  ApolloCache.prototype.getMemoryInternals = internal_1.getApolloCacheMemoryInternals;
196
344
  }
345
+ const nullResult = Object.freeze({
346
+ data: null,
347
+ dataState: "complete",
348
+ complete: true,
349
+ });
350
+ const nullObservable = Object.assign(new rxjs_1.Observable((observer) => {
351
+ observer.next(nullResult);
352
+ }), { dirty: false, getCurrentResult: () => nullResult });
353
+ const emptyArrayResult = Object.freeze({
354
+ data: [],
355
+ dataState: "complete",
356
+ complete: true,
357
+ });
358
+ const emptyArrayObservable = Object.assign(new rxjs_1.Observable((observer) => {
359
+ observer.next(emptyArrayResult);
360
+ }), { getCurrentResult: () => emptyArrayResult });
197
361
  //# sourceMappingURL=cache.cjs.map