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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/.changeset/eighty-squids-fix.md +15 -0
  2. package/.changeset/eleven-kangaroos-jump.md +5 -0
  3. package/.changeset/forty-shrimps-fry.md +5 -0
  4. package/.changeset/funny-jeans-invent.md +16 -0
  5. package/.changeset/grumpy-vans-type.md +5 -0
  6. package/.changeset/khaki-spies-work.md +11 -0
  7. package/.changeset/light-apes-rescue.md +5 -0
  8. package/.changeset/little-spoons-kick.md +7 -0
  9. package/.changeset/loud-cows-raise.md +7 -0
  10. package/.changeset/many-buses-allow.md +5 -0
  11. package/.changeset/odd-lemons-relax.md +5 -0
  12. package/.changeset/poor-eels-punch.md +5 -0
  13. package/.changeset/pre.json +18 -0
  14. package/.changeset/real-teachers-peel.md +5 -0
  15. package/.changeset/slimy-chicken-melt.md +5 -0
  16. package/.changeset/thick-books-grin.md +5 -0
  17. package/.changeset/tough-rockets-allow.md +5 -0
  18. package/.changeset/tricky-tables-shave.md +5 -0
  19. package/.changeset/warm-ties-sit.md +7 -0
  20. package/CHANGELOG.md +84 -0
  21. package/__cjs/cache/core/cache.cjs +1 -1
  22. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  23. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  24. package/__cjs/cache/inmemory/policies.cjs +4 -4
  25. package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
  26. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  27. package/__cjs/core/ApolloClient.cjs +25 -16
  28. package/__cjs/core/ApolloClient.cjs.map +1 -1
  29. package/__cjs/core/ApolloClient.d.cts +17 -5
  30. package/__cjs/core/LocalState.cjs +2 -2
  31. package/__cjs/core/ObservableQuery.cjs +44 -22
  32. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  33. package/__cjs/core/ObservableQuery.d.cts +6 -3
  34. package/__cjs/core/QueryManager.cjs +72 -35
  35. package/__cjs/core/QueryManager.cjs.map +1 -1
  36. package/__cjs/core/QueryManager.d.cts +14 -3
  37. package/__cjs/core/types.d.cts +32 -0
  38. package/__cjs/core/watchQueryOptions.d.cts +18 -25
  39. package/__cjs/invariantErrorCodes.cjs +57 -77
  40. package/__cjs/link/core/ApolloLink.cjs +2 -2
  41. package/__cjs/link/http/checkFetcher.cjs +1 -1
  42. package/__cjs/link/http/createHttpLink.cjs +1 -1
  43. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
  44. package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
  45. package/__cjs/link/persisted-queries/index.cjs +2 -2
  46. package/__cjs/link/utils/validateOperation.cjs +1 -1
  47. package/__cjs/masking/maskDefinition.cjs +2 -2
  48. package/__cjs/masking/maskFragment.cjs +2 -2
  49. package/__cjs/masking/maskOperation.cjs +1 -1
  50. package/__cjs/masking/utils.cjs +1 -1
  51. package/__cjs/react/context/ApolloConsumer.cjs +1 -1
  52. package/__cjs/react/context/ApolloContext.cjs +1 -1
  53. package/__cjs/react/context/ApolloContext.cjs.map +1 -1
  54. package/__cjs/react/context/ApolloContext.d.cts +2 -2
  55. package/__cjs/react/context/ApolloProvider.cjs +1 -1
  56. package/__cjs/react/hooks/index.cjs +13 -8
  57. package/__cjs/react/hooks/index.cjs.map +1 -1
  58. package/__cjs/react/hooks/index.d.cts +6 -12
  59. package/__cjs/react/hooks/internal/wrapHook.cjs +26 -5
  60. package/__cjs/react/hooks/internal/wrapHook.cjs.map +1 -1
  61. package/__cjs/react/hooks/internal/wrapHook.d.cts +3 -1
  62. package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
  63. package/__cjs/react/hooks/useBackgroundQuery.d.cts +146 -43
  64. package/__cjs/react/hooks/useFragment.cjs.map +1 -1
  65. package/__cjs/react/hooks/useFragment.d.cts +51 -26
  66. package/__cjs/react/hooks/useLazyQuery.cjs +1 -1
  67. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  68. package/__cjs/react/hooks/useLazyQuery.d.cts +263 -262
  69. package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
  70. package/__cjs/react/hooks/useLoadableQuery.d.cts +105 -32
  71. package/__cjs/react/hooks/useMutation.cjs +14 -19
  72. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  73. package/__cjs/react/hooks/useMutation.d.cts +198 -3
  74. package/__cjs/react/hooks/useQuery.cjs +82 -189
  75. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  76. package/__cjs/react/hooks/useQuery.d.cts +283 -4
  77. package/__cjs/react/hooks/useQueryRefHandlers.cjs.map +1 -1
  78. package/__cjs/react/hooks/useQueryRefHandlers.d.cts +21 -20
  79. package/__cjs/react/hooks/useReadQuery.cjs.map +1 -1
  80. package/__cjs/react/hooks/useReadQuery.d.cts +26 -24
  81. package/__cjs/react/hooks/useSubscription.cjs +8 -57
  82. package/__cjs/react/hooks/useSubscription.cjs.map +1 -1
  83. package/__cjs/react/hooks/useSubscription.d.cts +96 -9
  84. package/__cjs/react/hooks/useSuspenseFragment.cjs.map +1 -1
  85. package/__cjs/react/hooks/useSuspenseFragment.d.cts +40 -37
  86. package/__cjs/react/hooks/useSuspenseQuery.cjs +2 -2
  87. package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
  88. package/__cjs/react/hooks/useSuspenseQuery.d.cts +202 -35
  89. package/__cjs/react/hooks/useSyncExternalStore.cjs +1 -1
  90. package/__cjs/react/index.cjs.map +1 -1
  91. package/__cjs/react/index.d.cts +2 -1
  92. package/__cjs/react/internal/index.cjs +3 -1
  93. package/__cjs/react/internal/index.cjs.map +1 -1
  94. package/__cjs/react/internal/index.d.cts +2 -0
  95. package/__cjs/react/internal/types.d.cts +46 -0
  96. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  97. package/__cjs/react/query-preloader/createQueryPreloader.d.cts +2 -3
  98. package/__cjs/react/ssr/getDataFromTree.cjs +43 -12
  99. package/__cjs/react/ssr/getDataFromTree.cjs.map +1 -1
  100. package/__cjs/react/ssr/getDataFromTree.d.cts +5 -0
  101. package/__cjs/react/ssr/index.cjs +1 -3
  102. package/__cjs/react/ssr/index.cjs.map +1 -1
  103. package/__cjs/react/ssr/index.d.cts +0 -1
  104. package/__cjs/react/ssr/useSSRQuery.cjs +60 -0
  105. package/__cjs/react/ssr/useSSRQuery.cjs.map +1 -0
  106. package/__cjs/react/ssr/useSSRQuery.d.cts +5 -0
  107. package/__cjs/react/types/deprecated.cjs +3 -0
  108. package/__cjs/react/types/deprecated.cjs.map +1 -0
  109. package/__cjs/react/types/deprecated.d.cts +65 -0
  110. package/__cjs/react/types/types.documentation.d.cts +4 -12
  111. package/__cjs/testing/matchers/index.cjs +4 -0
  112. package/__cjs/testing/matchers/index.cjs.map +1 -1
  113. package/__cjs/testing/matchers/toEmitStrictTyped.cjs +42 -0
  114. package/__cjs/testing/matchers/toEmitStrictTyped.cjs.map +1 -0
  115. package/__cjs/testing/matchers/toEmitStrictTyped.d.cts +7 -0
  116. package/__cjs/testing/matchers/toEqualLazyQueryResult.cjs.map +1 -1
  117. package/__cjs/testing/matchers/toEqualLazyQueryResult.d.cts +2 -2
  118. package/__cjs/testing/matchers/toEqualQueryResult.cjs.map +1 -1
  119. package/__cjs/testing/matchers/toEqualQueryResult.d.cts +2 -2
  120. package/__cjs/testing/matchers/toEqualStrictTyped.cjs +26 -0
  121. package/__cjs/testing/matchers/toEqualStrictTyped.cjs.map +1 -0
  122. package/__cjs/testing/matchers/toEqualStrictTyped.d.cts +3 -0
  123. package/__cjs/testing/matchers/utils/getSerializableProperties.cjs +23 -0
  124. package/__cjs/testing/matchers/utils/getSerializableProperties.cjs.map +1 -0
  125. package/__cjs/testing/matchers/utils/getSerializableProperties.d.cts +2 -0
  126. package/__cjs/version.cjs +1 -1
  127. package/__cjs/version.d.cts +1 -1
  128. package/cache/core/cache.js +1 -1
  129. package/cache/inmemory/entityStore.js +3 -3
  130. package/cache/inmemory/key-extractor.js +1 -1
  131. package/cache/inmemory/policies.js +4 -4
  132. package/cache/inmemory/readFromStore.js +2 -2
  133. package/cache/inmemory/writeToStore.js +4 -4
  134. package/core/ApolloClient.d.ts +17 -5
  135. package/core/ApolloClient.js +25 -16
  136. package/core/ApolloClient.js.map +1 -1
  137. package/core/LocalState.js +2 -2
  138. package/core/ObservableQuery.d.ts +6 -3
  139. package/core/ObservableQuery.js +44 -22
  140. package/core/ObservableQuery.js.map +1 -1
  141. package/core/QueryManager.d.ts +14 -3
  142. package/core/QueryManager.js +73 -36
  143. package/core/QueryManager.js.map +1 -1
  144. package/core/types.d.ts +32 -0
  145. package/core/watchQueryOptions.d.ts +18 -25
  146. package/invariantErrorCodes.js +57 -77
  147. package/link/core/ApolloLink.js +2 -2
  148. package/link/http/checkFetcher.js +1 -1
  149. package/link/http/createHttpLink.js +1 -1
  150. package/link/http/parseAndCheckHttpResponse.js +1 -1
  151. package/link/http/serializeFetchParameter.js +1 -1
  152. package/link/persisted-queries/index.js +2 -2
  153. package/link/utils/validateOperation.js +1 -1
  154. package/masking/maskDefinition.js +2 -2
  155. package/masking/maskFragment.js +2 -2
  156. package/masking/maskOperation.js +1 -1
  157. package/masking/utils.js +1 -1
  158. package/package.json +1 -1
  159. package/react/context/ApolloConsumer.js +1 -1
  160. package/react/context/ApolloContext.d.ts +2 -2
  161. package/react/context/ApolloContext.js +1 -1
  162. package/react/context/ApolloContext.js.map +1 -1
  163. package/react/context/ApolloProvider.js +1 -1
  164. package/react/hooks/index.d.ts +6 -12
  165. package/react/hooks/index.js +6 -6
  166. package/react/hooks/index.js.map +1 -1
  167. package/react/hooks/internal/wrapHook.d.ts +3 -1
  168. package/react/hooks/internal/wrapHook.js +24 -5
  169. package/react/hooks/internal/wrapHook.js.map +1 -1
  170. package/react/hooks/useBackgroundQuery.d.ts +146 -43
  171. package/react/hooks/useBackgroundQuery.js.map +1 -1
  172. package/react/hooks/useFragment.d.ts +51 -26
  173. package/react/hooks/useFragment.js.map +1 -1
  174. package/react/hooks/useLazyQuery.d.ts +263 -262
  175. package/react/hooks/useLazyQuery.js +1 -1
  176. package/react/hooks/useLazyQuery.js.map +1 -1
  177. package/react/hooks/useLoadableQuery.d.ts +105 -32
  178. package/react/hooks/useLoadableQuery.js.map +1 -1
  179. package/react/hooks/useMutation.d.ts +198 -3
  180. package/react/hooks/useMutation.js +14 -19
  181. package/react/hooks/useMutation.js.map +1 -1
  182. package/react/hooks/useQuery.d.ts +283 -4
  183. package/react/hooks/useQuery.js +83 -190
  184. package/react/hooks/useQuery.js.map +1 -1
  185. package/react/hooks/useQueryRefHandlers.d.ts +21 -20
  186. package/react/hooks/useQueryRefHandlers.js.map +1 -1
  187. package/react/hooks/useReadQuery.d.ts +26 -24
  188. package/react/hooks/useReadQuery.js.map +1 -1
  189. package/react/hooks/useSubscription.d.ts +96 -9
  190. package/react/hooks/useSubscription.js +7 -53
  191. package/react/hooks/useSubscription.js.map +1 -1
  192. package/react/hooks/useSuspenseFragment.d.ts +40 -37
  193. package/react/hooks/useSuspenseFragment.js.map +1 -1
  194. package/react/hooks/useSuspenseQuery.d.ts +202 -35
  195. package/react/hooks/useSuspenseQuery.js +2 -2
  196. package/react/hooks/useSuspenseQuery.js.map +1 -1
  197. package/react/hooks/useSyncExternalStore.js +1 -1
  198. package/react/index.d.ts +2 -1
  199. package/react/index.js.map +1 -1
  200. package/react/internal/index.d.ts +2 -0
  201. package/react/internal/index.js +2 -1
  202. package/react/internal/index.js.map +1 -1
  203. package/react/internal/types.d.ts +46 -0
  204. package/react/query-preloader/createQueryPreloader.d.ts +2 -3
  205. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  206. package/react/ssr/getDataFromTree.d.ts +5 -0
  207. package/react/ssr/getDataFromTree.js +43 -12
  208. package/react/ssr/getDataFromTree.js.map +1 -1
  209. package/react/ssr/index.d.ts +0 -1
  210. package/react/ssr/index.js +0 -1
  211. package/react/ssr/index.js.map +1 -1
  212. package/react/ssr/useSSRQuery.d.ts +5 -0
  213. package/react/ssr/useSSRQuery.js +56 -0
  214. package/react/ssr/useSSRQuery.js.map +1 -0
  215. package/react/types/deprecated.d.ts +65 -0
  216. package/react/types/deprecated.js +2 -0
  217. package/react/types/deprecated.js.map +1 -0
  218. package/react/types/types.documentation.d.ts +4 -12
  219. package/testing/matchers/index.js +4 -0
  220. package/testing/matchers/index.js.map +1 -1
  221. package/testing/matchers/toEmitStrictTyped.d.ts +7 -0
  222. package/testing/matchers/toEmitStrictTyped.js +38 -0
  223. package/testing/matchers/toEmitStrictTyped.js.map +1 -0
  224. package/testing/matchers/toEqualLazyQueryResult.d.ts +2 -2
  225. package/testing/matchers/toEqualLazyQueryResult.js.map +1 -1
  226. package/testing/matchers/toEqualQueryResult.d.ts +2 -2
  227. package/testing/matchers/toEqualQueryResult.js.map +1 -1
  228. package/testing/matchers/toEqualStrictTyped.d.ts +3 -0
  229. package/testing/matchers/toEqualStrictTyped.js +22 -0
  230. package/testing/matchers/toEqualStrictTyped.js.map +1 -0
  231. package/testing/matchers/utils/getSerializableProperties.d.ts +2 -0
  232. package/testing/matchers/utils/getSerializableProperties.js +20 -0
  233. package/testing/matchers/utils/getSerializableProperties.js.map +1 -0
  234. package/version.d.ts +1 -1
  235. package/version.js +1 -1
  236. package/__cjs/react/ssr/RenderPromises.cjs +0 -104
  237. package/__cjs/react/ssr/RenderPromises.cjs.map +0 -1
  238. package/__cjs/react/ssr/RenderPromises.d.cts +0 -22
  239. package/__cjs/react/types/types.d.cts +0 -661
  240. package/react/ssr/RenderPromises.d.ts +0 -22
  241. package/react/ssr/RenderPromises.js +0 -100
  242. package/react/ssr/RenderPromises.js.map +0 -1
  243. package/react/types/types.d.ts +0 -661
  244. /package/__cjs/react/{types → internal}/types.cjs +0 -0
  245. /package/__cjs/react/{types → internal}/types.cjs.map +0 -0
  246. /package/react/{types → internal}/types.js +0 -0
  247. /package/react/{types → internal}/types.js.map +0 -0
