@apollo/client 3.8.7 → 3.8.9

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 (614) hide show
  1. package/.changeset/README.md +8 -0
  2. package/.changeset/config.json +14 -0
  3. package/CHANGELOG.md +3369 -0
  4. package/apollo-client.cjs +356 -354
  5. package/apollo-client.cjs.map +1 -1
  6. package/apollo-client.min.cjs +1 -1
  7. package/cache/cache.cjs +133 -152
  8. package/cache/cache.cjs.map +1 -1
  9. package/cache/cache.cjs.native.js +133 -152
  10. package/cache/core/cache.d.ts +10 -0
  11. package/cache/core/cache.js +17 -5
  12. package/cache/core/cache.js.map +1 -1
  13. package/cache/core/types/Cache.d.ts +1 -1
  14. package/cache/core/types/Cache.js.map +1 -1
  15. package/cache/core/types/DataProxy.d.ts +92 -0
  16. package/cache/core/types/common.js +4 -1
  17. package/cache/core/types/common.js.map +1 -1
  18. package/cache/inmemory/entityStore.d.ts +1 -1
  19. package/cache/inmemory/entityStore.js +190 -29
  20. package/cache/inmemory/entityStore.js.map +1 -1
  21. package/cache/inmemory/fixPolyfills.js +9 -0
  22. package/cache/inmemory/fixPolyfills.js.map +1 -1
  23. package/cache/inmemory/fixPolyfills.native.js +12 -0
  24. package/cache/inmemory/fixPolyfills.native.js.map +1 -1
  25. package/cache/inmemory/fragmentRegistry.js +23 -12
  26. package/cache/inmemory/fragmentRegistry.js.map +1 -1
  27. package/cache/inmemory/helpers.js +20 -11
  28. package/cache/inmemory/helpers.js.map +1 -1
  29. package/cache/inmemory/inMemoryCache.js +161 -8
  30. package/cache/inmemory/inMemoryCache.js.map +1 -1
  31. package/cache/inmemory/key-extractor.js +72 -4
  32. package/cache/inmemory/key-extractor.js.map +1 -1
  33. package/cache/inmemory/object-canon.js +91 -3
  34. package/cache/inmemory/object-canon.js.map +1 -1
  35. package/cache/inmemory/policies.js +193 -43
  36. package/cache/inmemory/policies.js.map +1 -1
  37. package/cache/inmemory/reactiveVars.js +20 -2
  38. package/cache/inmemory/reactiveVars.js.map +1 -1
  39. package/cache/inmemory/readFromStore.d.ts +4 -0
  40. package/cache/inmemory/readFromStore.js +54 -9
  41. package/cache/inmemory/readFromStore.js.map +1 -1
  42. package/cache/inmemory/types.d.ts +28 -0
  43. package/cache/inmemory/types.js.map +1 -1
  44. package/cache/inmemory/writeToStore.js +152 -25
  45. package/cache/inmemory/writeToStore.js.map +1 -1
  46. package/config/jest/setup.js +2 -0
  47. package/config/jest/setup.js.map +1 -1
  48. package/core/ApolloClient.d.ts +264 -3
  49. package/core/ApolloClient.js +235 -7
  50. package/core/ApolloClient.js.map +1 -1
  51. package/core/LocalState.d.ts +4 -6
  52. package/core/LocalState.js +56 -18
  53. package/core/LocalState.js.map +1 -1
  54. package/core/ObservableQuery.d.ts +25 -0
  55. package/core/ObservableQuery.js +239 -30
  56. package/core/ObservableQuery.js.map +1 -1
  57. package/core/QueryInfo.d.ts +3 -3
  58. package/core/QueryInfo.js +116 -13
  59. package/core/QueryInfo.js.map +1 -1
  60. package/core/QueryManager.d.ts +4 -0
  61. package/core/QueryManager.js +229 -33
  62. package/core/QueryManager.js.map +1 -1
  63. package/core/core.cjs +112 -103
  64. package/core/core.cjs.map +1 -1
  65. package/core/core.cjs.native.js +112 -103
  66. package/core/equalByQuery.js +20 -1
  67. package/core/equalByQuery.js.map +1 -1
  68. package/core/index.d.ts +1 -1
  69. package/core/index.js +19 -0
  70. package/core/index.js.map +1 -1
  71. package/core/networkStatus.d.ts +39 -0
  72. package/core/networkStatus.js +39 -0
  73. package/core/networkStatus.js.map +1 -1
  74. package/core/types.d.ts +9 -0
  75. package/core/types.js.map +1 -1
  76. package/core/watchQueryOptions.d.ts +225 -1
  77. package/core/watchQueryOptions.js.map +1 -1
  78. package/dev/dev.cjs +5 -111
  79. package/dev/dev.cjs.map +1 -1
  80. package/dev/dev.cjs.native.js +5 -111
  81. package/dev/loadErrorMessageHandler.js.map +1 -1
  82. package/errors/errors.cjs +1 -1
  83. package/errors/errors.cjs.map +1 -1
  84. package/errors/errors.cjs.native.js +1 -1
  85. package/errors/index.js +14 -1
  86. package/errors/index.js.map +1 -1
  87. package/invariantErrorCodes.js +1 -0
  88. package/link/batch/batch.cjs +2 -2
  89. package/link/batch/batch.cjs.map +1 -1
  90. package/link/batch/batch.cjs.native.js +2 -2
  91. package/link/batch/batchLink.d.ts +21 -0
  92. package/link/batch/batchLink.js +2 -1
  93. package/link/batch/batchLink.js.map +1 -1
  94. package/link/batch/batching.js +24 -1
  95. package/link/batch/batching.js.map +1 -1
  96. package/link/batch-http/batch-http.cjs +3 -2
  97. package/link/batch-http/batch-http.cjs.map +1 -1
  98. package/link/batch-http/batch-http.cjs.native.js +3 -2
  99. package/link/batch-http/batchHttpLink.d.ts +4 -0
  100. package/link/batch-http/batchHttpLink.js +53 -2
  101. package/link/batch-http/batchHttpLink.js.map +1 -1
  102. package/link/context/context.cjs.map +1 -1
  103. package/link/context/index.js +1 -0
  104. package/link/context/index.js.map +1 -1
  105. package/link/core/ApolloLink.js +12 -5
  106. package/link/core/ApolloLink.js.map +1 -1
  107. package/link/core/core.cjs +5 -5
  108. package/link/core/core.cjs.map +1 -1
  109. package/link/core/core.cjs.native.js +5 -5
  110. package/link/error/error.cjs +1 -1
  111. package/link/error/error.cjs.map +1 -1
  112. package/link/error/error.cjs.native.js +1 -1
  113. package/link/error/index.d.ts +3 -0
  114. package/link/error/index.js +4 -1
  115. package/link/error/index.js.map +1 -1
  116. package/link/http/HttpLink.d.ts +0 -2
  117. package/link/http/HttpLink.js +1 -1
  118. package/link/http/HttpLink.js.map +1 -1
  119. package/link/http/checkFetcher.d.ts +1 -1
  120. package/link/http/checkFetcher.js.map +1 -1
  121. package/link/http/createHttpLink.js +23 -1
  122. package/link/http/createHttpLink.js.map +1 -1
  123. package/link/http/createSignalIfSupported.d.ts +5 -0
  124. package/link/http/createSignalIfSupported.js +5 -0
  125. package/link/http/createSignalIfSupported.js.map +1 -1
  126. package/link/http/http.cjs +14 -15
  127. package/link/http/http.cjs.map +1 -1
  128. package/link/http/http.cjs.native.js +14 -15
  129. package/link/http/index.js +2 -1
  130. package/link/http/index.js.map +1 -1
  131. package/link/http/iterators/async.d.ts +4 -0
  132. package/link/http/iterators/async.js +4 -0
  133. package/link/http/iterators/async.js.map +1 -1
  134. package/link/http/iterators/nodeStream.d.ts +4 -0
  135. package/link/http/iterators/nodeStream.js +4 -0
  136. package/link/http/iterators/nodeStream.js.map +1 -1
  137. package/link/http/iterators/promise.d.ts +4 -0
  138. package/link/http/iterators/promise.js +4 -0
  139. package/link/http/iterators/promise.js.map +1 -1
  140. package/link/http/iterators/reader.d.ts +4 -0
  141. package/link/http/iterators/reader.js +4 -0
  142. package/link/http/iterators/reader.js.map +1 -1
  143. package/link/http/parseAndCheckHttpResponse.js +50 -13
  144. package/link/http/parseAndCheckHttpResponse.js.map +1 -1
  145. package/link/http/responseIterator.d.ts +4 -0
  146. package/link/http/responseIterator.js +6 -0
  147. package/link/http/responseIterator.js.map +1 -1
  148. package/link/http/rewriteURIForGET.d.ts +1 -1
  149. package/link/http/rewriteURIForGET.js +10 -0
  150. package/link/http/rewriteURIForGET.js.map +1 -1
  151. package/link/http/selectHttpOptionsAndBody.d.ts +46 -1
  152. package/link/http/selectHttpOptionsAndBody.js +23 -0
  153. package/link/http/selectHttpOptionsAndBody.js.map +1 -1
  154. package/link/http/selectURI.js.map +1 -1
  155. package/link/http/serializeFetchParameter.js.map +1 -1
  156. package/link/persisted-queries/index.js +34 -4
  157. package/link/persisted-queries/index.js.map +1 -1
  158. package/link/persisted-queries/persisted-queries.cjs +4 -4
  159. package/link/persisted-queries/persisted-queries.cjs.map +1 -1
  160. package/link/persisted-queries/persisted-queries.cjs.native.js +4 -4
  161. package/link/remove-typename/remove-typename.cjs +12 -9
  162. package/link/remove-typename/remove-typename.cjs.map +1 -1
  163. package/link/remove-typename/remove-typename.cjs.native.js +12 -9
  164. package/link/remove-typename/removeTypenameFromVariables.js +12 -9
  165. package/link/remove-typename/removeTypenameFromVariables.js.map +1 -1
  166. package/link/retry/delayFunction.d.ts +29 -0
  167. package/link/retry/delayFunction.js +6 -0
  168. package/link/retry/delayFunction.js.map +1 -1
  169. package/link/retry/retry.cjs +5 -5
  170. package/link/retry/retry.cjs.map +1 -1
  171. package/link/retry/retry.cjs.native.js +5 -5
  172. package/link/retry/retryFunction.d.ts +20 -0
  173. package/link/retry/retryLink.d.ts +6 -0
  174. package/link/retry/retryLink.js +31 -6
  175. package/link/retry/retryLink.js.map +1 -1
  176. package/link/schema/index.d.ts +13 -0
  177. package/link/schema/index.js +3 -3
  178. package/link/schema/index.js.map +1 -1
  179. package/link/schema/schema.cjs +3 -3
  180. package/link/schema/schema.cjs.map +1 -1
  181. package/link/schema/schema.cjs.native.js +3 -3
  182. package/link/subscriptions/index.js +35 -2
  183. package/link/subscriptions/index.js.map +1 -1
  184. package/link/subscriptions/subscriptions.cjs +3 -2
  185. package/link/subscriptions/subscriptions.cjs.map +1 -1
  186. package/link/subscriptions/subscriptions.cjs.native.js +3 -2
  187. package/link/utils/createOperation.js.map +1 -1
  188. package/link/utils/filterOperationVariables.js +4 -0
  189. package/link/utils/filterOperationVariables.js.map +1 -1
  190. package/link/utils/toPromise.js.map +1 -1
  191. package/link/utils/transformOperation.js +3 -2
  192. package/link/utils/transformOperation.js.map +1 -1
  193. package/link/utils/utils.cjs +2 -2
  194. package/link/utils/utils.cjs.map +1 -1
  195. package/link/utils/utils.cjs.native.js +2 -2
  196. package/link/utils/validateOperation.js.map +1 -1
  197. package/link/ws/index.d.ts +12 -0
  198. package/link/ws/index.js +1 -1
  199. package/link/ws/index.js.map +1 -1
  200. package/link/ws/ws.cjs +1 -1
  201. package/link/ws/ws.cjs.map +1 -1
  202. package/link/ws/ws.cjs.native.js +1 -1
  203. package/package.json +56 -45
  204. package/react/cache/QueryReference.d.ts +6 -1
  205. package/react/cache/QueryReference.js +26 -1
  206. package/react/cache/QueryReference.js.map +1 -1
  207. package/react/cache/SuspenseCache.d.ts +10 -0
  208. package/react/cache/SuspenseCache.js +1 -1
  209. package/react/cache/SuspenseCache.js.map +1 -1
  210. package/react/cache/getSuspenseCache.js.map +1 -1
  211. package/react/context/ApolloContext.d.ts +5 -0
  212. package/react/context/ApolloContext.js +10 -3
  213. package/react/context/ApolloContext.js.map +1 -1
  214. package/react/context/context.cjs +1 -3
  215. package/react/context/context.cjs.map +1 -1
  216. package/react/context/context.cjs.native.js +1 -3
  217. package/react/hoc/graphql.js.map +1 -1
  218. package/react/hoc/hoc-utils.js +3 -1
  219. package/react/hoc/hoc-utils.js.map +1 -1
  220. package/react/hoc/hoc.cjs +9 -17
  221. package/react/hoc/hoc.cjs.map +1 -1
  222. package/react/hoc/hoc.cjs.native.js +9 -17
  223. package/react/hoc/mutation-hoc.js +9 -4
  224. package/react/hoc/mutation-hoc.js.map +1 -1
  225. package/react/hoc/query-hoc.js +10 -4
  226. package/react/hoc/query-hoc.js.map +1 -1
  227. package/react/hoc/subscription-hoc.js +10 -4
  228. package/react/hoc/subscription-hoc.js.map +1 -1
  229. package/react/hoc/withApollo.js +3 -4
  230. package/react/hoc/withApollo.js.map +1 -1
  231. package/react/hooks/hooks.cjs +72 -57
  232. package/react/hooks/hooks.cjs.map +1 -1
  233. package/react/hooks/hooks.cjs.native.js +72 -57
  234. package/react/hooks/internal/__use.js +5 -0
  235. package/react/hooks/internal/__use.js.map +1 -1
  236. package/react/hooks/internal/index.d.ts +1 -0
  237. package/react/hooks/internal/index.js +2 -0
  238. package/react/hooks/internal/index.js.map +1 -1
  239. package/react/hooks/internal/useDeepMemo.js.map +1 -1
  240. package/react/hooks/internal/useIsomorphicLayoutEffect.js +6 -3
  241. package/react/hooks/internal/useIsomorphicLayoutEffect.js.map +1 -1
  242. package/react/hooks/internal/useLazyRef.d.ts +3 -0
  243. package/react/hooks/internal/useLazyRef.js +10 -0
  244. package/react/hooks/internal/useLazyRef.js.map +1 -0
  245. package/react/hooks/useBackgroundQuery.js +6 -0
  246. package/react/hooks/useBackgroundQuery.js.map +1 -1
  247. package/react/hooks/useFragment.js +21 -17
  248. package/react/hooks/useFragment.js.map +1 -1
  249. package/react/hooks/useLazyQuery.js +13 -4
  250. package/react/hooks/useLazyQuery.js.map +1 -1
  251. package/react/hooks/useMutation.js +8 -3
  252. package/react/hooks/useMutation.js.map +1 -1
  253. package/react/hooks/useQuery.d.ts +11 -0
  254. package/react/hooks/useQuery.js +137 -5
  255. package/react/hooks/useQuery.js.map +1 -1
  256. package/react/hooks/useReactiveVar.js +5 -0
  257. package/react/hooks/useReactiveVar.js.map +1 -1
  258. package/react/hooks/useReadQuery.d.ts +19 -0
  259. package/react/hooks/useReadQuery.js.map +1 -1
  260. package/react/hooks/useSubscription.js +5 -0
  261. package/react/hooks/useSubscription.js.map +1 -1
  262. package/react/hooks/useSuspenseQuery.js +11 -5
  263. package/react/hooks/useSuspenseQuery.js.map +1 -1
  264. package/react/hooks/useSyncExternalStore.js +53 -1
  265. package/react/hooks/useSyncExternalStore.js.map +1 -1
  266. package/react/parser/index.js +4 -5
  267. package/react/parser/index.js.map +1 -1
  268. package/react/parser/parser.cjs +2 -4
  269. package/react/parser/parser.cjs.map +1 -1
  270. package/react/parser/parser.cjs.native.js +2 -4
  271. package/react/ssr/RenderPromises.js +21 -1
  272. package/react/ssr/RenderPromises.js.map +1 -1
  273. package/react/ssr/getDataFromTree.js +18 -3
  274. package/react/ssr/getDataFromTree.js.map +1 -1
  275. package/react/ssr/ssr.cjs +6 -4
  276. package/react/ssr/ssr.cjs.map +1 -1
  277. package/react/ssr/ssr.cjs.native.js +6 -4
  278. package/react/types/types.d.ts +66 -0
  279. package/testing/core/core.cjs +18 -13
  280. package/testing/core/core.cjs.map +1 -1
  281. package/testing/core/core.cjs.native.js +18 -13
  282. package/testing/core/itAsync.d.ts +1 -1
  283. package/testing/core/itAsync.js.map +1 -1
  284. package/testing/core/mocking/mockFetch.d.ts +2 -2
  285. package/testing/core/mocking/mockFetch.js +8 -4
  286. package/testing/core/mocking/mockFetch.js.map +1 -1
  287. package/testing/core/mocking/mockLink.js +25 -9
  288. package/testing/core/mocking/mockLink.js.map +1 -1
  289. package/testing/core/mocking/mockQueryManager.js +2 -0
  290. package/testing/core/mocking/mockQueryManager.js.map +1 -1
  291. package/testing/core/mocking/mockSubscriptionLink.d.ts +1 -1
  292. package/testing/core/mocking/mockSubscriptionLink.js +1 -1
  293. package/testing/core/mocking/mockSubscriptionLink.js.map +1 -1
  294. package/testing/core/mocking/mockWatchQuery.js +1 -1
  295. package/testing/core/mocking/mockWatchQuery.js.map +1 -1
  296. package/testing/core/observableToPromise.d.ts +17 -3
  297. package/testing/core/observableToPromise.js +5 -0
  298. package/testing/core/observableToPromise.js.map +1 -1
  299. package/testing/core/subscribeAndCount.js +3 -0
  300. package/testing/core/subscribeAndCount.js.map +1 -1
  301. package/testing/core/wait.js +2 -2
  302. package/testing/core/wait.js.map +1 -1
  303. package/testing/core/withConsoleSpy.d.ts +3 -0
  304. package/testing/core/withConsoleSpy.js +7 -1
  305. package/testing/core/withConsoleSpy.js.map +1 -1
  306. package/testing/core/wrap.js +2 -0
  307. package/testing/core/wrap.js.map +1 -1
  308. package/testing/internal/ObservableStream.js +14 -14
  309. package/testing/internal/ObservableStream.js.map +1 -1
  310. package/testing/internal/disposables/spyOnConsole.d.ts +1 -0
  311. package/testing/internal/disposables/spyOnConsole.js +2 -0
  312. package/testing/internal/disposables/spyOnConsole.js.map +1 -1
  313. package/testing/internal/disposables/withCleanup.d.ts +1 -0
  314. package/testing/internal/disposables/withCleanup.js +3 -0
  315. package/testing/internal/disposables/withCleanup.js.map +1 -1
  316. package/testing/internal/profile/Render.d.ts +28 -0
  317. package/testing/internal/profile/Render.js +12 -1
  318. package/testing/internal/profile/Render.js.map +1 -1
  319. package/testing/internal/profile/profile.d.ts +44 -7
  320. package/testing/internal/profile/profile.js +63 -51
  321. package/testing/internal/profile/profile.js.map +1 -1
  322. package/testing/internal/profile/traces.d.ts +4 -0
  323. package/testing/internal/profile/traces.js +9 -5
  324. package/testing/internal/profile/traces.js.map +1 -1
  325. package/testing/matchers/ProfiledComponent.js +27 -22
  326. package/testing/matchers/ProfiledComponent.js.map +1 -1
  327. package/testing/matchers/toHaveSuspenseCacheEntryUsing.js.map +1 -1
  328. package/testing/matchers/toMatchDocument.js.map +1 -1
  329. package/testing/react/MockedProvider.d.ts +4 -0
  330. package/testing/react/MockedProvider.js +6 -2
  331. package/testing/react/MockedProvider.js.map +1 -1
  332. package/testing/testing.cjs +4 -2
  333. package/testing/testing.cjs.map +1 -1
  334. package/testing/testing.cjs.native.js +4 -2
  335. package/utilities/common/arrays.js +1 -0
  336. package/utilities/common/arrays.js.map +1 -1
  337. package/utilities/common/canUse.js +16 -1
  338. package/utilities/common/canUse.js.map +1 -1
  339. package/utilities/common/cloneDeep.d.ts +3 -0
  340. package/utilities/common/cloneDeep.js +5 -0
  341. package/utilities/common/cloneDeep.js.map +1 -1
  342. package/utilities/common/compact.d.ts +4 -0
  343. package/utilities/common/compact.js +4 -0
  344. package/utilities/common/compact.js.map +1 -1
  345. package/utilities/common/errorHandling.js +1 -3
  346. package/utilities/common/errorHandling.js.map +1 -1
  347. package/utilities/common/incrementalResult.js +3 -0
  348. package/utilities/common/incrementalResult.js.map +1 -1
  349. package/utilities/common/makeUniqueId.js +2 -0
  350. package/utilities/common/makeUniqueId.js.map +1 -1
  351. package/utilities/common/maybeDeepFreeze.js +3 -0
  352. package/utilities/common/maybeDeepFreeze.js.map +1 -1
  353. package/utilities/common/mergeDeep.js +12 -1
  354. package/utilities/common/mergeDeep.js.map +1 -1
  355. package/utilities/common/mergeOptions.d.ts +1 -1
  356. package/utilities/common/mergeOptions.js.map +1 -1
  357. package/utilities/common/omitDeep.js.map +1 -1
  358. package/utilities/globals/global.js +7 -1
  359. package/utilities/globals/global.js.map +1 -1
  360. package/utilities/globals/globals.cjs +4 -5
  361. package/utilities/globals/globals.cjs.map +1 -1
  362. package/utilities/globals/globals.cjs.native.js +4 -5
  363. package/utilities/globals/index.d.ts +4 -0
  364. package/utilities/globals/index.js +7 -0
  365. package/utilities/globals/index.js.map +1 -1
  366. package/utilities/globals/invariantWrappers.d.ts +40 -0
  367. package/utilities/globals/invariantWrappers.js +11 -3
  368. package/utilities/globals/invariantWrappers.js.map +1 -1
  369. package/utilities/globals/maybe.js.map +1 -1
  370. package/utilities/graphql/DocumentTransform.js +19 -6
  371. package/utilities/graphql/DocumentTransform.js.map +1 -1
  372. package/utilities/graphql/directives.js +3 -0
  373. package/utilities/graphql/directives.js.map +1 -1
  374. package/utilities/graphql/fragments.d.ts +25 -0
  375. package/utilities/graphql/fragments.js +36 -0
  376. package/utilities/graphql/fragments.js.map +1 -1
  377. package/utilities/graphql/getFromAST.d.ts +5 -0
  378. package/utilities/graphql/getFromAST.js +9 -0
  379. package/utilities/graphql/getFromAST.js.map +1 -1
  380. package/utilities/graphql/print.js.map +1 -1
  381. package/utilities/graphql/storeUtils.d.ts +14 -0
  382. package/utilities/graphql/storeUtils.js +7 -2
  383. package/utilities/graphql/storeUtils.js.map +1 -1
  384. package/utilities/graphql/transform.js +106 -7
  385. package/utilities/graphql/transform.js.map +1 -1
  386. package/utilities/observables/Concast.d.ts +1 -1
  387. package/utilities/observables/Concast.js +84 -1
  388. package/utilities/observables/Concast.js.map +1 -1
  389. package/utilities/observables/Observable.js +6 -0
  390. package/utilities/observables/Observable.js.map +1 -1
  391. package/utilities/observables/asyncMap.js +12 -3
  392. package/utilities/observables/asyncMap.js.map +1 -1
  393. package/utilities/observables/iteration.js +3 -0
  394. package/utilities/observables/iteration.js.map +1 -1
  395. package/utilities/observables/subclassing.js +14 -0
  396. package/utilities/observables/subclassing.js.map +1 -1
  397. package/utilities/policies/pagination.js +47 -3
  398. package/utilities/policies/pagination.js.map +1 -1
  399. package/utilities/promises/decoration.js +1 -0
  400. package/utilities/promises/decoration.js.map +1 -1
  401. package/utilities/types/DeepOmit.js.map +1 -1
  402. package/utilities/types/DeepPartial.d.ts +1 -1
  403. package/utilities/types/DeepPartial.js +4 -0
  404. package/utilities/types/DeepPartial.js.map +1 -1
  405. package/utilities/types/IsStrictlyAny.js.map +1 -1
  406. package/utilities/types/TODO.d.ts +3 -0
  407. package/utilities/types/TODO.js +2 -0
  408. package/utilities/types/TODO.js.map +1 -0
  409. package/utilities/utilities.cjs +31 -27
  410. package/utilities/utilities.cjs.map +1 -1
  411. package/utilities/utilities.cjs.native.js +31 -27
  412. package/version.js +1 -1
  413. package/cache/core/cache.d.ts.map +0 -1
  414. package/cache/core/types/Cache.d.ts.map +0 -1
  415. package/cache/core/types/DataProxy.d.ts.map +0 -1
  416. package/cache/core/types/common.d.ts.map +0 -1
  417. package/cache/index.d.ts.map +0 -1
  418. package/cache/inmemory/entityStore.d.ts.map +0 -1
  419. package/cache/inmemory/fixPolyfills.d.ts.map +0 -1
  420. package/cache/inmemory/fixPolyfills.native.d.ts.map +0 -1
  421. package/cache/inmemory/fragmentRegistry.d.ts.map +0 -1
  422. package/cache/inmemory/helpers.d.ts.map +0 -1
  423. package/cache/inmemory/inMemoryCache.d.ts.map +0 -1
  424. package/cache/inmemory/key-extractor.d.ts.map +0 -1
  425. package/cache/inmemory/object-canon.d.ts.map +0 -1
  426. package/cache/inmemory/policies.d.ts.map +0 -1
  427. package/cache/inmemory/reactiveVars.d.ts.map +0 -1
  428. package/cache/inmemory/readFromStore.d.ts.map +0 -1
  429. package/cache/inmemory/types.d.ts.map +0 -1
  430. package/cache/inmemory/writeToStore.d.ts.map +0 -1
  431. package/config/jest/setup.d.ts.map +0 -1
  432. package/core/ApolloClient.d.ts.map +0 -1
  433. package/core/LocalState.d.ts.map +0 -1
  434. package/core/ObservableQuery.d.ts.map +0 -1
  435. package/core/QueryInfo.d.ts.map +0 -1
  436. package/core/QueryManager.d.ts.map +0 -1
  437. package/core/equalByQuery.d.ts.map +0 -1
  438. package/core/index.d.ts.map +0 -1
  439. package/core/networkStatus.d.ts.map +0 -1
  440. package/core/types.d.ts.map +0 -1
  441. package/core/watchQueryOptions.d.ts.map +0 -1
  442. package/dev/index.d.ts.map +0 -1
  443. package/dev/loadDevMessages.d.ts.map +0 -1
  444. package/dev/loadErrorMessageHandler.d.ts.map +0 -1
  445. package/dev/loadErrorMessages.d.ts.map +0 -1
  446. package/errors/index.d.ts.map +0 -1
  447. package/index.d.ts.map +0 -1
  448. package/invariantErrorCodes.d.ts.map +0 -1
  449. package/link/batch/batchLink.d.ts.map +0 -1
  450. package/link/batch/batching.d.ts.map +0 -1
  451. package/link/batch/index.d.ts.map +0 -1
  452. package/link/batch-http/batchHttpLink.d.ts.map +0 -1
  453. package/link/batch-http/index.d.ts.map +0 -1
  454. package/link/context/index.d.ts.map +0 -1
  455. package/link/core/ApolloLink.d.ts.map +0 -1
  456. package/link/core/concat.d.ts.map +0 -1
  457. package/link/core/empty.d.ts.map +0 -1
  458. package/link/core/execute.d.ts.map +0 -1
  459. package/link/core/from.d.ts.map +0 -1
  460. package/link/core/index.d.ts.map +0 -1
  461. package/link/core/split.d.ts.map +0 -1
  462. package/link/core/types.d.ts.map +0 -1
  463. package/link/error/index.d.ts.map +0 -1
  464. package/link/http/HttpLink.d.ts.map +0 -1
  465. package/link/http/checkFetcher.d.ts.map +0 -1
  466. package/link/http/createHttpLink.d.ts.map +0 -1
  467. package/link/http/createSignalIfSupported.d.ts.map +0 -1
  468. package/link/http/index.d.ts.map +0 -1
  469. package/link/http/iterators/async.d.ts.map +0 -1
  470. package/link/http/iterators/nodeStream.d.ts.map +0 -1
  471. package/link/http/iterators/promise.d.ts.map +0 -1
  472. package/link/http/iterators/reader.d.ts.map +0 -1
  473. package/link/http/parseAndCheckHttpResponse.d.ts.map +0 -1
  474. package/link/http/responseIterator.d.ts.map +0 -1
  475. package/link/http/rewriteURIForGET.d.ts.map +0 -1
  476. package/link/http/selectHttpOptionsAndBody.d.ts.map +0 -1
  477. package/link/http/selectURI.d.ts.map +0 -1
  478. package/link/http/serializeFetchParameter.d.ts.map +0 -1
  479. package/link/persisted-queries/index.d.ts.map +0 -1
  480. package/link/remove-typename/index.d.ts.map +0 -1
  481. package/link/remove-typename/removeTypenameFromVariables.d.ts.map +0 -1
  482. package/link/retry/delayFunction.d.ts.map +0 -1
  483. package/link/retry/index.d.ts.map +0 -1
  484. package/link/retry/retryFunction.d.ts.map +0 -1
  485. package/link/retry/retryLink.d.ts.map +0 -1
  486. package/link/schema/index.d.ts.map +0 -1
  487. package/link/subscriptions/index.d.ts.map +0 -1
  488. package/link/utils/createOperation.d.ts.map +0 -1
  489. package/link/utils/filterOperationVariables.d.ts.map +0 -1
  490. package/link/utils/fromError.d.ts.map +0 -1
  491. package/link/utils/fromPromise.d.ts.map +0 -1
  492. package/link/utils/index.d.ts.map +0 -1
  493. package/link/utils/throwServerError.d.ts.map +0 -1
  494. package/link/utils/toPromise.d.ts.map +0 -1
  495. package/link/utils/transformOperation.d.ts.map +0 -1
  496. package/link/utils/validateOperation.d.ts.map +0 -1
  497. package/link/ws/index.d.ts.map +0 -1
  498. package/react/cache/QueryReference.d.ts.map +0 -1
  499. package/react/cache/SuspenseCache.d.ts.map +0 -1
  500. package/react/cache/getSuspenseCache.d.ts.map +0 -1
  501. package/react/cache/index.d.ts.map +0 -1
  502. package/react/cache/types.d.ts.map +0 -1
  503. package/react/components/Mutation.d.ts.map +0 -1
  504. package/react/components/Query.d.ts.map +0 -1
  505. package/react/components/Subscription.d.ts.map +0 -1
  506. package/react/components/index.d.ts.map +0 -1
  507. package/react/components/types.d.ts.map +0 -1
  508. package/react/context/ApolloConsumer.d.ts.map +0 -1
  509. package/react/context/ApolloContext.d.ts.map +0 -1
  510. package/react/context/ApolloProvider.d.ts.map +0 -1
  511. package/react/context/index.d.ts.map +0 -1
  512. package/react/hoc/graphql.d.ts.map +0 -1
  513. package/react/hoc/hoc-utils.d.ts.map +0 -1
  514. package/react/hoc/index.d.ts.map +0 -1
  515. package/react/hoc/mutation-hoc.d.ts.map +0 -1
  516. package/react/hoc/query-hoc.d.ts.map +0 -1
  517. package/react/hoc/subscription-hoc.d.ts.map +0 -1
  518. package/react/hoc/types.d.ts.map +0 -1
  519. package/react/hoc/withApollo.d.ts.map +0 -1
  520. package/react/hooks/constants.d.ts.map +0 -1
  521. package/react/hooks/index.d.ts.map +0 -1
  522. package/react/hooks/internal/__use.d.ts.map +0 -1
  523. package/react/hooks/internal/index.d.ts.map +0 -1
  524. package/react/hooks/internal/useDeepMemo.d.ts.map +0 -1
  525. package/react/hooks/internal/useIsomorphicLayoutEffect.d.ts.map +0 -1
  526. package/react/hooks/useApolloClient.d.ts.map +0 -1
  527. package/react/hooks/useBackgroundQuery.d.ts.map +0 -1
  528. package/react/hooks/useFragment.d.ts.map +0 -1
  529. package/react/hooks/useLazyQuery.d.ts.map +0 -1
  530. package/react/hooks/useMutation.d.ts.map +0 -1
  531. package/react/hooks/useQuery.d.ts.map +0 -1
  532. package/react/hooks/useReactiveVar.d.ts.map +0 -1
  533. package/react/hooks/useReadQuery.d.ts.map +0 -1
  534. package/react/hooks/useSubscription.d.ts.map +0 -1
  535. package/react/hooks/useSuspenseQuery.d.ts.map +0 -1
  536. package/react/hooks/useSyncExternalStore.d.ts.map +0 -1
  537. package/react/index.d.ts.map +0 -1
  538. package/react/parser/index.d.ts.map +0 -1
  539. package/react/ssr/RenderPromises.d.ts.map +0 -1
  540. package/react/ssr/getDataFromTree.d.ts.map +0 -1
  541. package/react/ssr/index.d.ts.map +0 -1
  542. package/react/ssr/renderToStringWithData.d.ts.map +0 -1
  543. package/react/types/types.d.ts.map +0 -1
  544. package/testing/core/index.d.ts.map +0 -1
  545. package/testing/core/itAsync.d.ts.map +0 -1
  546. package/testing/core/mocking/mockClient.d.ts.map +0 -1
  547. package/testing/core/mocking/mockFetch.d.ts.map +0 -1
  548. package/testing/core/mocking/mockLink.d.ts.map +0 -1
  549. package/testing/core/mocking/mockQueryManager.d.ts.map +0 -1
  550. package/testing/core/mocking/mockSubscriptionLink.d.ts.map +0 -1
  551. package/testing/core/mocking/mockWatchQuery.d.ts.map +0 -1
  552. package/testing/core/observableToPromise.d.ts.map +0 -1
  553. package/testing/core/subscribeAndCount.d.ts.map +0 -1
  554. package/testing/core/wait.d.ts.map +0 -1
  555. package/testing/core/withConsoleSpy.d.ts.map +0 -1
  556. package/testing/core/wrap.d.ts.map +0 -1
  557. package/testing/index.d.ts.map +0 -1
  558. package/testing/internal/ObservableStream.d.ts.map +0 -1
  559. package/testing/internal/disposables/index.d.ts.map +0 -1
  560. package/testing/internal/disposables/spyOnConsole.d.ts.map +0 -1
  561. package/testing/internal/disposables/withCleanup.d.ts.map +0 -1
  562. package/testing/internal/index.d.ts.map +0 -1
  563. package/testing/internal/profile/Render.d.ts.map +0 -1
  564. package/testing/internal/profile/index.d.ts.map +0 -1
  565. package/testing/internal/profile/profile.d.ts.map +0 -1
  566. package/testing/internal/profile/traces.d.ts.map +0 -1
  567. package/testing/matchers/ProfiledComponent.d.ts.map +0 -1
  568. package/testing/matchers/index.d.ts.map +0 -1
  569. package/testing/matchers/toHaveSuspenseCacheEntryUsing.d.ts.map +0 -1
  570. package/testing/matchers/toMatchDocument.d.ts.map +0 -1
  571. package/testing/react/MockedProvider.d.ts.map +0 -1
  572. package/utilities/common/arrays.d.ts.map +0 -1
  573. package/utilities/common/canUse.d.ts.map +0 -1
  574. package/utilities/common/cloneDeep.d.ts.map +0 -1
  575. package/utilities/common/compact.d.ts.map +0 -1
  576. package/utilities/common/errorHandling.d.ts.map +0 -1
  577. package/utilities/common/filterInPlace.d.ts +0 -2
  578. package/utilities/common/filterInPlace.d.ts.map +0 -1
  579. package/utilities/common/filterInPlace.js +0 -11
  580. package/utilities/common/filterInPlace.js.map +0 -1
  581. package/utilities/common/incrementalResult.d.ts.map +0 -1
  582. package/utilities/common/makeUniqueId.d.ts.map +0 -1
  583. package/utilities/common/maybeDeepFreeze.d.ts.map +0 -1
  584. package/utilities/common/mergeDeep.d.ts.map +0 -1
  585. package/utilities/common/mergeOptions.d.ts.map +0 -1
  586. package/utilities/common/objects.d.ts.map +0 -1
  587. package/utilities/common/omitDeep.d.ts.map +0 -1
  588. package/utilities/common/stringifyForDisplay.d.ts.map +0 -1
  589. package/utilities/common/stripTypename.d.ts.map +0 -1
  590. package/utilities/globals/global.d.ts.map +0 -1
  591. package/utilities/globals/index.d.ts.map +0 -1
  592. package/utilities/globals/invariantWrappers.d.ts.map +0 -1
  593. package/utilities/globals/maybe.d.ts.map +0 -1
  594. package/utilities/graphql/DocumentTransform.d.ts.map +0 -1
  595. package/utilities/graphql/directives.d.ts.map +0 -1
  596. package/utilities/graphql/fragments.d.ts.map +0 -1
  597. package/utilities/graphql/getFromAST.d.ts.map +0 -1
  598. package/utilities/graphql/operations.d.ts.map +0 -1
  599. package/utilities/graphql/print.d.ts.map +0 -1
  600. package/utilities/graphql/storeUtils.d.ts.map +0 -1
  601. package/utilities/graphql/transform.d.ts.map +0 -1
  602. package/utilities/index.d.ts.map +0 -1
  603. package/utilities/observables/Concast.d.ts.map +0 -1
  604. package/utilities/observables/Observable.d.ts.map +0 -1
  605. package/utilities/observables/asyncMap.d.ts.map +0 -1
  606. package/utilities/observables/iteration.d.ts.map +0 -1
  607. package/utilities/observables/subclassing.d.ts.map +0 -1
  608. package/utilities/policies/pagination.d.ts.map +0 -1
  609. package/utilities/promises/decoration.d.ts.map +0 -1
  610. package/utilities/types/DeepOmit.d.ts.map +0 -1
  611. package/utilities/types/DeepPartial.d.ts.map +0 -1
  612. package/utilities/types/IsStrictlyAny.d.ts.map +0 -1
  613. package/utilities/types/Primitive.d.ts.map +0 -1
  614. package/version.d.ts.map +0 -1