@@ -1,6 +1,281 @@
1
- import type { OperationVariables } from "@apollo/client/core";
2
- import type { DocumentNode, TypedDocumentNode } from "@apollo/client/core";
3
- import type { NoInfer, QueryHookOptions, QueryResult } from "@apollo/client/react";
1
+ import type { ApolloClient, DefaultContext, ErrorLike, ErrorPolicy, FetchMoreQueryOptions, OperationVariables, RefetchWritePolicy, SubscribeToMoreFunction, UpdateQueryMapFn, WatchQueryFetchPolicy } from "@apollo/client/core";
2
+ import type { ApolloQueryResult, DocumentNode, ObservableQuery, TypedDocumentNode, WatchQueryOptions } from "@apollo/client/core";
3
+ import { NetworkStatus } from "@apollo/client/core";
4
+ import type { MaybeMasked, Unmasked } from "@apollo/client/masking";
5
+ import type { NoInfer } from "@apollo/client/utilities";
6
+ import type { NextFetchPolicyContext } from "../../core/watchQueryOptions.js";
7
+ export declare namespace useQuery {
8
+ interface Options<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
9
+ /**
10
+ * Specifies how the query interacts with the Apollo Client cache during execution (for example, whether it checks the cache for results before sending a request to the server).
11
+ *
12
+ * For details, see [Setting a fetch policy](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy).
13
+ *
14
+ * The default value is `cache-first`.
15
+ *
16
+ * @docGroup
17
+ *
18
+ * 3. Caching options
19
+ */
20
+ fetchPolicy?: WatchQueryFetchPolicy;
21
+ /**
22
+ * Specifies the `FetchPolicy` to be used after this query has completed.
23
+ *
24
+ * @docGroup
25
+ *
26
+ * 3. Caching options
27
+ */
28
+ nextFetchPolicy?: WatchQueryFetchPolicy | ((this: WatchQueryOptions<TVariables, TData>, currentFetchPolicy: WatchQueryFetchPolicy, context: NextFetchPolicyContext<TData, TVariables>) => WatchQueryFetchPolicy);
29
+ /**
30
+ * Defaults to the initial value of options.fetchPolicy, but can be explicitly configured to specify the WatchQueryFetchPolicy to revert back to whenever variables change (unless nextFetchPolicy intervenes).
31
+ *
32
+ * @docGroup
33
+ *
34
+ * 3. Caching options
35
+ */
36
+ initialFetchPolicy?: WatchQueryFetchPolicy;
37
+ /**
38
+ * Specifies whether a `NetworkStatus.refetch` operation should merge incoming field data with existing data, or overwrite the existing data. Overwriting is probably preferable, but merging is currently the default behavior, for backwards compatibility with Apollo Client 3.x.
39
+ *
40
+ * @docGroup
41
+ *
42
+ * 3. Caching options
43
+ */
44
+ refetchWritePolicy?: RefetchWritePolicy;
45
+ /**
46
+ * An object containing all of the GraphQL variables your query requires to execute.
47
+ *
48
+ * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
49
+ *
50
+ * @docGroup
51
+ *
52
+ * 1. Operation options
53
+ */
54
+ variables?: TVariables;
55
+ /**
56
+ * Specifies how the query handles a response that returns both GraphQL errors and partial results.
57
+ *
58
+ * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
59
+ *
60
+ * The default value is `none`, meaning that the query result includes error details but not partial results.
61
+ *
62
+ * @docGroup
63
+ *
64
+ * 1. Operation options
65
+ */
66
+ errorPolicy?: ErrorPolicy;
67
+ /**
68
+ * Specifies the interval (in milliseconds) at which the query polls for updated results.
69
+ *
70
+ * The default value is `0` (no polling).
71
+ *
72
+ * @docGroup
73
+ *
74
+ * 2. Networking options
75
+ */
76
+ pollInterval?: number;
77
+ /**
78
+ * If `true`, the in-progress query's associated component re-renders whenever the network status changes or a network error occurs.
79
+ *
80
+ * The default value is `false`.
81
+ *
82
+ * @docGroup
83
+ *
84
+ * 2. Networking options
85
+ */
86
+ notifyOnNetworkStatusChange?: boolean;
87
+ /**
88
+ * If `true`, the query can return partial results from the cache if the cache doesn't contain results for all queried fields.
89
+ *
90
+ * The default value is `false`.
91
+ *
92
+ * @docGroup
93
+ *
94
+ * 3. Caching options
95
+ */
96
+ returnPartialData?: boolean;
97
+ /**
98
+ * A callback function that's called whenever a refetch attempt occurs while polling. If the function returns `true`, the refetch is skipped and not reattempted until the next poll interval.
99
+ *
100
+ * @docGroup
101
+ *
102
+ * 2. Networking options
103
+ */
104
+ skipPollAttempt?: () => boolean;
105
+ /**
106
+ * Pass `false` to skip executing the query during [server-side rendering](https://www.apollographql.com/docs/react/performance/server-side-rendering/).
107
+ *
108
+ * @docGroup
109
+ *
110
+ * 2. Networking options
111
+ */
112
+ ssr?: boolean;
113
+ /**
114
+ * The instance of `ApolloClient` to use to execute the query.
115
+ *
116
+ * By default, the instance that's passed down via context is used, but you can provide a different instance here.
117
+ *
118
+ * @docGroup
119
+ *
120
+ * 1. Operation options
121
+ */
122
+ client?: ApolloClient;
123
+ /**
124
+ * If you're using [Apollo Link](https://www.apollographql.com/docs/react/api/link/introduction/), this object is the initial value of the `context` object that's passed along your link chain.
125
+ *
126
+ * @docGroup
127
+ *
128
+ * 2. Networking options
129
+ */
130
+ context?: DefaultContext;
131
+ /**
132
+ * If true, the query is not executed.
133
+ *
134
+ * The default value is `false`.
135
+ *
136
+ * @docGroup
137
+ *
138
+ * 1. Operation options
139
+ */
140
+ skip?: boolean;
141
+ }
142
+ interface Result<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
143
+ /**
144
+ * The instance of Apollo Client that executed the query. Can be useful for manually executing followup queries or writing data to the cache.
145
+ *
146
+ * @docGroup
147
+ *
148
+ * 2. Network info
149
+ */
150
+ client: ApolloClient;
151
+ /**
152
+ * A reference to the internal `ObservableQuery` used by the hook.
153
+ */
154
+ observable: ObservableQuery<TData, TVariables>;
155
+ /**
156
+ * An object containing the result of your GraphQL query after it completes.
157
+ *
158
+ * This value might be `undefined` if a query results in one or more errors (depending on the query's `errorPolicy`).
159
+ *
160
+ * @docGroup
161
+ *
162
+ * 1. Operation data
163
+ */
164
+ data: MaybeMasked<TData> | undefined;
165
+ /**
166
+ * An object containing the result from the most recent _previous_ execution of this query.
167
+ *
168
+ * This value is `undefined` if this is the query's first execution.
169
+ *
170
+ * @docGroup
171
+ *
172
+ * 1. Operation data
173
+ */
174
+ previousData?: MaybeMasked<TData>;
175
+ /**
176
+ * If the query produces one or more errors, this object contains either an array of `graphQLErrors` or a single `networkError`. Otherwise, this value is `undefined`.
177
+ *
178
+ * For more information, see [Handling operation errors](https://www.apollographql.com/docs/react/data/error-handling/).
179
+ *
180
+ * @docGroup
181
+ *
182
+ * 1. Operation data
183
+ */
184
+ error?: ErrorLike;
185
+ /**
186
+ * If `true`, the query is still in flight and results have not yet been returned.
187
+ *
188
+ * @docGroup
189
+ *
190
+ * 2. Network info
191
+ */
192
+ loading: boolean;
193
+ /**
194
+ * A number indicating the current network state of the query's associated request. [See possible values.](https://github.com/apollographql/apollo-client/blob/d96f4578f89b933c281bb775a39503f6cdb59ee8/src/core/networkStatus.ts#L4)
195
+ *
196
+ * Used in conjunction with the [`notifyOnNetworkStatusChange`](#notifyonnetworkstatuschange) option.
197
+ *
198
+ * @docGroup
199
+ *
200
+ * 2. Network info
201
+ */
202
+ networkStatus: NetworkStatus;
203
+ /**
204
+ * A function that instructs the query to begin re-executing at a specified interval (in milliseconds).
205
+ *
206
+ *
207
+ * @docGroup
208
+ *
209
+ * 3. Helper functions
210
+ */
211
+ startPolling: (pollInterval: number) => void;
212
+ /**
213
+ * A function that instructs the query to stop polling after a previous call to `startPolling`.
214
+ *
215
+ *
216
+ * @docGroup
217
+ *
218
+ * 3. Helper functions
219
+ */
220
+ stopPolling: () => void;
221
+ /**
222
+ * A function that enables you to execute a [subscription](https://www.apollographql.com/docs/react/data/subscriptions/), usually to subscribe to specific fields that were included in the query.
223
+ *
224
+ * This function returns _another_ function that you can call to terminate the subscription.
225
+ *
226
+ *
227
+ * @docGroup
228
+ *
229
+ * 3. Helper functions
230
+ */
231
+ subscribeToMore: SubscribeToMoreFunction<TData, TVariables>;
232
+ /**
233
+ * A function that enables you to update the query's cached result without executing a followup GraphQL operation.
234
+ *
235
+ * See [using updateQuery and updateFragment](https://www.apollographql.com/docs/react/caching/cache-interaction/#using-updatequery-and-updatefragment) for additional information.
236
+ *
237
+ *
238
+ * @docGroup
239
+ *
240
+ * 3. Helper functions
241
+ */
242
+ updateQuery: (mapFn: UpdateQueryMapFn<TData, TVariables>) => void;
243
+ /**
244
+ * A function that enables you to re-execute the query, optionally passing in new `variables`.
245
+ *
246
+ * To guarantee that the refetch performs a network request, its `fetchPolicy` is set to `network-only` (unless the original query's `fetchPolicy` is `no-cache` or `cache-and-network`, which also guarantee a network request).
247
+ *
248
+ * See also [Refetching](https://www.apollographql.com/docs/react/data/queries/#refetching).
249
+ *
250
+ * @docGroup
251
+ *
252
+ * 3. Helper functions
253
+ */
254
+ refetch: (variables?: Partial<TVariables>) => Promise<ApolloQueryResult<MaybeMasked<TData>>>;
255
+ /**
256
+ * An object containing the variables that were provided for the query.
257
+ *
258
+ * @docGroup
259
+ *
260
+ * 1. Operation data
261
+ */
262
+ variables: TVariables | undefined;
263
+ /**
264
+ * A function that helps you fetch the next set of results for a [paginated list field](https://www.apollographql.com/docs/react/pagination/core-api/).
265
+ *
266
+ *
267
+ * @docGroup
268
+ *
269
+ * 3. Helper functions
270
+ */
271
+ fetchMore: <TFetchData = TData, TFetchVars extends OperationVariables = TVariables>(fetchMoreOptions: FetchMoreQueryOptions<TFetchVars, TFetchData> & {
272
+ updateQuery?: (previousQueryResult: Unmasked<TData>, options: {
273
+ fetchMoreResult: Unmasked<TFetchData>;
274
+ variables: TFetchVars;
275
+ }) => Unmasked<TData>;
276
+ }) => Promise<ApolloQueryResult<MaybeMasked<TFetchData>>>;
277
+ }
278
+ }
4
279
  /**
5
280
  * A hook for executing queries in an Apollo application.
6
281
  *
@@ -35,5 +310,9 @@ import type { NoInfer, QueryHookOptions, QueryResult } from "@apollo/client/reac
35
310
  * @param options - Options to control how the query is executed.
36
311
  * @returns Query result object
37
312
  */
38
- export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>): QueryResult<TData, TVariables>;
313
+ export declare function useQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: useQuery.Options<NoInfer<TData>, NoInfer<TVariables>>): useQuery.Result<TData, TVariables>;
314
+ export declare namespace useQuery {
315
+ var ssrDisabledResult: ApolloQueryResult<any>;
316
+ var skipStandbyResult: ApolloQueryResult<any>;
317
+ }
39
318
  //# sourceMappingURL=useQuery.d.ts.map
@@ -9,20 +9,14 @@
9
9
  * options
10
10
  * watchQueryOptions
11
11
  * makeWatchQueryOptions
12
- * isSSRAllowed
13
- * disableNetworkFetches
14
- * renderPromises
15
- * isSyncSSR
16
12
  */
17
13
  /** */
18
14
  import { equal } from "@wry/equality";
19
15
  import * as React from "react";
20
16
  import { asapScheduler, observeOn } from "rxjs";
21
17
  import { NetworkStatus } from "@apollo/client/core";
22
- import { getApolloContext } from "@apollo/client/react/context";
23
18
  import { DocumentType, verifyDocumentType } from "@apollo/client/react/parser";
24
- import { compact, maybeDeepFreeze } from "@apollo/client/utilities";
25
- import { mergeOptions } from "@apollo/client/utilities";
19
+ import { maybeDeepFreeze, mergeOptions } from "@apollo/client/utilities";
26
20
  import { wrapHook } from "./internal/index.js";