@@ -21,6 +21,8 @@ export function useMutation(mutation, options) {
21
21
  mutation: mutation,
22
22
  options: options,
23
23
  });
24
+ // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks
25
+ // higher-order components.
24
26
  {
25
27
  Object.assign(ref.current, { client: client, options: options, mutation: mutation });
26
28
  }
@@ -47,8 +49,8 @@ export function useMutation(mutation, options) {
47
49
  .then(function (response) {
48
50
  var _a, _b;
49
51
  var data = response.data, errors = response.errors;
50
- var error = errors && errors.length > 0
51
- ? new ApolloError({ graphQLErrors: errors })
52
+ var error = errors && errors.length > 0 ?
53
+ new ApolloError({ graphQLErrors: errors })
52
54
  : void 0;
53
55
  var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);
54
56
  if (error && onError) {
@@ -90,6 +92,7 @@ export function useMutation(mutation, options) {
90
92
  var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);
91
93
  if (onError) {
92
94
  onError(error, clientOptions);
95
+ // TODO(brian): why are we returning this here???
93
96
  return { data: void 0, errors: error };
94
97
  }
95
98
  throw error;
@@ -97,7 +100,9 @@ export function useMutation(mutation, options) {
97
100
  }, []);
98
101
  var reset = React.useCallback(function () {
99
102
  if (ref.current.isMounted) {
100
- setResult({ called: false, loading: false, client: client });
103
+ var result_3 = { called: false, loading: false, client: client };
104
+ Object.assign(ref.current, { mutationId: 0, result: result_3 });
105
+ setResult(result_3);
101
106
  }
102
107
  }, []);
103
108
  React.useEffect(function () {
@@ -1 +1 @@
1
- {"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAgB/B,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OAKC;IAED,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAA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C,SAAS,GAAG,IAAI,CAAC;QAE7B,OAAO;YACL,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAChC,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import * as React from \"react\";\nimport type { DocumentNode } from \"graphql\";\nimport type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport type {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n NoInfer,\n} from \"../types/types.js\";\n\nimport type {\n ApolloCache,\n DefaultContext,\n OperationVariables,\n} from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport { equal } from \"@wry/equality\";\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>,\n TContext,\n TCache\n >\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = React.useState<Omit<MutationResult, \"reset\">>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = React.useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client, options, mutation });\n }\n\n const execute = React.useCallback(\n (\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const { options, mutation } = ref.current;\n const baseOptions = { ...options, mutation };\n const client = executeOptions.client || ref.current.client;\n\n if (\n !ref.current.result.loading &&\n !baseOptions.ignoreResults &&\n ref.current.isMounted\n ) {\n setResult(\n (ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n })\n );\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(baseOptions, executeOptions as any);\n\n return client\n .mutate(clientOptions)\n .then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0\n ? new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (error && onError) {\n onError(error, clientOptions);\n }\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onCompleted =\n executeOptions.onCompleted || ref.current.options?.onCompleted;\n\n if (!error) {\n onCompleted?.(response.data!, clientOptions);\n }\n\n return response;\n })\n .catch((error) => {\n if (mutationId === ref.current.mutationId && ref.current.isMounted) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (onError) {\n onError(error, clientOptions);\n\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n },\n []\n );\n\n const reset = React.useCallback(() => {\n if (ref.current.isMounted) {\n setResult({ called: false, loading: false, client });\n }\n }, []);\n\n React.useEffect(() => {\n ref.current.isMounted = true;\n\n return () => {\n ref.current.isMounted = false;\n };\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
1
+ {"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAiB/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CAAA,CAAC;YAEzD,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,CACL,KAAK,EACL,aAA2D,CAC5D,CAAC;gBAEF,iDAAiD;gBACjD,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;YACzC,CAAC;YAED,MAAM,KAAK,CAAC;QACd,CAAC,CAAC,CAAC;IACP,CAAC,EACD,EAAE,CACH,CAAC;IAEF,IAAM,KAAK,GAAG,KAAK,CAAC,WAAW,CAAC;QAC9B,IAAI,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC1B,IAAM,QAAM,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,QAAA,EAAE,CAAC;YACzD,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,UAAA,EAAE,CAAC,CAAC;YACtD,SAAS,CAAC,QAAM,CAAC,CAAC;QACpB,CAAC;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,KAAK,CAAC,SAAS,CAAC;QACd,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;QAE7B,OAAO;YACL,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAChC,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import * as React from \"react\";\nimport type { DocumentNode } from \"graphql\";\nimport type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport type {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n NoInfer,\n} from \"../types/types.js\";\n\nimport type {\n ApolloCache,\n DefaultContext,\n MutationOptions,\n OperationVariables,\n} from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport { equal } from \"@wry/equality\";\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>,\n TContext,\n TCache\n >\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = React.useState<Omit<MutationResult, \"reset\">>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = React.useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client, options, mutation });\n }\n\n const execute = React.useCallback(\n (\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const { options, mutation } = ref.current;\n const baseOptions = { ...options, mutation };\n const client = executeOptions.client || ref.current.client;\n\n if (\n !ref.current.result.loading &&\n !baseOptions.ignoreResults &&\n ref.current.isMounted\n ) {\n setResult(\n (ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n })\n );\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(baseOptions, executeOptions);\n\n return client\n .mutate(clientOptions as MutationOptions<TData, OperationVariables>)\n .then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0 ?\n new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (error && onError) {\n onError(\n error,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n }\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onCompleted =\n executeOptions.onCompleted || ref.current.options?.onCompleted;\n\n if (!error) {\n onCompleted?.(\n response.data!,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n }\n\n return response;\n })\n .catch((error) => {\n if (mutationId === ref.current.mutationId && ref.current.isMounted) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (onError) {\n onError(\n error,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n },\n []\n );\n\n const reset = React.useCallback(() => {\n if (ref.current.isMounted) {\n const result = { called: false, loading: false, client };\n Object.assign(ref.current, { mutationId: 0, result });\n setResult(result);\n }\n }, []);\n\n React.useEffect(() => {\n ref.current.isMounted = true;\n\n return () => {\n ref.current.isMounted = false;\n };\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
@@ -8,7 +8,18 @@ declare class InternalState<TData, TVariables extends OperationVariables> {
8
8
  readonly client: ReturnType<typeof useApolloClient>;
9
9
  readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>;
10
10
  constructor(client: ReturnType<typeof useApolloClient>, query: DocumentNode | TypedDocumentNode<TData, TVariables>, previous?: InternalState<TData, TVariables>);
11
+ /**
12
+ * Forces an update using local component state.
13
+ * As this is not batched with `useSyncExternalStore` updates,
14
+ * this is only used as a fallback if the `useSyncExternalStore` "force update"
15
+ * method is not registered at the moment.
16
+ * See https://github.com/facebook/react/issues/25191
17
+ * */
11
18
  forceUpdateState(): void;
19
+ /**
20
+ * Will be overwritten by the `useSyncExternalStore` "force update" method
21
+ * whenever it is available and reset to `forceUpdateState` when it isn't.
22
+ */
12
23
  forceUpdate: () => void;
13
24
  executeQuery(options: QueryHookOptions<TData, TVariables> & {
14
25
  query?: DocumentNode;
@@ -23,14 +23,24 @@ export function useInternalState(client, query) {
23
23
  stateRef.current = new InternalState(client, query, stateRef.current);
24
24
  }
25
25
  var state = stateRef.current;
26
+ // By default, InternalState.prototype.forceUpdate is an empty function, but
27
+ // we replace it here (before anyone has had a chance to see this state yet)
28
+ // with a function that unconditionally forces an update, using the latest
29
+ // setTick function. Updating this state by calling state.forceUpdate is the
30
+ // only way we trigger React component updates (no other useState calls within
31
+ // the InternalState class).
26
32
  state.forceUpdateState = React.useReducer(function (tick) { return tick + 1; }, 0)[1];
27
33
  return state;
28
34
  }
29
- var InternalState = (function () {
35
+ var InternalState = /** @class */ (function () {
30
36
  function InternalState(client, query, previous) {
31
37
  var _this = this;
32
38
  this.client = client;
33
39
  this.query = query;
40
+ /**
41
+ * Will be overwritten by the `useSyncExternalStore` "force update" method
42
+ * whenever it is available and reset to `forceUpdateState` when it isn't.
43
+ */
34
44
  this.forceUpdate = function () { return _this.forceUpdateState(); };
35
45
  this.ssrDisabledResult = maybeDeepFreeze({
36
46
  loading: true,
@@ -44,15 +54,28 @@ var InternalState = (function () {
44
54
  error: void 0,
45
55
  networkStatus: NetworkStatus.ready,
46
56
  });
57
+ // This cache allows the referential stability of this.result (as returned by
58
+ // getCurrentResult) to translate into referential stability of the resulting
59
+ // QueryResult object returned by toQueryResult.
47
60
  this.toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)();
48
61
  verifyDocumentType(query, DocumentType.Query);
62
+ // Reuse previousData from previous InternalState (if any) to provide
63
+ // continuity of previousData even if/when the query or client changes.
49
64
  var previousResult = previous && previous.result;
50
65
  var previousData = previousResult && previousResult.data;
51
66
  if (previousData) {
52
67
  this.previousData = previousData;
53
68
  }
54
69
  }
70
+ /**
71
+ * Forces an update using local component state.
72
+ * As this is not batched with `useSyncExternalStore` updates,
73
+ * this is only used as a fallback if the `useSyncExternalStore` "force update"
74
+ * method is not registered at the moment.
75
+ * See https://github.com/facebook/react/issues/25191
76
+ * */
55
77
  InternalState.prototype.forceUpdateState = function () {
78
+ // Replaced (in useInternalState) with a method that triggers an update.
56
79
  globalThis.__DEV__ !== false && invariant.warn(50);
57
80
  };
58
81
  InternalState.prototype.executeQuery = function (options) {
@@ -63,11 +86,18 @@ var InternalState = (function () {
63
86
  }
64
87
  this.watchQueryOptions = this.createWatchQueryOptions((this.queryHookOptions = options));
65
88
  var concast = this.observable.reobserveAsConcast(this.getObsQueryOptions());
89
+ // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,
90
+ // but save the current data as this.previousData, just like setResult
91
+ // usually does.
66
92
  this.previousData = ((_a = this.result) === null || _a === void 0 ? void 0 : _a.data) || this.previousData;
67
93
  this.result = void 0;
68
94
  this.forceUpdate();
69
95
  return new Promise(function (resolve) {
70
96
  var result;
97
+ // Subscribe to the concast independently of the ObservableQuery in case
98
+ // the component gets unmounted before the promise resolves. This prevents
99
+ // the concast from terminating early and resolving with `undefined` when
100
+ // there are no more subscribers for the concast.
71
101
  concast.subscribe({
72
102
  next: function (value) {
73
103
  result = value;
@@ -81,8 +111,18 @@ var InternalState = (function () {
81
111
  });
82
112
  });
83
113
  };
114
+ // Methods beginning with use- should be called according to the standard
115
+ // rules of React hooks: only at the top level of the calling function, and
116
+ // without any dynamic conditional logic.
84
117
  InternalState.prototype.useQuery = function (options) {
85
118
  var _this = this;
119
+ // The renderPromises field gets initialized here in the useQuery method, at
120
+ // the beginning of everything (for a given component rendering, at least),
121
+ // so we can safely use this.renderPromises in other/later InternalState
122
+ // methods without worrying it might be uninitialized. Even after
123
+ // initialization, this.renderPromises is usually undefined (unless SSR is
124
+ // happening), but that's fine as long as it has been initialized that way,
125
+ // rather than left uninitialized.
86
126
  this.renderPromises = React.useContext(getApolloContext()).renderPromises;
87
127
  this.useOptions(options);
88
128
  var obsQuery = this.useObservableQuery();
@@ -93,7 +133,11 @@ var InternalState = (function () {
93
133
  _this.forceUpdate = handleStoreChange;
94
134
  var onNext = function () {
95
135
  var previousResult = _this.result;
136
+ // We use `getCurrentResult()` instead of the onNext argument because
137
+ // the values differ slightly. Specifically, loading results will have
138
+ // an empty object for data instead of `undefined` for some reason.
96
139
  var result = obsQuery.getCurrentResult();
140
+ // Make sure we're not attempting to re-render similar results
97
141
  if (previousResult &&
98
142
  previousResult.loading === result.loading &&
99
143
  previousResult.networkStatus === result.networkStatus &&
@@ -106,6 +150,7 @@ var InternalState = (function () {
106
150
  subscription.unsubscribe();
107
151
  subscription = obsQuery.resubscribeAfterError(onNext, onError);
108
152
  if (!hasOwnProperty.call(error, "graphQLErrors")) {
153
+ // The error is not a GraphQL error
109
154
  throw error;
110
155
  }
111
156
  var previousResult = _this.result;
@@ -121,40 +166,83 @@ var InternalState = (function () {
121
166
  }
122
167
  };
123
168
  var subscription = obsQuery.subscribe(onNext, onError);
169
+ // Do the "unsubscribe" with a short delay.
170
+ // This way, an existing subscription can be reused without an additional
171
+ // request if "unsubscribe" and "resubscribe" to the same ObservableQuery
172
+ // happen in very fast succession.
124
173
  return function () {
125
174
  setTimeout(function () { return subscription.unsubscribe(); });
126
175
  _this.forceUpdate = function () { return _this.forceUpdateState(); };
127
176
  };
128
177
  }, [
178
+ // We memoize the subscribe function using useCallback and the following
179
+ // dependency keys, because the subscribe function reference is all that
180
+ // useSyncExternalStore uses internally as a dependency key for the
181
+ // useEffect ultimately responsible for the subscription, so we are
182
+ // effectively passing this dependency array to that useEffect buried
183
+ // inside useSyncExternalStore, as desired.
129
184
  obsQuery,
130
185
  this.renderPromises,
131
186
  this.client.disableNetworkFetches,
132
187
  ]), function () { return _this.getCurrentResult(); }, function () { return _this.getCurrentResult(); });
188
+ // TODO Remove this method when we remove support for options.partialRefetch.
133
189
  this.unsafeHandlePartialRefetch(result);
134
190
  return this.toQueryResult(result);
135
191
  };
136
192
  InternalState.prototype.useOptions = function (options) {
137
193
  var _a;
138
194
  var watchQueryOptions = this.createWatchQueryOptions((this.queryHookOptions = options));
195
+ // Update this.watchQueryOptions, but only when they have changed, which
196
+ // allows us to depend on the referential stability of
197
+ // this.watchQueryOptions elsewhere.
139
198
  var currentWatchQueryOptions = this.watchQueryOptions;
140
199
  if (!equal(watchQueryOptions, currentWatchQueryOptions)) {
141
200
  this.watchQueryOptions = watchQueryOptions;
142
201
  if (currentWatchQueryOptions && this.observable) {
202
+ // Though it might be tempting to postpone this reobserve call to the
203
+ // useEffect block, we need getCurrentResult to return an appropriate
204
+ // loading:true result synchronously (later within the same call to
205
+ // useQuery). Since we already have this.observable here (not true for
206
+ // the very first call to useQuery), we are not initiating any new
207
+ // subscriptions, though it does feel less than ideal that reobserve
208
+ // (potentially) kicks off a network request (for example, when the
209
+ // variables have changed), which is technically a side-effect.
143
210
  this.observable.reobserve(this.getObsQueryOptions());
211
+ // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,
212
+ // but save the current data as this.previousData, just like setResult
213
+ // usually does.
144
214
  this.previousData = ((_a = this.result) === null || _a === void 0 ? void 0 : _a.data) || this.previousData;
145
215
  this.result = void 0;
146
216
  }
147
217
  }
218
+ // Make sure state.onCompleted and state.onError always reflect the latest
219
+ // options.onCompleted and options.onError callbacks provided to useQuery,
220
+ // since those functions are often recreated every time useQuery is called.
221
+ // Like the forceUpdate method, the versions of these methods inherited from
222
+ // InternalState.prototype are empty no-ops, but we can override them on the
223
+ // base state object (without modifying the prototype).
148
224
  this.onCompleted =
149
225
  options.onCompleted || InternalState.prototype.onCompleted;
150
226
  this.onError = options.onError || InternalState.prototype.onError;
151
227
  if ((this.renderPromises || this.client.disableNetworkFetches) &&
152
228
  this.queryHookOptions.ssr === false &&
153
229
  !this.queryHookOptions.skip) {
230
+ // If SSR has been explicitly disabled, and this function has been called
231
+ // on the server side, return the default loading state.
154
232
  this.result = this.ssrDisabledResult;
155
233
  }
156
234
  else if (this.queryHookOptions.skip ||
157
235
  this.watchQueryOptions.fetchPolicy === "standby") {
236
+ // When skipping a query (ie. we're not querying for data but still want to
237
+ // render children), make sure the `data` is cleared out and `loading` is
238
+ // set to `false` (since we aren't loading anything).
239
+ //
240
+ // NOTE: We no longer think this is the correct behavior. Skipping should
241
+ // not automatically set `data` to `undefined`, but instead leave the
242
+ // previous data in place. In other words, skipping should not mandate that
243
+ // previously received data is all of a sudden removed. Unfortunately,
244
+ // changing this is breaking, so we'll have to wait until Apollo Client 4.0
245
+ // to address this.
158
246
  this.result = this.skipStandbyResult;
159
247
  }
160
248
  else if (this.result === this.ssrDisabledResult ||
@@ -170,17 +258,36 @@ var InternalState = (function () {
170
258
  if (this.queryHookOptions.defaultOptions) {
171
259
  toMerge.push(this.queryHookOptions.defaultOptions);
172
260
  }
261
+ // We use compact rather than mergeOptions for this part of the merge,
262
+ // because we want watchQueryOptions.variables (if defined) to replace
263
+ // this.observable.options.variables whole. This replacement allows
264
+ // removing variables by removing them from the variables input to
265
+ // useQuery. If the variables were always merged together (rather than
266
+ // replaced), there would be no way to remove existing variables.
267
+ // However, the variables from options.defaultOptions and globalDefaults
268
+ // (if provided) should be merged, to ensure individual defaulted
269
+ // variables always have values, if not otherwise defined in
270
+ // observable.options or watchQueryOptions.
173
271
  toMerge.push(compact(this.observable && this.observable.options, this.watchQueryOptions));
174
272
  return toMerge.reduce(mergeOptions);
175
273
  };
274
+ // A function to massage options before passing them to ObservableQuery.
176
275
  InternalState.prototype.createWatchQueryOptions = function (_a) {
177
276
  var _b;
178
277
  if (_a === void 0) { _a = {}; }
179
- var skip = _a.skip, ssr = _a.ssr, onCompleted = _a.onCompleted, onError = _a.onError, defaultOptions = _a.defaultOptions, otherOptions = __rest(_a, ["skip", "ssr", "onCompleted", "onError", "defaultOptions"]);
278
+ var skip = _a.skip, ssr = _a.ssr, onCompleted = _a.onCompleted, onError = _a.onError, defaultOptions = _a.defaultOptions,
279
+ // The above options are useQuery-specific, so this ...otherOptions spread
280
+ // makes otherOptions almost a WatchQueryOptions object, except for the
281
+ // query property that we add below.
282
+ otherOptions = __rest(_a, ["skip", "ssr", "onCompleted", "onError", "defaultOptions"]);
283
+ // This Object.assign is safe because otherOptions is a fresh ...rest object
284
+ // that did not exist until just now, so modifications are still allowed.
180
285
  var watchQueryOptions = Object.assign(otherOptions, { query: this.query });
181
286
  if (this.renderPromises &&
182
287
  (watchQueryOptions.fetchPolicy === "network-only" ||
183
288
  watchQueryOptions.fetchPolicy === "cache-and-network")) {
289
+ // this behavior was added to react-apollo without explanation in this PR
290
+ // https://github.com/apollographql/react-apollo/pull/1579
184
291
  watchQueryOptions.fetchPolicy = "cache-first";
185
292
  }
186
293
  if (!watchQueryOptions.variables) {
@@ -188,6 +295,9 @@ var InternalState = (function () {
188
295
  }
189
296
  if (skip) {
190
297
  var _c = watchQueryOptions.fetchPolicy, fetchPolicy = _c === void 0 ? this.getDefaultFetchPolicy() : _c, _d = watchQueryOptions.initialFetchPolicy, initialFetchPolicy = _d === void 0 ? fetchPolicy : _d;
298
+ // When skipping, we set watchQueryOptions.fetchPolicy initially to
299
+ // "standby", but we also need/want to preserve the initial non-standby
300
+ // fetchPolicy that would have been used if not skipping.
191
301
  Object.assign(watchQueryOptions, {
192
302
  initialFetchPolicy: initialFetchPolicy,
193
303
  fetchPolicy: "standby",
@@ -206,13 +316,19 @@ var InternalState = (function () {
206
316
  ((_b = this.client.defaultOptions.watchQuery) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
207
317
  "cache-first");
208
318
  };
319
+ // Defining these methods as no-ops on the prototype allows us to call
320
+ // state.onCompleted and/or state.onError without worrying about whether a
321
+ // callback was provided.
209
322
  InternalState.prototype.onCompleted = function (data) { };
210
323
  InternalState.prototype.onError = function (error) { };
211
324
  InternalState.prototype.useObservableQuery = function () {
325
+ // See if there is an existing observable that was used to fetch the same
326
+ // data and if so, use it instead since it will contain the proper queryId
327
+ // to fetch the result set. This is used during SSR.
212
328
  var obsQuery = (this.observable =
213
329
  (this.renderPromises &&
214
330
  this.renderPromises.getSSRObservable(this.watchQueryOptions)) ||
215
- this.observable ||
331
+ this.observable || // Reuse this.observable if possible (and not SSR)
216
332
  this.client.watchQuery(this.getObsQueryOptions()));
217
333
  this.obsQueryFields = React.useMemo(function () { return ({
218
334
  refetch: obsQuery.refetch.bind(obsQuery),
@@ -227,6 +343,7 @@ var InternalState = (function () {
227
343
  if (this.renderPromises && ssrAllowed) {
228
344
  this.renderPromises.registerSSRObservable(obsQuery);
229
345
  if (obsQuery.getCurrentResult().loading) {
346
+ // TODO: This is a legacy API which could probably be cleaned up
230
347
  this.renderPromises.addObservableQueryPromise(obsQuery);
231
348
  }
232
349
  }
@@ -238,6 +355,8 @@ var InternalState = (function () {
238
355
  this.previousData = previousResult.data;
239
356
  }
240
357
  this.result = nextResult;
358
+ // Calling state.setResult always triggers an update, though some call sites
359
+ // perform additional equality checks before committing to an update.
241
360
  this.forceUpdate();
242
361
  this.handleErrorOrCompleted(nextResult, previousResult);
243
362
  };
@@ -245,6 +364,7 @@ var InternalState = (function () {
245
364
  var _this = this;
246
365
  if (!result.loading) {
247
366
  var error_1 = this.toApolloError(result);
367
+ // wait a tick in case we are in the middle of rendering a component
248
368
  Promise.resolve()
249
369
  .then(function () {
250
370
  if (error_1) {
@@ -262,11 +382,14 @@ var InternalState = (function () {
262
382
  }
263
383
  };
264
384
  InternalState.prototype.toApolloError = function (result) {
265
- return isNonEmptyArray(result.errors)
266
- ? new ApolloError({ graphQLErrors: result.errors })
385
+ return isNonEmptyArray(result.errors) ?
386
+ new ApolloError({ graphQLErrors: result.errors })
267
387
  : result.error;
268
388
  };
269
389
  InternalState.prototype.getCurrentResult = function () {
390
+ // Using this.result as a cache ensures getCurrentResult continues returning
391
+ // the same (===) result object, unless state.setResult has been called, or
392
+ // we're doing server rendering and therefore override the result below.
270
393
  if (!this.result) {
271
394
  this.handleErrorOrCompleted((this.result = this.observable.getCurrentResult()));
272
395
  }
@@ -279,11 +402,20 @@ var InternalState = (function () {
279
402
  var data = result.data, partial = result.partial, resultWithoutPartial = __rest(result, ["data", "partial"]);
280
403
  this.toQueryResultCache.set(result, (queryResult = __assign(__assign(__assign({ data: data }, resultWithoutPartial), this.obsQueryFields), { client: this.client, observable: this.observable, variables: this.observable.variables, called: !this.queryHookOptions.skip, previousData: this.previousData })));
281
404
  if (!queryResult.error && isNonEmptyArray(result.errors)) {
405
+ // Until a set naming convention for networkError and graphQLErrors is
406
+ // decided upon, we map errors (graphQLErrors) to the error options.
407
+ // TODO: Is it possible for both result.error and result.errors to be
408
+ // defined here?
282
409
  queryResult.error = new ApolloError({ graphQLErrors: result.errors });
283
410
  }
284
411
  return queryResult;
285
412
  };
286
413
  InternalState.prototype.unsafeHandlePartialRefetch = function (result) {
414
+ // WARNING: SIDE-EFFECTS IN THE RENDER FUNCTION
415
+ //
416
+ // TODO: This code should be removed when the partialRefetch option is
417
+ // removed. I was unable to get this hook to behave reasonably in certain
418
+ // edge cases when this block was put in an effect.
287
419
  if (result.partial &&
288
420
  this.queryHookOptions.partialRefetch &&
289
421
  !result.loading &&
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAE7D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAMtC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AASpD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAQpD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,C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C,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;YACvD,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,WAAW,KAAK,YAAY,EACpD;YACA,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;gBACpB,OAAO,EAAE,IAAI;gBACb,aAAa,EAAE,aAAa,CAAC,OAAO;aACrC,CAAC,CAAC;YACH,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;SAC3B;IACH,CAAC;IACH,oBAAC;AAAD,CAAC,AApiBD,IAoiBC","sourcesContent":["import { invariant } from \"../../utilities/globals/index.js\";\n\nimport * as React from \"react\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport { equal } from \"@wry/equality\";\n\nimport type {\n OperationVariables,\n WatchQueryFetchPolicy,\n} from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport type { ApolloContextValue } from \"../context/index.js\";\nimport { getApolloContext } from \"../context/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport type {\n ApolloClient,\n ApolloQueryResult,\n ObservableQuery,\n DocumentNode,\n TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { NetworkStatus } from \"../../core/index.js\";\nimport type {\n QueryHookOptions,\n QueryResult,\n ObservableQueryFields,\n NoInfer,\n} from \"../types/types.js\";\n\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n canUseWeakMap,\n compact,\n isNonEmptyArray,\n maybeDeepFreeze,\n} from \"../../utilities/index.js\";\n\nconst {\n prototype: { hasOwnProperty },\n} = Object;\n\nexport function useQuery<\n TData = any,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: QueryHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>\n > = Object.create(null)\n): QueryResult<TData, TVariables> {\n return useInternalState(useApolloClient(options.client), query).useQuery(\n options\n );\n}\n\nexport function useInternalState<TData, TVariables extends OperationVariables>(\n client: ApolloClient<any>,\n query: DocumentNode | TypedDocumentNode<TData, TVariables>\n): InternalState<TData, TVariables> {\n const stateRef = React.useRef<InternalState<TData, TVariables>>();\n if (\n !stateRef.current ||\n client !== stateRef.current.client ||\n query !== stateRef.current.query\n ) {\n stateRef.current = new InternalState(client, query, stateRef.current);\n }\n const state = stateRef.current;\n\n // By default, InternalState.prototype.forceUpdate is an empty function, but\n // we replace it here (before anyone has had a chance to see this state yet)\n // with a function that unconditionally forces an update, using the latest\n // setTick function. Updating this state by calling state.forceUpdate is the\n // only way we trigger React component updates (no other useState calls within\n // the InternalState class).\n state.forceUpdateState = React.useReducer((tick) => tick + 1, 0)[1];\n\n return state;\n}\n\nclass InternalState<TData, TVariables extends OperationVariables> {\n constructor(\n public readonly client: ReturnType<typeof useApolloClient>,\n public readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n previous?: InternalState<TData, TVariables>\n ) {\n verifyDocumentType(query, DocumentType.Query);\n\n // Reuse previousData from previous InternalState (if any) to provide\n // continuity of previousData even if/when the query or client changes.\n const previousResult = previous && previous.result;\n const previousData = previousResult && previousResult.data;\n if (previousData) {\n this.previousData = previousData;\n }\n }\n\n /**\n * Forces an update using local component state.\n * As this is not batched with `useSyncExternalStore` updates,\n * this is only used as a fallback if the `useSyncExternalStore` \"force update\"\n * method is not registered at the moment.\n * See https://github.com/facebook/react/issues/25191\n * */\n forceUpdateState() {\n // Replaced (in useInternalState) with a method that triggers an update.\n invariant.warn(\n \"Calling default no-op implementation of InternalState#forceUpdate\"\n );\n }\n\n /**\n * Will be overwritten by the `useSyncExternalStore` \"force update\" method\n * whenever it is available and reset to `forceUpdateState` when it isn't.\n */\n forceUpdate = () => this.forceUpdateState();\n\n executeQuery(\n options: QueryHookOptions<TData, TVariables> & {\n query?: DocumentNode;\n }\n ) {\n if (options.query) {\n Object.assign(this, { query: options.query });\n }\n\n this.watchQueryOptions = this.createWatchQueryOptions(\n (this.queryHookOptions = options)\n );\n\n const concast = this.observable.reobserveAsConcast(\n this.getObsQueryOptions()\n );\n\n // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,\n // but save the current data as this.previousData, just like setResult\n // usually does.\n this.previousData = this.result?.data || this.previousData;\n this.result = void 0;\n this.forceUpdate();\n\n return new Promise<QueryResult<TData, TVariables>>((resolve) => {\n let result: ApolloQueryResult<TData>;\n\n // Subscribe to the concast independently of the ObservableQuery in case\n // the component gets unmounted before the promise resolves. This prevents\n // the concast from terminating early and resolving with `undefined` when\n // there are no more subscribers for the concast.\n concast.subscribe({\n next: (value) => {\n result = value;\n },\n error: () => {\n resolve(this.toQueryResult(this.observable.getCurrentResult()));\n },\n complete: () => {\n resolve(this.toQueryResult(result));\n },\n });\n });\n }\n\n // Methods beginning with use- should be called according to the standard\n // rules of React hooks: only at the top level of the calling function, and\n // without any dynamic conditional logic.\n useQuery(options: QueryHookOptions<TData, TVariables>) {\n // The renderPromises field gets initialized here in the useQuery method, at\n // the beginning of everything (for a given component rendering, at least),\n // so we can safely use this.renderPromises in other/later InternalState\n // methods without worrying it might be uninitialized. Even after\n // initialization, this.renderPromises is usually undefined (unless SSR is\n // happening), but that's fine as long as it has been initialized that way,\n // rather than left uninitialized.\n this.renderPromises = React.useContext(getApolloContext()).renderPromises;\n\n this.useOptions(options);\n\n const obsQuery = this.useObservableQuery();\n\n const result = useSyncExternalStore(\n React.useCallback(\n (handleStoreChange) => {\n if (this.renderPromises) {\n return () => {};\n }\n\n this.forceUpdate = handleStoreChange;\n\n const onNext = () => {\n const previousResult = this.result;\n // We use `getCurrentResult()` instead of the onNext argument because\n // the values differ slightly. Specifically, loading results will have\n // an empty object for data instead of `undefined` for some reason.\n const result = obsQuery.getCurrentResult();\n // Make sure we're not attempting to re-render similar results\n if (\n previousResult &&\n previousResult.loading === result.loading &&\n previousResult.networkStatus === result.networkStatus &&\n equal(previousResult.data, result.data)\n ) {\n return;\n }\n\n this.setResult(result);\n };\n\n const onError = (error: Error) => {\n subscription.unsubscribe();\n subscription = obsQuery.resubscribeAfterError(onNext, onError);\n\n if (!hasOwnProperty.call(error, \"graphQLErrors\")) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = this.result;\n if (\n !previousResult ||\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n this.setResult({\n data: (previousResult && previousResult.data) as TData,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n }\n };\n\n let subscription = obsQuery.subscribe(onNext, onError);\n\n // Do the \"unsubscribe\" with a short delay.\n // This way, an existing subscription can be reused without an additional\n // request if \"unsubscribe\" and \"resubscribe\" to the same ObservableQuery\n // happen in very fast succession.\n return () => {\n setTimeout(() => subscription.unsubscribe());\n this.forceUpdate = () => this.forceUpdateState();\n };\n },\n [\n // We memoize the subscribe function using useCallback and the following\n // dependency keys, because the subscribe function reference is all that\n // useSyncExternalStore uses internally as a dependency key for the\n // useEffect ultimately responsible for the subscription, so we are\n // effectively passing this dependency array to that useEffect buried\n // inside useSyncExternalStore, as desired.\n obsQuery,\n this.renderPromises,\n this.client.disableNetworkFetches,\n ]\n ),\n\n () => this.getCurrentResult(),\n () => this.getCurrentResult()\n );\n\n // TODO Remove this method when we remove support for options.partialRefetch.\n this.unsafeHandlePartialRefetch(result);\n\n return this.toQueryResult(result);\n }\n\n // These members (except for renderPromises) are all populated by the\n // useOptions method, which is called unconditionally at the beginning of the\n // useQuery method, so we can safely use these members in other/later methods\n // without worrying they might be uninitialized.\n private renderPromises: ApolloContextValue[\"renderPromises\"];\n private queryHookOptions: QueryHookOptions<TData, TVariables>;\n private watchQueryOptions: WatchQueryOptions<TVariables, TData>;\n\n private useOptions(options: QueryHookOptions<TData, TVariables>) {\n const watchQueryOptions = this.createWatchQueryOptions(\n (this.queryHookOptions = options)\n );\n\n // Update this.watchQueryOptions, but only when they have changed, which\n // allows us to depend on the referential stability of\n // this.watchQueryOptions elsewhere.\n const currentWatchQueryOptions = this.watchQueryOptions;\n\n if (!equal(watchQueryOptions, currentWatchQueryOptions)) {\n this.watchQueryOptions = watchQueryOptions;\n\n if (currentWatchQueryOptions && this.observable) {\n // Though it might be tempting to postpone this reobserve call to the\n // useEffect block, we need getCurrentResult to return an appropriate\n // loading:true result synchronously (later within the same call to\n // useQuery). Since we already have this.observable here (not true for\n // the very first call to useQuery), we are not initiating any new\n // subscriptions, though it does feel less than ideal that reobserve\n // (potentially) kicks off a network request (for example, when the\n // variables have changed), which is technically a side-effect.\n this.observable.reobserve(this.getObsQueryOptions());\n\n // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,\n // but save the current data as this.previousData, just like setResult\n // usually does.\n this.previousData = this.result?.data || this.previousData;\n this.result = void 0;\n }\n }\n\n // Make sure state.onCompleted and state.onError always reflect the latest\n // options.onCompleted and options.onError callbacks provided to useQuery,\n // since those functions are often recreated every time useQuery is called.\n // Like the forceUpdate method, the versions of these methods inherited from\n // InternalState.prototype are empty no-ops, but we can override them on the\n // base state object (without modifying the prototype).\n this.onCompleted =\n options.onCompleted || InternalState.prototype.onCompleted;\n this.onError = options.onError || InternalState.prototype.onError;\n\n if (\n (this.renderPromises || this.client.disableNetworkFetches) &&\n this.queryHookOptions.ssr === false &&\n !this.queryHookOptions.skip\n ) {\n // If SSR has been explicitly disabled, and this function has been called\n // on the server side, return the default loading state.\n this.result = this.ssrDisabledResult;\n } else if (\n this.queryHookOptions.skip ||\n this.watchQueryOptions.fetchPolicy === \"standby\"\n ) {\n // When skipping a query (ie. we're not querying for data but still want to\n // render children), make sure the `data` is cleared out and `loading` is\n // set to `false` (since we aren't loading anything).\n //\n // NOTE: We no longer think this is the correct behavior. Skipping should\n // not automatically set `data` to `undefined`, but instead leave the\n // previous data in place. In other words, skipping should not mandate that\n // previously received data is all of a sudden removed. Unfortunately,\n // changing this is breaking, so we'll have to wait until Apollo Client 4.0\n // to address this.\n this.result = this.skipStandbyResult;\n } else if (\n this.result === this.ssrDisabledResult ||\n this.result === this.skipStandbyResult\n ) {\n this.result = void 0;\n }\n }\n\n private getObsQueryOptions(): WatchQueryOptions<TVariables, TData> {\n const toMerge: Array<Partial<WatchQueryOptions<TVariables, TData>>> = [];\n\n const globalDefaults = this.client.defaultOptions.watchQuery;\n if (globalDefaults) toMerge.push(globalDefaults);\n\n if (this.queryHookOptions.defaultOptions) {\n toMerge.push(this.queryHookOptions.defaultOptions);\n }\n\n // We use compact rather than mergeOptions for this part of the merge,\n // because we want watchQueryOptions.variables (if defined) to replace\n // this.observable.options.variables whole. This replacement allows\n // removing variables by removing them from the variables input to\n // useQuery. If the variables were always merged together (rather than\n // replaced), there would be no way to remove existing variables.\n // However, the variables from options.defaultOptions and globalDefaults\n // (if provided) should be merged, to ensure individual defaulted\n // variables always have values, if not otherwise defined in\n // observable.options or watchQueryOptions.\n toMerge.push(\n compact(\n this.observable && this.observable.options,\n this.watchQueryOptions\n )\n );\n\n return toMerge.reduce(mergeOptions) as WatchQueryOptions<TVariables, TData>;\n }\n\n private ssrDisabledResult = maybeDeepFreeze({\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n });\n\n private skipStandbyResult = maybeDeepFreeze({\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.ready,\n });\n\n // A function to massage options before passing them to ObservableQuery.\n private createWatchQueryOptions({\n skip,\n ssr,\n onCompleted,\n onError,\n defaultOptions,\n // The above options are useQuery-specific, so this ...otherOptions spread\n // makes otherOptions almost a WatchQueryOptions object, except for the\n // query property that we add below.\n ...otherOptions\n }: QueryHookOptions<TData, TVariables> = {}): WatchQueryOptions<\n TVariables,\n TData\n > {\n // This Object.assign is safe because otherOptions is a fresh ...rest object\n // that did not exist until just now, so modifications are still allowed.\n const watchQueryOptions: WatchQueryOptions<TVariables, TData> =\n Object.assign(otherOptions, { query: this.query });\n\n if (\n this.renderPromises &&\n (watchQueryOptions.fetchPolicy === \"network-only\" ||\n watchQueryOptions.fetchPolicy === \"cache-and-network\")\n ) {\n // this behavior was added to react-apollo without explanation in this PR\n // https://github.com/apollographql/react-apollo/pull/1579\n watchQueryOptions.fetchPolicy = \"cache-first\";\n }\n\n if (!watchQueryOptions.variables) {\n watchQueryOptions.variables = {} as TVariables;\n }\n\n if (skip) {\n const {\n fetchPolicy = this.getDefaultFetchPolicy(),\n initialFetchPolicy = fetchPolicy,\n } = watchQueryOptions;\n\n // When skipping, we set watchQueryOptions.fetchPolicy initially to\n // \"standby\", but we also need/want to preserve the initial non-standby\n // fetchPolicy that would have been used if not skipping.\n Object.assign(watchQueryOptions, {\n initialFetchPolicy,\n fetchPolicy: \"standby\",\n });\n } else if (!watchQueryOptions.fetchPolicy) {\n watchQueryOptions.fetchPolicy =\n this.observable?.options.initialFetchPolicy ||\n this.getDefaultFetchPolicy();\n }\n\n return watchQueryOptions;\n }\n\n getDefaultFetchPolicy(): WatchQueryFetchPolicy {\n return (\n this.queryHookOptions.defaultOptions?.fetchPolicy ||\n this.client.defaultOptions.watchQuery?.fetchPolicy ||\n \"cache-first\"\n );\n }\n\n // Defining these methods as no-ops on the prototype allows us to call\n // state.onCompleted and/or state.onError without worrying about whether a\n // callback was provided.\n private onCompleted(data: TData) {}\n private onError(error: ApolloError) {}\n\n private observable: ObservableQuery<TData, TVariables>;\n private obsQueryFields: Omit<\n ObservableQueryFields<TData, TVariables>,\n \"variables\"\n >;\n\n private useObservableQuery() {\n // See if there is an existing observable that was used to fetch the same\n // data and if so, use it instead since it will contain the proper queryId\n // to fetch the result set. This is used during SSR.\n const obsQuery = (this.observable =\n (this.renderPromises &&\n this.renderPromises.getSSRObservable(this.watchQueryOptions)) ||\n this.observable || // Reuse this.observable if possible (and not SSR)\n this.client.watchQuery(this.getObsQueryOptions()));\n\n this.obsQueryFields = React.useMemo(\n () => ({\n refetch: obsQuery.refetch.bind(obsQuery),\n reobserve: obsQuery.reobserve.bind(obsQuery),\n fetchMore: obsQuery.fetchMore.bind(obsQuery),\n updateQuery: obsQuery.updateQuery.bind(obsQuery),\n startPolling: obsQuery.startPolling.bind(obsQuery),\n stopPolling: obsQuery.stopPolling.bind(obsQuery),\n subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),\n }),\n [obsQuery]\n );\n\n const ssrAllowed = !(\n this.queryHookOptions.ssr === false || this.queryHookOptions.skip\n );\n\n if (this.renderPromises && ssrAllowed) {\n this.renderPromises.registerSSRObservable(obsQuery);\n\n if (obsQuery.getCurrentResult().loading) {\n // TODO: This is a legacy API which could probably be cleaned up\n this.renderPromises.addObservableQueryPromise(obsQuery);\n }\n }\n\n return obsQuery;\n }\n\n // These members are populated by getCurrentResult and setResult, and it's\n // okay/normal for them to be initially undefined.\n private result: undefined | ApolloQueryResult<TData>;\n private previousData: undefined | TData;\n\n private setResult(nextResult: ApolloQueryResult<TData>) {\n const previousResult = this.result;\n if (previousResult && previousResult.data) {\n this.previousData = previousResult.data;\n }\n this.result = nextResult;\n // Calling state.setResult always triggers an update, though some call sites\n // perform additional equality checks before committing to an update.\n this.forceUpdate();\n this.handleErrorOrCompleted(nextResult, previousResult);\n }\n\n private handleErrorOrCompleted(\n result: ApolloQueryResult<TData>,\n previousResult?: ApolloQueryResult<TData>\n ) {\n if (!result.loading) {\n const error = this.toApolloError(result);\n\n // wait a tick in case we are in the middle of rendering a component\n Promise.resolve()\n .then(() => {\n if (error) {\n this.onError(error);\n } else if (\n result.data &&\n previousResult?.networkStatus !== result.networkStatus &&\n result.networkStatus === NetworkStatus.ready\n ) {\n this.onCompleted(result.data);\n }\n })\n .catch((error) => {\n invariant.warn(error);\n });\n }\n }\n\n private toApolloError(\n result: ApolloQueryResult<TData>\n ): ApolloError | undefined {\n return isNonEmptyArray(result.errors)\n ? new ApolloError({ graphQLErrors: result.errors })\n : result.error;\n }\n\n private getCurrentResult(): ApolloQueryResult<TData> {\n // Using this.result as a cache ensures getCurrentResult continues returning\n // the same (===) result object, unless state.setResult has been called, or\n // we're doing server rendering and therefore override the result below.\n if (!this.result) {\n this.handleErrorOrCompleted(\n (this.result = this.observable.getCurrentResult())\n );\n }\n return this.result;\n }\n\n // This cache allows the referential stability of this.result (as returned by\n // getCurrentResult) to translate into referential stability of the resulting\n // QueryResult object returned by toQueryResult.\n private toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)<\n ApolloQueryResult<TData>,\n QueryResult<TData, TVariables>\n >();\n\n toQueryResult(\n result: ApolloQueryResult<TData>\n ): QueryResult<TData, TVariables> {\n let queryResult = this.toQueryResultCache.get(result);\n if (queryResult) return queryResult;\n\n const { data, partial, ...resultWithoutPartial } = result;\n this.toQueryResultCache.set(\n result,\n (queryResult = {\n data, // Ensure always defined, even if result.data is missing.\n ...resultWithoutPartial,\n ...this.obsQueryFields,\n client: this.client,\n observable: this.observable,\n variables: this.observable.variables,\n called: !this.queryHookOptions.skip,\n previousData: this.previousData,\n })\n );\n\n if (!queryResult.error && isNonEmptyArray(result.errors)) {\n // Until a set naming convention for networkError and graphQLErrors is\n // decided upon, we map errors (graphQLErrors) to the error options.\n // TODO: Is it possible for both result.error and result.errors to be\n // defined here?\n queryResult.error = new ApolloError({ graphQLErrors: result.errors });\n }\n\n return queryResult;\n }\n\n private unsafeHandlePartialRefetch(result: ApolloQueryResult<TData>) {\n // WARNING: SIDE-EFFECTS IN THE RENDER FUNCTION\n //\n // TODO: This code should be removed when the partialRefetch option is\n // removed. I was unable to get this hook to behave reasonably in certain\n // edge cases when this block was put in an effect.\n if (\n result.partial &&\n this.queryHookOptions.partialRefetch &&\n !result.loading &&\n (!result.data || Object.keys(result.data).length === 0) &&\n this.observable.options.fetchPolicy !== \"cache-only\"\n ) {\n Object.assign(result, {\n loading: true,\n networkStatus: NetworkStatus.refetch,\n });\n this.observable.refetch();\n }\n }\n}\n"]}
1
+ {"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":";AAAA,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AAE;QACF,sEAAsE;QACtE,yEAAyE;QACzE,mDAAmD;QACnD,IACE,MAAM,CAAC,OAAO;YACd,IAAI,CAAC,gBAAgB,CAAC,cAAc;YACpC,CAAC,MAAM,CAAC,OAAO;YACf,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;YACvD,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,WAAW,KAAK,YAAY,EACpD,CAAC;YACD,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;gBACpB,OAAO,EAAE,IAAI;gBACb,aAAa,EAAE,aAAa,CAAC,OAAO;aACrC,CAAC,CAAC;YACH,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;QAC5B,CAAC;IACH,CAAC;IACH,oBAAC;AAAD,CAAC,AApiBD,IAoiBC","sourcesContent":["import { invariant } from \"../../utilities/globals/index.js\";\n\nimport * as React from \"react\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport { equal } from \"@wry/equality\";\n\nimport type {\n OperationVariables,\n WatchQueryFetchPolicy,\n} from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport type { ApolloContextValue } from \"../context/index.js\";\nimport { getApolloContext } from \"../context/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport type {\n ApolloClient,\n ApolloQueryResult,\n ObservableQuery,\n DocumentNode,\n TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { NetworkStatus } from \"../../core/index.js\";\nimport type {\n QueryHookOptions,\n QueryResult,\n ObservableQueryFields,\n NoInfer,\n} from \"../types/types.js\";\n\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n canUseWeakMap,\n compact,\n isNonEmptyArray,\n maybeDeepFreeze,\n} from \"../../utilities/index.js\";\n\nconst {\n prototype: { hasOwnProperty },\n} = Object;\n\nexport function useQuery<\n TData = any,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: QueryHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>\n > = Object.create(null)\n): QueryResult<TData, TVariables> {\n return useInternalState(useApolloClient(options.client), query).useQuery(\n options\n );\n}\n\nexport function useInternalState<TData, TVariables extends OperationVariables>(\n client: ApolloClient<any>,\n query: DocumentNode | TypedDocumentNode<TData, TVariables>\n): InternalState<TData, TVariables> {\n const stateRef = React.useRef<InternalState<TData, TVariables>>();\n if (\n !stateRef.current ||\n client !== stateRef.current.client ||\n query !== stateRef.current.query\n ) {\n stateRef.current = new InternalState(client, query, stateRef.current);\n }\n const state = stateRef.current;\n\n // By default, InternalState.prototype.forceUpdate is an empty function, but\n // we replace it here (before anyone has had a chance to see this state yet)\n // with a function that unconditionally forces an update, using the latest\n // setTick function. Updating this state by calling state.forceUpdate is the\n // only way we trigger React component updates (no other useState calls within\n // the InternalState class).\n state.forceUpdateState = React.useReducer((tick) => tick + 1, 0)[1];\n\n return state;\n}\n\nclass InternalState<TData, TVariables extends OperationVariables> {\n constructor(\n public readonly client: ReturnType<typeof useApolloClient>,\n public readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n previous?: InternalState<TData, TVariables>\n ) {\n verifyDocumentType(query, DocumentType.Query);\n\n // Reuse previousData from previous InternalState (if any) to provide\n // continuity of previousData even if/when the query or client changes.\n const previousResult = previous && previous.result;\n const previousData = previousResult && previousResult.data;\n if (previousData) {\n this.previousData = previousData;\n }\n }\n\n /**\n * Forces an update using local component state.\n * As this is not batched with `useSyncExternalStore` updates,\n * this is only used as a fallback if the `useSyncExternalStore` \"force update\"\n * method is not registered at the moment.\n * See https://github.com/facebook/react/issues/25191\n * */\n forceUpdateState() {\n // Replaced (in useInternalState) with a method that triggers an update.\n invariant.warn(\n \"Calling default no-op implementation of InternalState#forceUpdate\"\n );\n }\n\n /**\n * Will be overwritten by the `useSyncExternalStore` \"force update\" method\n * whenever it is available and reset to `forceUpdateState` when it isn't.\n */\n forceUpdate = () => this.forceUpdateState();\n\n executeQuery(\n options: QueryHookOptions<TData, TVariables> & {\n query?: DocumentNode;\n }\n ) {\n if (options.query) {\n Object.assign(this, { query: options.query });\n }\n\n this.watchQueryOptions = this.createWatchQueryOptions(\n (this.queryHookOptions = options)\n );\n\n const concast = this.observable.reobserveAsConcast(\n this.getObsQueryOptions()\n );\n\n // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,\n // but save the current data as this.previousData, just like setResult\n // usually does.\n this.previousData = this.result?.data || this.previousData;\n this.result = void 0;\n this.forceUpdate();\n\n return new Promise<QueryResult<TData, TVariables>>((resolve) => {\n let result: ApolloQueryResult<TData>;\n\n // Subscribe to the concast independently of the ObservableQuery in case\n // the component gets unmounted before the promise resolves. This prevents\n // the concast from terminating early and resolving with `undefined` when\n // there are no more subscribers for the concast.\n concast.subscribe({\n next: (value) => {\n result = value;\n },\n error: () => {\n resolve(this.toQueryResult(this.observable.getCurrentResult()));\n },\n complete: () => {\n resolve(this.toQueryResult(result));\n },\n });\n });\n }\n\n // Methods beginning with use- should be called according to the standard\n // rules of React hooks: only at the top level of the calling function, and\n // without any dynamic conditional logic.\n useQuery(options: QueryHookOptions<TData, TVariables>) {\n // The renderPromises field gets initialized here in the useQuery method, at\n // the beginning of everything (for a given component rendering, at least),\n // so we can safely use this.renderPromises in other/later InternalState\n // methods without worrying it might be uninitialized. Even after\n // initialization, this.renderPromises is usually undefined (unless SSR is\n // happening), but that's fine as long as it has been initialized that way,\n // rather than left uninitialized.\n this.renderPromises = React.useContext(getApolloContext()).renderPromises;\n\n this.useOptions(options);\n\n const obsQuery = this.useObservableQuery();\n\n const result = useSyncExternalStore(\n React.useCallback(\n (handleStoreChange) => {\n if (this.renderPromises) {\n return () => {};\n }\n\n this.forceUpdate = handleStoreChange;\n\n const onNext = () => {\n const previousResult = this.result;\n // We use `getCurrentResult()` instead of the onNext argument because\n // the values differ slightly. Specifically, loading results will have\n // an empty object for data instead of `undefined` for some reason.\n const result = obsQuery.getCurrentResult();\n // Make sure we're not attempting to re-render similar results\n if (\n previousResult &&\n previousResult.loading === result.loading &&\n previousResult.networkStatus === result.networkStatus &&\n equal(previousResult.data, result.data)\n ) {\n return;\n }\n\n this.setResult(result);\n };\n\n const onError = (error: Error) => {\n subscription.unsubscribe();\n subscription = obsQuery.resubscribeAfterError(onNext, onError);\n\n if (!hasOwnProperty.call(error, \"graphQLErrors\")) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = this.result;\n if (\n !previousResult ||\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n this.setResult({\n data: (previousResult && previousResult.data) as TData,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n }\n };\n\n let subscription = obsQuery.subscribe(onNext, onError);\n\n // Do the \"unsubscribe\" with a short delay.\n // This way, an existing subscription can be reused without an additional\n // request if \"unsubscribe\" and \"resubscribe\" to the same ObservableQuery\n // happen in very fast succession.\n return () => {\n setTimeout(() => subscription.unsubscribe());\n this.forceUpdate = () => this.forceUpdateState();\n };\n },\n [\n // We memoize the subscribe function using useCallback and the following\n // dependency keys, because the subscribe function reference is all that\n // useSyncExternalStore uses internally as a dependency key for the\n // useEffect ultimately responsible for the subscription, so we are\n // effectively passing this dependency array to that useEffect buried\n // inside useSyncExternalStore, as desired.\n obsQuery,\n this.renderPromises,\n this.client.disableNetworkFetches,\n ]\n ),\n\n () => this.getCurrentResult(),\n () => this.getCurrentResult()\n );\n\n // TODO Remove this method when we remove support for options.partialRefetch.\n this.unsafeHandlePartialRefetch(result);\n\n return this.toQueryResult(result);\n }\n\n // These members (except for renderPromises) are all populated by the\n // useOptions method, which is called unconditionally at the beginning of the\n // useQuery method, so we can safely use these members in other/later methods\n // without worrying they might be uninitialized.\n private renderPromises: ApolloContextValue[\"renderPromises\"];\n private queryHookOptions!: QueryHookOptions<TData, TVariables>;\n private watchQueryOptions!: WatchQueryOptions<TVariables, TData>;\n\n private useOptions(options: QueryHookOptions<TData, TVariables>) {\n const watchQueryOptions = this.createWatchQueryOptions(\n (this.queryHookOptions = options)\n );\n\n // Update this.watchQueryOptions, but only when they have changed, which\n // allows us to depend on the referential stability of\n // this.watchQueryOptions elsewhere.\n const currentWatchQueryOptions = this.watchQueryOptions;\n\n if (!equal(watchQueryOptions, currentWatchQueryOptions)) {\n this.watchQueryOptions = watchQueryOptions;\n\n if (currentWatchQueryOptions && this.observable) {\n // Though it might be tempting to postpone this reobserve call to the\n // useEffect block, we need getCurrentResult to return an appropriate\n // loading:true result synchronously (later within the same call to\n // useQuery). Since we already have this.observable here (not true for\n // the very first call to useQuery), we are not initiating any new\n // subscriptions, though it does feel less than ideal that reobserve\n // (potentially) kicks off a network request (for example, when the\n // variables have changed), which is technically a side-effect.\n this.observable.reobserve(this.getObsQueryOptions());\n\n // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,\n // but save the current data as this.previousData, just like setResult\n // usually does.\n this.previousData = this.result?.data || this.previousData;\n this.result = void 0;\n }\n }\n\n // Make sure state.onCompleted and state.onError always reflect the latest\n // options.onCompleted and options.onError callbacks provided to useQuery,\n // since those functions are often recreated every time useQuery is called.\n // Like the forceUpdate method, the versions of these methods inherited from\n // InternalState.prototype are empty no-ops, but we can override them on the\n // base state object (without modifying the prototype).\n this.onCompleted =\n options.onCompleted || InternalState.prototype.onCompleted;\n this.onError = options.onError || InternalState.prototype.onError;\n\n if (\n (this.renderPromises || this.client.disableNetworkFetches) &&\n this.queryHookOptions.ssr === false &&\n !this.queryHookOptions.skip\n ) {\n // If SSR has been explicitly disabled, and this function has been called\n // on the server side, return the default loading state.\n this.result = this.ssrDisabledResult;\n } else if (\n this.queryHookOptions.skip ||\n this.watchQueryOptions.fetchPolicy === \"standby\"\n ) {\n // When skipping a query (ie. we're not querying for data but still want to\n // render children), make sure the `data` is cleared out and `loading` is\n // set to `false` (since we aren't loading anything).\n //\n // NOTE: We no longer think this is the correct behavior. Skipping should\n // not automatically set `data` to `undefined`, but instead leave the\n // previous data in place. In other words, skipping should not mandate that\n // previously received data is all of a sudden removed. Unfortunately,\n // changing this is breaking, so we'll have to wait until Apollo Client 4.0\n // to address this.\n this.result = this.skipStandbyResult;\n } else if (\n this.result === this.ssrDisabledResult ||\n this.result === this.skipStandbyResult\n ) {\n this.result = void 0;\n }\n }\n\n private getObsQueryOptions(): WatchQueryOptions<TVariables, TData> {\n const toMerge: Array<Partial<WatchQueryOptions<TVariables, TData>>> = [];\n\n const globalDefaults = this.client.defaultOptions.watchQuery;\n if (globalDefaults) toMerge.push(globalDefaults);\n\n if (this.queryHookOptions.defaultOptions) {\n toMerge.push(this.queryHookOptions.defaultOptions);\n }\n\n // We use compact rather than mergeOptions for this part of the merge,\n // because we want watchQueryOptions.variables (if defined) to replace\n // this.observable.options.variables whole. This replacement allows\n // removing variables by removing them from the variables input to\n // useQuery. If the variables were always merged together (rather than\n // replaced), there would be no way to remove existing variables.\n // However, the variables from options.defaultOptions and globalDefaults\n // (if provided) should be merged, to ensure individual defaulted\n // variables always have values, if not otherwise defined in\n // observable.options or watchQueryOptions.\n toMerge.push(\n compact(\n this.observable && this.observable.options,\n this.watchQueryOptions\n )\n );\n\n return toMerge.reduce(mergeOptions) as WatchQueryOptions<TVariables, TData>;\n }\n\n private ssrDisabledResult = maybeDeepFreeze({\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n });\n\n private skipStandbyResult = maybeDeepFreeze({\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.ready,\n });\n\n // A function to massage options before passing them to ObservableQuery.\n private createWatchQueryOptions({\n skip,\n ssr,\n onCompleted,\n onError,\n defaultOptions,\n // The above options are useQuery-specific, so this ...otherOptions spread\n // makes otherOptions almost a WatchQueryOptions object, except for the\n // query property that we add below.\n ...otherOptions\n }: QueryHookOptions<TData, TVariables> = {}): WatchQueryOptions<\n TVariables,\n TData\n > {\n // This Object.assign is safe because otherOptions is a fresh ...rest object\n // that did not exist until just now, so modifications are still allowed.\n const watchQueryOptions: WatchQueryOptions<TVariables, TData> =\n Object.assign(otherOptions, { query: this.query });\n\n if (\n this.renderPromises &&\n (watchQueryOptions.fetchPolicy === \"network-only\" ||\n watchQueryOptions.fetchPolicy === \"cache-and-network\")\n ) {\n // this behavior was added to react-apollo without explanation in this PR\n // https://github.com/apollographql/react-apollo/pull/1579\n watchQueryOptions.fetchPolicy = \"cache-first\";\n }\n\n if (!watchQueryOptions.variables) {\n watchQueryOptions.variables = {} as TVariables;\n }\n\n if (skip) {\n const {\n fetchPolicy = this.getDefaultFetchPolicy(),\n initialFetchPolicy = fetchPolicy,\n } = watchQueryOptions;\n\n // When skipping, we set watchQueryOptions.fetchPolicy initially to\n // \"standby\", but we also need/want to preserve the initial non-standby\n // fetchPolicy that would have been used if not skipping.\n Object.assign(watchQueryOptions, {\n initialFetchPolicy,\n fetchPolicy: \"standby\",\n });\n } else if (!watchQueryOptions.fetchPolicy) {\n watchQueryOptions.fetchPolicy =\n this.observable?.options.initialFetchPolicy ||\n this.getDefaultFetchPolicy();\n }\n\n return watchQueryOptions;\n }\n\n getDefaultFetchPolicy(): WatchQueryFetchPolicy {\n return (\n this.queryHookOptions.defaultOptions?.fetchPolicy ||\n this.client.defaultOptions.watchQuery?.fetchPolicy ||\n \"cache-first\"\n );\n }\n\n // Defining these methods as no-ops on the prototype allows us to call\n // state.onCompleted and/or state.onError without worrying about whether a\n // callback was provided.\n private onCompleted(data: TData) {}\n private onError(error: ApolloError) {}\n\n private observable!: ObservableQuery<TData, TVariables>;\n private obsQueryFields!: Omit<\n ObservableQueryFields<TData, TVariables>,\n \"variables\"\n >;\n\n private useObservableQuery() {\n // See if there is an existing observable that was used to fetch the same\n // data and if so, use it instead since it will contain the proper queryId\n // to fetch the result set. This is used during SSR.\n const obsQuery = (this.observable =\n (this.renderPromises &&\n this.renderPromises.getSSRObservable(this.watchQueryOptions)) ||\n this.observable || // Reuse this.observable if possible (and not SSR)\n this.client.watchQuery(this.getObsQueryOptions()));\n\n this.obsQueryFields = React.useMemo(\n () => ({\n refetch: obsQuery.refetch.bind(obsQuery),\n reobserve: obsQuery.reobserve.bind(obsQuery),\n fetchMore: obsQuery.fetchMore.bind(obsQuery),\n updateQuery: obsQuery.updateQuery.bind(obsQuery),\n startPolling: obsQuery.startPolling.bind(obsQuery),\n stopPolling: obsQuery.stopPolling.bind(obsQuery),\n subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),\n }),\n [obsQuery]\n );\n\n const ssrAllowed = !(\n this.queryHookOptions.ssr === false || this.queryHookOptions.skip\n );\n\n if (this.renderPromises && ssrAllowed) {\n this.renderPromises.registerSSRObservable(obsQuery);\n\n if (obsQuery.getCurrentResult().loading) {\n // TODO: This is a legacy API which could probably be cleaned up\n this.renderPromises.addObservableQueryPromise(obsQuery);\n }\n }\n\n return obsQuery;\n }\n\n // These members are populated by getCurrentResult and setResult, and it's\n // okay/normal for them to be initially undefined.\n private result: undefined | ApolloQueryResult<TData>;\n private previousData: undefined | TData;\n\n private setResult(nextResult: ApolloQueryResult<TData>) {\n const previousResult = this.result;\n if (previousResult && previousResult.data) {\n this.previousData = previousResult.data;\n }\n this.result = nextResult;\n // Calling state.setResult always triggers an update, though some call sites\n // perform additional equality checks before committing to an update.\n this.forceUpdate();\n this.handleErrorOrCompleted(nextResult, previousResult);\n }\n\n private handleErrorOrCompleted(\n result: ApolloQueryResult<TData>,\n previousResult?: ApolloQueryResult<TData>\n ) {\n if (!result.loading) {\n const error = this.toApolloError(result);\n\n // wait a tick in case we are in the middle of rendering a component\n Promise.resolve()\n .then(() => {\n if (error) {\n this.onError(error);\n } else if (\n result.data &&\n previousResult?.networkStatus !== result.networkStatus &&\n result.networkStatus === NetworkStatus.ready\n ) {\n this.onCompleted(result.data);\n }\n })\n .catch((error) => {\n invariant.warn(error);\n });\n }\n }\n\n private toApolloError(\n result: ApolloQueryResult<TData>\n ): ApolloError | undefined {\n return isNonEmptyArray(result.errors) ?\n new ApolloError({ graphQLErrors: result.errors })\n : result.error;\n }\n\n private getCurrentResult(): ApolloQueryResult<TData> {\n // Using this.result as a cache ensures getCurrentResult continues returning\n // the same (===) result object, unless state.setResult has been called, or\n // we're doing server rendering and therefore override the result below.\n if (!this.result) {\n this.handleErrorOrCompleted(\n (this.result = this.observable.getCurrentResult())\n );\n }\n return this.result;\n }\n\n // This cache allows the referential stability of this.result (as returned by\n // getCurrentResult) to translate into referential stability of the resulting\n // QueryResult object returned by toQueryResult.\n private toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)<\n ApolloQueryResult<TData>,\n QueryResult<TData, TVariables>\n >();\n\n toQueryResult(\n result: ApolloQueryResult<TData>\n ): QueryResult<TData, TVariables> {\n let queryResult = this.toQueryResultCache.get(result);\n if (queryResult) return queryResult;\n\n const { data, partial, ...resultWithoutPartial } = result;\n this.toQueryResultCache.set(\n result,\n (queryResult = {\n data, // Ensure always defined, even if result.data is missing.\n ...resultWithoutPartial,\n ...this.obsQueryFields,\n client: this.client,\n observable: this.observable,\n variables: this.observable.variables,\n called: !this.queryHookOptions.skip,\n previousData: this.previousData,\n })\n );\n\n if (!queryResult.error && isNonEmptyArray(result.errors)) {\n // Until a set naming convention for networkError and graphQLErrors is\n // decided upon, we map errors (graphQLErrors) to the error options.\n // TODO: Is it possible for both result.error and result.errors to be\n // defined here?\n queryResult.error = new ApolloError({ graphQLErrors: result.errors });\n }\n\n return queryResult;\n }\n\n private unsafeHandlePartialRefetch(result: ApolloQueryResult<TData>) {\n // WARNING: SIDE-EFFECTS IN THE RENDER FUNCTION\n //\n // TODO: This code should be removed when the partialRefetch option is\n // removed. I was unable to get this hook to behave reasonably in certain\n // edge cases when this block was put in an effect.\n if (\n result.partial &&\n this.queryHookOptions.partialRefetch &&\n !result.loading &&\n (!result.data || Object.keys(result.data).length === 0) &&\n this.observable.options.fetchPolicy !== \"cache-only\"\n ) {\n Object.assign(result, {\n loading: true,\n networkStatus: NetworkStatus.refetch,\n });\n this.observable.refetch();\n }\n }\n}\n"]}
@@ -2,6 +2,11 @@ import * as React from "react";
2
2
  import { useSyncExternalStore } from "./useSyncExternalStore.js";
3
3
  export function useReactiveVar(rv) {
4
4
  return useSyncExternalStore(React.useCallback(function (update) {
5
+ // By reusing the same onNext function in the nested call to
6
+ // rv.onNextChange(onNext), we can keep using the initial clean-up function
7
+ // returned by rv.onNextChange(function onNext(v){...}), without having to
8
+ // register the new clean-up function (returned by the nested
9
+ // rv.onNextChange(onNext)) with yet another callback.
5
10
  return rv.onNextChange(function onNext() {
6
11
  update();
7
12
  rv.onNextChange(onNext);