27
21
  import { useApolloClient } from "./useApolloClient.js";
28
22
  import { useSyncExternalStore } from "./useSyncExternalStore.js";
@@ -62,96 +56,99 @@ const lastWatchOptions = Symbol();
62
56
  * @returns Query result object
63
57
  */
64
58
  export function useQuery(query, options = {}) {
59
+ "use no memo";
65
60
  return wrapHook("useQuery",
66
61
  // eslint-disable-next-line react-compiler/react-compiler
67
62
  useQuery_, useApolloClient(options && options.client))(query, options);
68
63
  }
69
64
  function useQuery_(query, options) {
70
- const result = useQueryInternals(query, options);
71
- const obsQueryFields = React.useMemo(() => bindObservableMethods(result.observable), [result.observable]);
72
- return React.useMemo(() => ({ ...result, ...obsQueryFields }), [result, obsQueryFields]);
73
- }
74
- function useInternalState(client, query, renderPromises, makeWatchQueryOptions) {
75
- function createInternalState(previous) {
65
+ const client = useApolloClient(options.client);
66
+ const { skip, ssr, ...opts } = options;
67
+ const watchQueryOptions = mergeOptions(client.defaultOptions.watchQuery, { ...opts, query });
68
+ if (skip) {
69
+ // When skipping, we set watchQueryOptions.fetchPolicy initially to
70
+ // "standby", but we also need/want to preserve the initial non-standby
71
+ // fetchPolicy that would have been used if not skipping.
72
+ watchQueryOptions.initialFetchPolicy =
73
+ options.initialFetchPolicy || options.fetchPolicy;
74
+ watchQueryOptions.fetchPolicy = "standby";
75
+ }
76
+ function createState(previous) {
76
77
  verifyDocumentType(query, DocumentType.Query);
77
- const internalState = {
78
+ const observable = client.watchQuery(watchQueryOptions);
79
+ return {
78
80
  client,
79
81
  query,
80
- observable:
81
- // See if there is an existing observable that was used to fetch the same
82
- // data and if so, use it instead since it will contain the proper queryId
83
- // to fetch the result set. This is used during SSR.
84
- (renderPromises &&
85
- renderPromises.getSSRObservable(makeWatchQueryOptions())) ||
86
- client.watchQuery(getObsQueryOptions(void 0, client, makeWatchQueryOptions())),
82
+ observable,
87
83
  resultData: {
84
+ current: observable.getCurrentResult(),
88
85
  // Reuse previousData from previous InternalState (if any) to provide
89
86
  // continuity of previousData even if/when the query or client changes.
90
- previousData: previous?.resultData.current?.data,
87
+ previousData: previous?.resultData.current.data,
91
88
  },
92
89
  };
93
- return internalState;
94
90
  }
95
- let [internalState, updateInternalState] = React.useState(createInternalState);
96
- if (client !== internalState.client || query !== internalState.query) {
91
+ let [state, setState] = React.useState(createState);
92
+ if (client !== state.client || query !== state.query) {
97
93
  // If the client or query have changed, we need to create a new InternalState.
98
94
  // This will trigger a re-render with the new state, but it will also continue
99
95
  // to run the current render function to completion.
100
96
  // Since we sometimes trigger some side-effects in the render function, we
101
97
  // re-assign `state` to the new state to ensure that those side-effects are
102
98
  // triggered with the new state.
103
- const newInternalState = createInternalState(internalState);
104
- updateInternalState(newInternalState);
105
- return newInternalState;
99
+ setState((state = createState(state)));
106
100
  }
107
- return internalState;
108
- }
109
- function useQueryInternals(query, options) {
110
- const client = useApolloClient(options.client);
111
- const renderPromises = React.useContext(getApolloContext()).renderPromises;
112
- const isSyncSSR = !!renderPromises;
113
- const disableNetworkFetches = client.disableNetworkFetches;
114
- const ssrAllowed = options.ssr !== false && !options.skip;
115
- const makeWatchQueryOptions = createMakeWatchQueryOptions(client, query, options, isSyncSSR);
116
- const { observable, resultData } = useInternalState(client, query, renderPromises, makeWatchQueryOptions);
117
- const watchQueryOptions = makeWatchQueryOptions(observable);
101
+ const { observable, resultData } = state;
102
+ useInitialFetchPolicyIfNecessary(watchQueryOptions, observable);
118
103
  useResubscribeIfNecessary(resultData, // might get mutated during render
119
104
  observable, // might get mutated during render
120
- client, watchQueryOptions);
121
- useRegisterSSRObservable(observable, renderPromises, ssrAllowed);
122
- const result = useObservableSubscriptionResult(resultData, observable, client, options, watchQueryOptions, disableNetworkFetches, isSyncSSR);
123
- return result;
124
- }
125
- function useObservableSubscriptionResult(resultData, observable, client, options, watchQueryOptions, disableNetworkFetches, isSyncSSR) {
126
- const resultOverride = ((isSyncSSR || disableNetworkFetches) &&
127
- options.ssr === false &&
128
- !options.skip) ?
129
- // If SSR has been explicitly disabled, and this function has been called
130
- // on the server side, return the default loading state.
131
- ssrDisabledResult
132
- : options.skip || watchQueryOptions.fetchPolicy === "standby" ?
133
- // When skipping a query (ie. we're not querying for data but still want to
134
- // render children), make sure the `data` is cleared out and `loading` is
135
- // set to `false` (since we aren't loading anything).
136
- //
137
- // NOTE: We no longer think this is the correct behavior. Skipping should
138
- // not automatically set `data` to `undefined`, but instead leave the
139
- // previous data in place. In other words, skipping should not mandate that
140
- // previously received data is all of a sudden removed. Unfortunately,
141
- // changing this is breaking, so we'll have to wait until Apollo Client 4.0
142
- // to address this.
143
- skipStandbyResult
144
- : void 0;
105
+ watchQueryOptions);
106
+ const ssrDisabledOverride = useSyncExternalStore(() => () => { }, () => void 0, () => (ssr === false ? useQuery.ssrDisabledResult : void 0));
107
+ const resultOverride = skip || watchQueryOptions.fetchPolicy === "standby" ?
108
+ // When skipping a query (ie. we're not querying for data but still want to
109
+ // render children), make sure the `data` is cleared out and `loading` is
110
+ // set to `false` (since we aren't loading anything).
111
+ //
112
+ // NOTE: We no longer think this is the correct behavior. Skipping should
113
+ // not automatically set `data` to `undefined`, but instead leave the
114
+ // previous data in place. In other words, skipping should not mandate that
115
+ // previously received data is all of a sudden removed. Unfortunately,
116
+ // changing this is breaking, so we'll have to wait until Apollo Client 4.0
117
+ // to address this.
118
+ useQuery.skipStandbyResult
119
+ : ssrDisabledOverride;
120
+ const result = useResultSubscription(observable, resultData, resultOverride);
121
+ const obsQueryFields = React.useMemo(() => ({
122
+ refetch: observable.refetch.bind(observable),
123
+ fetchMore: observable.fetchMore.bind(observable),
124
+ updateQuery: observable.updateQuery.bind(observable),
125
+ startPolling: observable.startPolling.bind(observable),
126
+ stopPolling: observable.stopPolling.bind(observable),
127
+ subscribeToMore: observable.subscribeToMore.bind(observable),
128
+ }), [observable]);
145
129
  const previousData = resultData.previousData;
146
- const currentResultOverride = React.useMemo(() => resultOverride &&
147
- toQueryResult(resultOverride, previousData, observable, client), [client, observable, resultOverride, previousData]);
130
+ return React.useMemo(() => {
131
+ const { data, partial, ...rest } = result;
132
+ return {
133
+ data, // Ensure always defined, even if result.data is missing.
134
+ ...rest,
135
+ client: client,
136
+ observable: observable,
137
+ variables: observable.variables,
138
+ previousData,
139
+ ...obsQueryFields,
140
+ };
141
+ }, [result, client, observable, previousData, obsQueryFields]);
142
+ }
143
+ function useInitialFetchPolicyIfNecessary(watchQueryOptions, observable) {
144
+ "use no memo";
145
+ if (!watchQueryOptions.fetchPolicy) {
146
+ watchQueryOptions.fetchPolicy = observable.options.initialFetchPolicy;
147
+ }
148
+ }
149
+ function useResultSubscription(observable, resultData, resultOverride) {
150
+ "use no memo";
148
151
  return useSyncExternalStore(React.useCallback((handleStoreChange) => {
149
- // reference `disableNetworkFetches` here to ensure that the rules of hooks
150
- // keep it as a dependency of this effect, even though it's not used
151
- disableNetworkFetches;
152
- if (isSyncSSR) {
153
- return () => { };
154
- }
155
152
  const subscription = observable
156
153
  // We use the asapScheduler here to prevent issues with trying to
157
154
  // update in the middle of a render. `reobserve` is kicked off in the
@@ -172,7 +169,12 @@ function useObservableSubscriptionResult(resultData, observable, client, options
172
169
  equal(previousResult.error, result.error)) {
173
170
  return;
174
171
  }
175
- setResult(result, resultData, observable, client, handleStoreChange);
172
+ if (previousResult && previousResult.data) {
173
+ // eslint-disable-next-line react-compiler/react-compiler
174
+ resultData.previousData = previousResult.data;
175
+ }
176
+ resultData.current = result;
177
+ handleStoreChange();
176
178
  });
177
179
  // Do the "unsubscribe" with a short delay.
178
180
  // This way, an existing subscription can be reused without an additional
@@ -181,16 +183,7 @@ function useObservableSubscriptionResult(resultData, observable, client, options
181
183
  return () => {
182
184
  setTimeout(() => subscription.unsubscribe());
183
185
  };
184
- }, [disableNetworkFetches, isSyncSSR, observable, resultData, client]), () => currentResultOverride || getCurrentResult(resultData, observable, client), () => currentResultOverride || getCurrentResult(resultData, observable, client));
185
- }
186
- function useRegisterSSRObservable(observable, renderPromises, ssrAllowed) {
187
- if (renderPromises && ssrAllowed) {
188
- renderPromises.registerSSRObservable(observable);
189
- if (observable.getCurrentResult().loading) {
190
- // TODO: This is a legacy API which could probably be cleaned up
191
- renderPromises.addObservableQueryPromise(observable);
192
- }
193
- }
186
+ }, [observable, resultData]), () => resultOverride || resultData.current, () => resultOverride || resultData.current);
194
187
  }
195
188
  // this hook is not compatible with any rules of React, and there's no good way to rewrite it.
196
189
  // it should stay a separate hook that will not be optimized by the compiler
@@ -198,7 +191,8 @@ function useResubscribeIfNecessary(
198
191
  /** this hook will mutate properties on `resultData` */
199
192
  resultData,
200
193
  /** this hook will mutate properties on `observable` */
201
- observable, client, watchQueryOptions) {
194
+ observable, watchQueryOptions) {
195
+ "use no memo";
202
196
  if (observable[lastWatchOptions] &&
203
197
  !equal(observable[lastWatchOptions], watchQueryOptions)) {
204
198
  // Though it might be tempting to postpone this reobserve call to the
@@ -209,129 +203,28 @@ observable, client, watchQueryOptions) {
209
203
  // subscriptions, though it does feel less than ideal that reobserve
210
204
  // (potentially) kicks off a network request (for example, when the
211
205
  // variables have changed), which is technically a side-effect.
212
- observable.reobserve(getObsQueryOptions(observable, client, watchQueryOptions));
206
+ observable.reobserve(watchQueryOptions);
213
207
  // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,
214
208
  // but save the current data as this.previousData, just like setResult
215
209
  // usually does.
216
210
  resultData.previousData =
217
- resultData.current?.data || resultData.previousData;
218
- resultData.current = void 0;
211
+ resultData.current.data || resultData.previousData;
212
+ resultData.current = observable.getCurrentResult();
219
213
  }
220
214
  observable[lastWatchOptions] = watchQueryOptions;
221
215
  }
222
- /*
223
- * A function to massage options before passing them to ObservableQuery.
224
- * This is two-step curried because we want to reuse the `make` function,
225
- * but the `observable` might differ between calls to `make`.
226
- */
227
- function createMakeWatchQueryOptions(client, query, { skip, ssr,
228
- // The above options are useQuery-specific, so this ...otherOptions spread
229
- // makes otherOptions almost a WatchQueryOptions object, except for the
230
- // query property that we add below.
231
- ...otherOptions } = {}, isSyncSSR) {
232
- return (observable) => {
233
- // This Object.assign is safe because otherOptions is a fresh ...rest object
234
- // that did not exist until just now, so modifications are still allowed.
235
- const watchQueryOptions = Object.assign(otherOptions, { query });
236
- if (isSyncSSR &&
237
- (watchQueryOptions.fetchPolicy === "network-only" ||
238
- watchQueryOptions.fetchPolicy === "cache-and-network")) {
239
- // this behavior was added to react-apollo without explanation in this PR
240
- // https://github.com/apollographql/react-apollo/pull/1579
241
- watchQueryOptions.fetchPolicy = "cache-first";
242
- }
243
- if (!watchQueryOptions.variables) {
244
- watchQueryOptions.variables = {};
245
- }
246
- if (skip) {
247
- // When skipping, we set watchQueryOptions.fetchPolicy initially to
248
- // "standby", but we also need/want to preserve the initial non-standby
249
- // fetchPolicy that would have been used if not skipping.
250
- watchQueryOptions.initialFetchPolicy =
251
- watchQueryOptions.initialFetchPolicy ||
252
- watchQueryOptions.fetchPolicy ||
253
- client.defaultOptions?.watchQuery?.fetchPolicy ||
254
- "cache-first";
255
- watchQueryOptions.fetchPolicy = "standby";
256
- }
257
- else if (!watchQueryOptions.fetchPolicy) {
258
- watchQueryOptions.fetchPolicy = observable?.options.initialFetchPolicy;
259
- }
260
- return watchQueryOptions;
261
- };
262
- }
263
- function getObsQueryOptions(observable, client, watchQueryOptions) {
264
- const toMerge = [];
265
- const globalDefaults = client.defaultOptions.watchQuery;
266
- if (globalDefaults)
267
- toMerge.push(globalDefaults);
268
- // We use compact rather than mergeOptions for this part of the merge,
269
- // because we want watchQueryOptions.variables (if defined) to replace
270
- // this.observable.options.variables whole. This replacement allows
271
- // removing variables by removing them from the variables input to
272
- // useQuery. If the variables were always merged together (rather than
273
- // replaced), there would be no way to remove existing variables.
274
- // However, the variables from options.defaultOptions and globalDefaults
275
- // (if provided) should be merged, to ensure individual defaulted
276
- // variables always have values, if not otherwise defined in
277
- // observable.options or watchQueryOptions.
278
- toMerge.push(compact(observable && observable.options, watchQueryOptions));
279
- return toMerge.reduce(mergeOptions);
280
- }
281
- function setResult(nextResult, resultData, observable, client, forceUpdate) {
282
- const previousResult = resultData.current;
283
- if (previousResult && previousResult.data) {
284
- resultData.previousData = previousResult.data;
285
- }
286
- resultData.current = toQueryResult(nextResult, resultData.previousData, observable, client);
287
- // Calling state.setResult always triggers an update, though some call sites
288
- // perform additional equality checks before committing to an update.
289
- forceUpdate();
290
- }
291
- function getCurrentResult(resultData, observable, client) {
292
- // Using this.result as a cache ensures getCurrentResult continues returning
293
- // the same (===) result object, unless state.setResult has been called, or
294
- // we're doing server rendering and therefore override the result below.
295
- if (!resultData.current) {
296
- setResult(observable.getCurrentResult(), resultData, observable, client, () => { });
297
- }
298
- return resultData.current;
299
- }
300
- function toQueryResult(result, previousData, observable, client) {
301
- const { data, partial, ...resultWithoutPartial } = result;
302
- const queryResult = {
303
- data, // Ensure always defined, even if result.data is missing.
304
- ...resultWithoutPartial,
305
- client: client,
306
- observable: observable,
307
- variables: observable.variables,
308
- previousData,
309
- };
310
- return queryResult;
311
- }
312
- const ssrDisabledResult = maybeDeepFreeze({
216
+ useQuery.ssrDisabledResult = maybeDeepFreeze({
313
217
  loading: true,
314
218
  data: void 0,
315
219
  error: void 0,
316
220
  networkStatus: NetworkStatus.loading,
317
221
  partial: true,
318
222
  });
319
- const skipStandbyResult = maybeDeepFreeze({
223
+ useQuery.skipStandbyResult = maybeDeepFreeze({
320
224
  loading: false,
321
225
  data: void 0,
322
226
  error: void 0,
323
227
  networkStatus: NetworkStatus.ready,
324
228
  partial: true,
325
229
  });
326
- function bindObservableMethods(observable) {
327
- return {
328
- refetch: observable.refetch.bind(observable),
329
- reobserve: observable.reobserve.bind(observable),
330
- fetchMore: observable.fetchMore.bind(observable),
331
- updateQuery: observable.updateQuery.bind(observable),
332
- startPolling: observable.startPolling.bind(observable),
333
- stopPolling: observable.stopPolling.bind(observable),
334
- subscribeToMore: observable.subscribeToMore.bind(observable),
335
- };
336
- }
337
230
  //# sourceMappingURL=useQuery.js.map