@tanstack/query-core 5.102.7 → 5.103.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/build/legacy/environmentManager.cjs +15 -1
  2. package/build/legacy/environmentManager.cjs.map +1 -1
  3. package/build/legacy/environmentManager.d.cts +15 -1
  4. package/build/legacy/environmentManager.d.cts.map +1 -1
  5. package/build/legacy/environmentManager.d.ts +15 -1
  6. package/build/legacy/environmentManager.d.ts.map +1 -1
  7. package/build/legacy/environmentManager.js +15 -1
  8. package/build/legacy/environmentManager.js.map +1 -1
  9. package/build/legacy/focusManager.cjs +57 -0
  10. package/build/legacy/focusManager.cjs.map +1 -1
  11. package/build/legacy/focusManager.d.cts +57 -0
  12. package/build/legacy/focusManager.d.cts.map +1 -1
  13. package/build/legacy/focusManager.d.ts +57 -0
  14. package/build/legacy/focusManager.d.ts.map +1 -1
  15. package/build/legacy/focusManager.js +57 -0
  16. package/build/legacy/focusManager.js.map +1 -1
  17. package/build/legacy/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
  18. package/build/legacy/hydration-Cq7QYAzB.d.ts.map +1 -0
  19. package/build/legacy/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
  20. package/build/legacy/hydration-DwR10Hi-.d.cts.map +1 -0
  21. package/build/legacy/hydration.cjs +57 -4
  22. package/build/legacy/hydration.cjs.map +1 -1
  23. package/build/legacy/hydration.d.cts +1 -1
  24. package/build/legacy/hydration.d.ts +1 -1
  25. package/build/legacy/hydration.js +57 -4
  26. package/build/legacy/hydration.js.map +1 -1
  27. package/build/legacy/index.cjs.map +1 -1
  28. package/build/legacy/index.d.cts +5 -5
  29. package/build/legacy/index.d.ts +5 -5
  30. package/build/legacy/index.js.map +1 -1
  31. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  32. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  33. package/build/legacy/infiniteQueryObserver.cjs +67 -0
  34. package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
  35. package/build/legacy/infiniteQueryObserver.d.cts +68 -1
  36. package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
  37. package/build/legacy/infiniteQueryObserver.d.ts +68 -1
  38. package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
  39. package/build/legacy/infiniteQueryObserver.js +67 -0
  40. package/build/legacy/infiniteQueryObserver.js.map +1 -1
  41. package/build/legacy/mutation.cjs +74 -0
  42. package/build/legacy/mutation.cjs.map +1 -1
  43. package/build/legacy/mutation.d.cts +1 -1
  44. package/build/legacy/mutation.d.ts +1 -1
  45. package/build/legacy/mutation.js +74 -0
  46. package/build/legacy/mutation.js.map +1 -1
  47. package/build/legacy/mutationCache.cjs +74 -0
  48. package/build/legacy/mutationCache.cjs.map +1 -1
  49. package/build/legacy/mutationCache.d.cts +1 -1
  50. package/build/legacy/mutationCache.d.ts +1 -1
  51. package/build/legacy/mutationCache.js +74 -0
  52. package/build/legacy/mutationCache.js.map +1 -1
  53. package/build/legacy/mutationObserver.cjs +71 -0
  54. package/build/legacy/mutationObserver.cjs.map +1 -1
  55. package/build/legacy/mutationObserver.d.cts +1 -1
  56. package/build/legacy/mutationObserver.d.ts +1 -1
  57. package/build/legacy/mutationObserver.js +71 -0
  58. package/build/legacy/mutationObserver.js.map +1 -1
  59. package/build/legacy/notifyManager.cjs +45 -1
  60. package/build/legacy/notifyManager.cjs.map +1 -1
  61. package/build/legacy/notifyManager.d.cts +83 -2
  62. package/build/legacy/notifyManager.d.cts.map +1 -1
  63. package/build/legacy/notifyManager.d.ts +83 -2
  64. package/build/legacy/notifyManager.d.ts.map +1 -1
  65. package/build/legacy/notifyManager.js +45 -1
  66. package/build/legacy/notifyManager.js.map +1 -1
  67. package/build/legacy/onlineManager.cjs +47 -0
  68. package/build/legacy/onlineManager.cjs.map +1 -1
  69. package/build/legacy/onlineManager.d.cts +47 -0
  70. package/build/legacy/onlineManager.d.cts.map +1 -1
  71. package/build/legacy/onlineManager.d.ts +47 -0
  72. package/build/legacy/onlineManager.d.ts.map +1 -1
  73. package/build/legacy/onlineManager.js +47 -0
  74. package/build/legacy/onlineManager.js.map +1 -1
  75. package/build/legacy/queriesObserver.cjs +64 -0
  76. package/build/legacy/queriesObserver.cjs.map +1 -1
  77. package/build/legacy/queriesObserver.d.cts +73 -1
  78. package/build/legacy/queriesObserver.d.cts.map +1 -1
  79. package/build/legacy/queriesObserver.d.ts +73 -1
  80. package/build/legacy/queriesObserver.d.ts.map +1 -1
  81. package/build/legacy/queriesObserver.js +64 -0
  82. package/build/legacy/queriesObserver.js.map +1 -1
  83. package/build/legacy/query.cjs +141 -0
  84. package/build/legacy/query.cjs.map +1 -1
  85. package/build/legacy/query.d.cts +1 -1
  86. package/build/legacy/query.d.ts +1 -1
  87. package/build/legacy/query.js +141 -0
  88. package/build/legacy/query.js.map +1 -1
  89. package/build/legacy/queryCache.cjs +120 -0
  90. package/build/legacy/queryCache.cjs.map +1 -1
  91. package/build/legacy/queryCache.d.cts +1 -1
  92. package/build/legacy/queryCache.d.ts +1 -1
  93. package/build/legacy/queryCache.js +120 -0
  94. package/build/legacy/queryCache.js.map +1 -1
  95. package/build/legacy/queryClient.cjs +363 -0
  96. package/build/legacy/queryClient.cjs.map +1 -1
  97. package/build/legacy/queryClient.d.cts +1 -1
  98. package/build/legacy/queryClient.d.ts +1 -1
  99. package/build/legacy/queryClient.js +363 -0
  100. package/build/legacy/queryClient.js.map +1 -1
  101. package/build/legacy/queryObserver.cjs +116 -2
  102. package/build/legacy/queryObserver.cjs.map +1 -1
  103. package/build/legacy/queryObserver.d.cts +1 -1
  104. package/build/legacy/queryObserver.d.ts +1 -1
  105. package/build/legacy/queryObserver.js +116 -2
  106. package/build/legacy/queryObserver.js.map +1 -1
  107. package/build/legacy/retryer.cjs +18 -0
  108. package/build/legacy/retryer.cjs.map +1 -1
  109. package/build/legacy/retryer.d.cts +1 -1
  110. package/build/legacy/retryer.d.ts +1 -1
  111. package/build/legacy/retryer.js +18 -0
  112. package/build/legacy/retryer.js.map +1 -1
  113. package/build/legacy/streamedQuery.cjs +10 -1
  114. package/build/legacy/streamedQuery.cjs.map +1 -1
  115. package/build/legacy/streamedQuery.d.cts +26 -2
  116. package/build/legacy/streamedQuery.d.cts.map +1 -1
  117. package/build/legacy/streamedQuery.d.ts +26 -2
  118. package/build/legacy/streamedQuery.d.ts.map +1 -1
  119. package/build/legacy/streamedQuery.js +10 -1
  120. package/build/legacy/streamedQuery.js.map +1 -1
  121. package/build/legacy/timeoutManager.cjs +99 -0
  122. package/build/legacy/timeoutManager.cjs.map +1 -1
  123. package/build/legacy/timeoutManager.d.cts +105 -0
  124. package/build/legacy/timeoutManager.d.cts.map +1 -1
  125. package/build/legacy/timeoutManager.d.ts +105 -0
  126. package/build/legacy/timeoutManager.d.ts.map +1 -1
  127. package/build/legacy/timeoutManager.js +99 -0
  128. package/build/legacy/timeoutManager.js.map +1 -1
  129. package/build/legacy/types.cjs.map +1 -1
  130. package/build/legacy/types.d.cts +1 -1
  131. package/build/legacy/types.d.ts +1 -1
  132. package/build/legacy/types.js.map +1 -1
  133. package/build/legacy/utils.cjs +76 -0
  134. package/build/legacy/utils.cjs.map +1 -1
  135. package/build/legacy/utils.d.cts +1 -1
  136. package/build/legacy/utils.d.ts +1 -1
  137. package/build/legacy/utils.js +76 -0
  138. package/build/legacy/utils.js.map +1 -1
  139. package/build/modern/environmentManager.cjs +15 -1
  140. package/build/modern/environmentManager.cjs.map +1 -1
  141. package/build/modern/environmentManager.d.cts +15 -1
  142. package/build/modern/environmentManager.d.cts.map +1 -1
  143. package/build/modern/environmentManager.d.ts +15 -1
  144. package/build/modern/environmentManager.d.ts.map +1 -1
  145. package/build/modern/environmentManager.js +15 -1
  146. package/build/modern/environmentManager.js.map +1 -1
  147. package/build/modern/focusManager.cjs +57 -0
  148. package/build/modern/focusManager.cjs.map +1 -1
  149. package/build/modern/focusManager.d.cts +57 -0
  150. package/build/modern/focusManager.d.cts.map +1 -1
  151. package/build/modern/focusManager.d.ts +57 -0
  152. package/build/modern/focusManager.d.ts.map +1 -1
  153. package/build/modern/focusManager.js +57 -0
  154. package/build/modern/focusManager.js.map +1 -1
  155. package/build/modern/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
  156. package/build/modern/hydration-Cq7QYAzB.d.ts.map +1 -0
  157. package/build/modern/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
  158. package/build/modern/hydration-DwR10Hi-.d.cts.map +1 -0
  159. package/build/modern/hydration.cjs +57 -4
  160. package/build/modern/hydration.cjs.map +1 -1
  161. package/build/modern/hydration.d.cts +1 -1
  162. package/build/modern/hydration.d.ts +1 -1
  163. package/build/modern/hydration.js +57 -4
  164. package/build/modern/hydration.js.map +1 -1
  165. package/build/modern/index.cjs.map +1 -1
  166. package/build/modern/index.d.cts +5 -5
  167. package/build/modern/index.d.ts +5 -5
  168. package/build/modern/index.js.map +1 -1
  169. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  170. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  171. package/build/modern/infiniteQueryObserver.cjs +67 -0
  172. package/build/modern/infiniteQueryObserver.cjs.map +1 -1
  173. package/build/modern/infiniteQueryObserver.d.cts +68 -1
  174. package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
  175. package/build/modern/infiniteQueryObserver.d.ts +68 -1
  176. package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
  177. package/build/modern/infiniteQueryObserver.js +67 -0
  178. package/build/modern/infiniteQueryObserver.js.map +1 -1
  179. package/build/modern/mutation.cjs +74 -0
  180. package/build/modern/mutation.cjs.map +1 -1
  181. package/build/modern/mutation.d.cts +1 -1
  182. package/build/modern/mutation.d.ts +1 -1
  183. package/build/modern/mutation.js +74 -0
  184. package/build/modern/mutation.js.map +1 -1
  185. package/build/modern/mutationCache.cjs +74 -0
  186. package/build/modern/mutationCache.cjs.map +1 -1
  187. package/build/modern/mutationCache.d.cts +1 -1
  188. package/build/modern/mutationCache.d.ts +1 -1
  189. package/build/modern/mutationCache.js +74 -0
  190. package/build/modern/mutationCache.js.map +1 -1
  191. package/build/modern/mutationObserver.cjs +71 -0
  192. package/build/modern/mutationObserver.cjs.map +1 -1
  193. package/build/modern/mutationObserver.d.cts +1 -1
  194. package/build/modern/mutationObserver.d.ts +1 -1
  195. package/build/modern/mutationObserver.js +71 -0
  196. package/build/modern/mutationObserver.js.map +1 -1
  197. package/build/modern/notifyManager.cjs +45 -1
  198. package/build/modern/notifyManager.cjs.map +1 -1
  199. package/build/modern/notifyManager.d.cts +83 -2
  200. package/build/modern/notifyManager.d.cts.map +1 -1
  201. package/build/modern/notifyManager.d.ts +83 -2
  202. package/build/modern/notifyManager.d.ts.map +1 -1
  203. package/build/modern/notifyManager.js +45 -1
  204. package/build/modern/notifyManager.js.map +1 -1
  205. package/build/modern/onlineManager.cjs +47 -0
  206. package/build/modern/onlineManager.cjs.map +1 -1
  207. package/build/modern/onlineManager.d.cts +47 -0
  208. package/build/modern/onlineManager.d.cts.map +1 -1
  209. package/build/modern/onlineManager.d.ts +47 -0
  210. package/build/modern/onlineManager.d.ts.map +1 -1
  211. package/build/modern/onlineManager.js +47 -0
  212. package/build/modern/onlineManager.js.map +1 -1
  213. package/build/modern/queriesObserver.cjs +64 -0
  214. package/build/modern/queriesObserver.cjs.map +1 -1
  215. package/build/modern/queriesObserver.d.cts +73 -1
  216. package/build/modern/queriesObserver.d.cts.map +1 -1
  217. package/build/modern/queriesObserver.d.ts +73 -1
  218. package/build/modern/queriesObserver.d.ts.map +1 -1
  219. package/build/modern/queriesObserver.js +64 -0
  220. package/build/modern/queriesObserver.js.map +1 -1
  221. package/build/modern/query.cjs +141 -0
  222. package/build/modern/query.cjs.map +1 -1
  223. package/build/modern/query.d.cts +1 -1
  224. package/build/modern/query.d.ts +1 -1
  225. package/build/modern/query.js +141 -0
  226. package/build/modern/query.js.map +1 -1
  227. package/build/modern/queryCache.cjs +120 -0
  228. package/build/modern/queryCache.cjs.map +1 -1
  229. package/build/modern/queryCache.d.cts +1 -1
  230. package/build/modern/queryCache.d.ts +1 -1
  231. package/build/modern/queryCache.js +120 -0
  232. package/build/modern/queryCache.js.map +1 -1
  233. package/build/modern/queryClient.cjs +363 -0
  234. package/build/modern/queryClient.cjs.map +1 -1
  235. package/build/modern/queryClient.d.cts +1 -1
  236. package/build/modern/queryClient.d.ts +1 -1
  237. package/build/modern/queryClient.js +363 -0
  238. package/build/modern/queryClient.js.map +1 -1
  239. package/build/modern/queryObserver.cjs +116 -2
  240. package/build/modern/queryObserver.cjs.map +1 -1
  241. package/build/modern/queryObserver.d.cts +1 -1
  242. package/build/modern/queryObserver.d.ts +1 -1
  243. package/build/modern/queryObserver.js +116 -2
  244. package/build/modern/queryObserver.js.map +1 -1
  245. package/build/modern/retryer.cjs +18 -0
  246. package/build/modern/retryer.cjs.map +1 -1
  247. package/build/modern/retryer.d.cts +1 -1
  248. package/build/modern/retryer.d.ts +1 -1
  249. package/build/modern/retryer.js +18 -0
  250. package/build/modern/retryer.js.map +1 -1
  251. package/build/modern/streamedQuery.cjs +10 -1
  252. package/build/modern/streamedQuery.cjs.map +1 -1
  253. package/build/modern/streamedQuery.d.cts +26 -2
  254. package/build/modern/streamedQuery.d.cts.map +1 -1
  255. package/build/modern/streamedQuery.d.ts +26 -2
  256. package/build/modern/streamedQuery.d.ts.map +1 -1
  257. package/build/modern/streamedQuery.js +10 -1
  258. package/build/modern/streamedQuery.js.map +1 -1
  259. package/build/modern/timeoutManager.cjs +99 -0
  260. package/build/modern/timeoutManager.cjs.map +1 -1
  261. package/build/modern/timeoutManager.d.cts +105 -0
  262. package/build/modern/timeoutManager.d.cts.map +1 -1
  263. package/build/modern/timeoutManager.d.ts +105 -0
  264. package/build/modern/timeoutManager.d.ts.map +1 -1
  265. package/build/modern/timeoutManager.js +99 -0
  266. package/build/modern/timeoutManager.js.map +1 -1
  267. package/build/modern/types.cjs.map +1 -1
  268. package/build/modern/types.d.cts +1 -1
  269. package/build/modern/types.d.ts +1 -1
  270. package/build/modern/types.js.map +1 -1
  271. package/build/modern/utils.cjs +76 -0
  272. package/build/modern/utils.cjs.map +1 -1
  273. package/build/modern/utils.d.cts +1 -1
  274. package/build/modern/utils.d.ts +1 -1
  275. package/build/modern/utils.js +76 -0
  276. package/build/modern/utils.js.map +1 -1
  277. package/package.json +9 -9
  278. package/src/environmentManager.ts +15 -1
  279. package/src/focusManager.ts +57 -0
  280. package/src/hydration.ts +82 -16
  281. package/src/index.ts +3 -0
  282. package/src/infiniteQueryObserver.ts +67 -0
  283. package/src/mutation.ts +108 -0
  284. package/src/mutationCache.ts +91 -0
  285. package/src/mutationObserver.ts +71 -0
  286. package/src/notifyManager.ts +46 -1
  287. package/src/onlineManager.ts +47 -0
  288. package/src/queriesObserver.ts +72 -0
  289. package/src/query.ts +193 -0
  290. package/src/queryCache.ts +134 -0
  291. package/src/queryClient.ts +369 -0
  292. package/src/queryObserver.ts +117 -4
  293. package/src/retryer.ts +18 -0
  294. package/src/streamedQuery.ts +25 -1
  295. package/src/timeoutManager.ts +105 -0
  296. package/src/types.ts +147 -5
  297. package/src/utils.ts +107 -0
  298. package/build/legacy/hydration-Bjs0MSgg.d.ts.map +0 -1
  299. package/build/legacy/hydration-DWGr3cFC.d.cts.map +0 -1
  300. package/build/modern/hydration-Bjs0MSgg.d.ts.map +0 -1
  301. package/build/modern/hydration-DWGr3cFC.d.cts.map +0 -1
@@ -58,6 +58,24 @@ interface MutationDefaults {
58
58
 
59
59
  // CLASS
60
60
 
61
+ /**
62
+ * `QueryClient` is used to interact with a cache of queries and mutations. It owns a
63
+ * `QueryCache` and a `MutationCache` (creating default ones if none are passed in) and holds
64
+ * the default options that are applied to queries and mutations created through it.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const queryClient = new QueryClient({
69
+ * defaultOptions: {
70
+ * queries: {
71
+ * staleTime: Infinity,
72
+ * },
73
+ * },
74
+ * })
75
+ *
76
+ * await queryClient.query({ queryKey: ['posts'], queryFn: fetchPosts })
77
+ * ```
78
+ */
61
79
  export class QueryClient {
62
80
  #queryCache: QueryCache
63
81
  #mutationCache: MutationCache
@@ -77,6 +95,12 @@ export class QueryClient {
77
95
  this.#mountCount = 0
78
96
  }
79
97
 
98
+ /**
99
+ * Called by a framework adapter's `QueryClientProvider`-equivalent when it mounts, to start
100
+ * listening for focus/online events and resume paused mutations. Ref-counted via an internal
101
+ * mount count, so nested or multiple providers sharing the same `QueryClient` don't tear down
102
+ * the shared listeners until the last one unmounts.
103
+ */
80
104
  mount(): void {
81
105
  this.#mountCount++
82
106
  if (this.#mountCount !== 1) return
@@ -95,6 +119,11 @@ export class QueryClient {
95
119
  })
96
120
  }
97
121
 
122
+ /**
123
+ * The inverse of {@link QueryClient#mount} — called by a framework adapter's
124
+ * `QueryClientProvider`-equivalent when it unmounts. Only tears down the focus/online
125
+ * listeners once the mount count returns to `0`.
126
+ */
98
127
  unmount(): void {
99
128
  this.#mountCount--
100
129
  if (this.#mountCount !== 0) return
@@ -106,6 +135,18 @@ export class QueryClient {
106
135
  this.#unsubscribeOnline = undefined
107
136
  }
108
137
 
138
+ /**
139
+ * Returns the number of queries in the cache that are currently fetching, optionally
140
+ * matching a set of filters. This includes background-fetching, loading new pages, and
141
+ * loading more infinite query results.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * if (queryClient.isFetching()) {
146
+ * console.log('At least one query is fetching!')
147
+ * }
148
+ * ```
149
+ */
109
150
  isFetching<TQueryFilters extends QueryFilters<any> = QueryFilters>(
110
151
  filters?: TQueryFilters,
111
152
  ): number {
@@ -113,6 +154,17 @@ export class QueryClient {
113
154
  .length
114
155
  }
115
156
 
157
+ /**
158
+ * Returns the number of mutations in the cache that are currently pending, optionally
159
+ * matching a set of filters.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * if (queryClient.isMutating()) {
164
+ * console.log('At least one mutation is pending!')
165
+ * }
166
+ * ```
167
+ */
116
168
  isMutating<
117
169
  TMutationFilters extends MutationFilters<any, any> = MutationFilters,
118
170
  >(filters?: TMutationFilters): number {
@@ -125,6 +177,8 @@ export class QueryClient {
125
177
  *
126
178
  * Hint: Do not use this function inside a component, because it won't receive updates.
127
179
  * Use `useQuery` to create a `QueryObserver` that subscribes to changes.
180
+ *
181
+ * @see {@link QueryClient#getQueriesData}
128
182
  */
129
183
  getQueryData<
130
184
  TQueryFnData = unknown,
@@ -153,6 +207,7 @@ export class QueryClient {
153
207
  const cachedData = query.state.data
154
208
 
155
209
  if (cachedData === undefined) {
210
+ // eslint-disable-next-line no-restricted-syntax
156
211
  return this.fetchQuery(options)
157
212
  }
158
213
 
@@ -160,12 +215,30 @@ export class QueryClient {
160
215
  options.revalidateIfStale &&
161
216
  query.isStaleByTime(resolveQueryValue(defaultedOptions.staleTime, query))
162
217
  ) {
218
+ // eslint-disable-next-line no-restricted-syntax
163
219
  void this.prefetchQuery(defaultedOptions)
164
220
  }
165
221
 
166
222
  return Promise.resolve(cachedData)
167
223
  }
168
224
 
225
+ /**
226
+ * Imperative (non-reactive) way to retrieve the cached data of multiple queries at once.
227
+ * Only queries matching the given filters are returned; if none match, an empty array is
228
+ * returned.
229
+ *
230
+ * Because the matched queries can hold data of different shapes (e.g. a broad filter can match
231
+ * queries with unrelated data types), the `TQueryFnData` generic defaults to `unknown` rather
232
+ * than being inferred. Passing a more specific type is a convenience for call sites that know
233
+ * every matched query holds the same shape — it is not checked against the actual cache
234
+ * contents.
235
+ *
236
+ * @see {@link QueryClient#getQueryData}
237
+ * @example
238
+ * ```ts
239
+ * const data = queryClient.getQueriesData({ queryKey: ['posts'] })
240
+ * ```
241
+ */
169
242
  getQueriesData<
170
243
  TQueryFnData = unknown,
171
244
  TQueryFilters extends QueryFilters<any> = QueryFilters,
@@ -176,6 +249,27 @@ export class QueryClient {
176
249
  })
177
250
  }
178
251
 
252
+ /**
253
+ * Synchronous way to immediately update a query's cached data. If the updater (or the value
254
+ * passed) resolves to `undefined`, the cache is left untouched and no query is created;
255
+ * otherwise, if the query does not exist yet, it will be created. To update multiple queries
256
+ * at once by partially matching query keys, use {@link QueryClient#setQueriesData} instead.
257
+ *
258
+ * Updates must be performed immutably: do not mutate `oldData`, or data previously retrieved
259
+ * via {@link QueryClient#getQueryData}, in place.
260
+ *
261
+ * @param queryKey - The query key to set data for.
262
+ * @param updater - Either the new data, or a function that receives the current data (which
263
+ * may be `undefined`) and returns the new data.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * queryClient.setQueryData(['posts'], newPosts)
268
+ *
269
+ * // Or, using an updater function that receives the current data:
270
+ * queryClient.setQueryData(['posts'], (oldPosts) => [...oldPosts, newPost])
271
+ * ```
272
+ */
179
273
  setQueryData<
180
274
  TQueryFnData = unknown,
181
275
  TTaggedQueryKey extends QueryKey = QueryKey,
@@ -211,6 +305,19 @@ export class QueryClient {
211
305
  .setData(data, { ...options, manual: true })
212
306
  }
213
307
 
308
+ /**
309
+ * Synchronous way to immediately update the cached data of multiple queries at once, using
310
+ * filters or partial query key matching. Only queries that already exist and match the given
311
+ * filters are updated; no new cache entries are created. Internally this calls
312
+ * {@link QueryClient#setQueryData} for each matching query.
313
+ *
314
+ * @example
315
+ * ```ts
316
+ * queryClient.setQueriesData({ queryKey: ['posts'] }, (oldPosts) =>
317
+ * oldPosts ? oldPosts.filter((post) => post.id !== deletedId) : oldPosts,
318
+ * )
319
+ * ```
320
+ */
214
321
  setQueriesData<
215
322
  TQueryFnData,
216
323
  TQueryFilters extends QueryFilters<any> = QueryFilters,
@@ -232,6 +339,16 @@ export class QueryClient {
232
339
  )
233
340
  }
234
341
 
342
+ /**
343
+ * Imperative (non-reactive) way to retrieve an existing query's state. If the query does not
344
+ * exist, `undefined` is returned.
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * const state = queryClient.getQueryState(['posts'])
349
+ * console.log(state?.dataUpdatedAt)
350
+ * ```
351
+ */
235
352
  getQueryState<
236
353
  TQueryFnData = unknown,
237
354
  TError = DefaultError,
@@ -247,6 +364,17 @@ export class QueryClient {
247
364
  )?.state
248
365
  }
249
366
 
367
+ /**
368
+ * Removes queries from the cache that match the given filters. Unlike
369
+ * {@link QueryClient#invalidateQueries} or {@link QueryClient#refetchQueries}, this removes
370
+ * matching queries from the cache instead of refetching them. Without filters, every query in
371
+ * the cache is removed.
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * queryClient.removeQueries({ queryKey: ['posts'], exact: true })
376
+ * ```
377
+ */
250
378
  removeQueries<TTaggedQueryKey extends QueryKey = QueryKey>(
251
379
  filters?: QueryFilters<TTaggedQueryKey>,
252
380
  ): void {
@@ -258,6 +386,16 @@ export class QueryClient {
258
386
  })
259
387
  }
260
388
 
389
+ /**
390
+ * Resets queries matching the given filters back to their initial state (e.g. any
391
+ * `initialData`), notifying subscribers rather than removing them. Active queries among the
392
+ * matched set are then refetched, and the returned promise resolves once that refetch settles.
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * await queryClient.resetQueries({ queryKey: ['posts'], exact: true })
397
+ * ```
398
+ */
261
399
  resetQueries<TTaggedQueryKey extends QueryKey = QueryKey>(
262
400
  filters?: QueryFilters<TTaggedQueryKey>,
263
401
  options?: ResetOptions,
@@ -280,6 +418,19 @@ export class QueryClient {
280
418
  })
281
419
  }
282
420
 
421
+ /**
422
+ * Cancels outgoing fetches for queries matching the given filters. Most useful when performing
423
+ * optimistic updates, since any outgoing refetch that resolves afterwards would otherwise
424
+ * overwrite the optimistic update. By default (`revert: true`), a cancelled query's data is
425
+ * reverted to its state before the outgoing fetch started.
426
+ *
427
+ * The returned promise never rejects, even if individual cancellations fail.
428
+ *
429
+ * @example
430
+ * ```ts
431
+ * await queryClient.cancelQueries({ queryKey: ['posts'], exact: true })
432
+ * ```
433
+ */
283
434
  cancelQueries<TTaggedQueryKey extends QueryKey = QueryKey>(
284
435
  filters?: QueryFilters<TTaggedQueryKey>,
285
436
  cancelOptions: CancelOptions = {},
@@ -295,6 +446,19 @@ export class QueryClient {
295
446
  return Promise.all(promises).then(noop).catch(noop)
296
447
  }
297
448
 
449
+ /**
450
+ * Marks queries matching the given filters as invalidated. Unlike
451
+ * {@link QueryClient#removeQueries}, invalidated queries stay in the cache.
452
+ *
453
+ * Unless `filters.refetchType` is `'none'`, matching queries are then refetched via
454
+ * {@link QueryClient#refetchQueries}, using `filters.refetchType` if set, otherwise
455
+ * `filters.type`, otherwise `'active'`.
456
+ *
457
+ * @example
458
+ * ```ts
459
+ * await queryClient.invalidateQueries({ queryKey: ['posts'], refetchType: 'active' })
460
+ * ```
461
+ */
298
462
  invalidateQueries<TTaggedQueryKey extends QueryKey = QueryKey>(
299
463
  filters?: InvalidateQueryFilters<TTaggedQueryKey>,
300
464
  options: InvalidateOptions = {},
@@ -317,6 +481,21 @@ export class QueryClient {
317
481
  })
318
482
  }
319
483
 
484
+ /**
485
+ * Refetches queries matching the given filters, regardless of whether they are stale. Without
486
+ * filters, every query in the cache is refetched. Queries that are disabled, or static (only
487
+ * have observers with a static `staleTime`), are never refetched.
488
+ *
489
+ * By default (`cancelRefetch: true`), a currently running fetch is cancelled before the new
490
+ * one starts. The returned promise resolves once all matching queries have settled; it does
491
+ * not reject on individual query failures unless `throwOnError` is set.
492
+ *
493
+ * @example
494
+ * ```ts
495
+ * // refetch all active queries partially matching a query key:
496
+ * await queryClient.refetchQueries({ queryKey: ['posts'], type: 'active' })
497
+ * ```
498
+ */
320
499
  refetchQueries<TTaggedQueryKey extends QueryKey = QueryKey>(
321
500
  filters?: RefetchQueryFilters<TTaggedQueryKey>,
322
501
  options: RefetchOptions = {},
@@ -343,6 +522,37 @@ export class QueryClient {
343
522
  return Promise.all(promises).then(noop)
344
523
  }
345
524
 
525
+ /**
526
+ * Asynchronous method to fetch and cache a query, resolving with the data or throwing with
527
+ * the error.
528
+ *
529
+ * If the query already exists in the cache and its data is not stale (per the given
530
+ * `staleTime`), the cached data is returned without fetching. Otherwise, the query is fetched
531
+ * and the promise resolves once the fetch settles. If a `select` function is provided, it is
532
+ * applied to the data in both cases (cached or freshly fetched) before it is returned.
533
+ *
534
+ * Unlike a reactive observer, retries are disabled by default here (`retry: false`) unless
535
+ * explicitly configured, since there is no component to catch a thrown error and retry through
536
+ * re-render.
537
+ *
538
+ * The accepted options are `QueryObserverOptions` minus the fields that only make sense for a
539
+ * reactive observer — `enabled`, `refetchInterval`, `refetchIntervalInBackground`,
540
+ * `refetchOnWindowFocus`, `refetchOnReconnect`, `refetchOnMount`, `retryOnMount`,
541
+ * `notifyOnChangeProps`, `throwOnError`, `suspense`, and `placeholderData` are not part of this
542
+ * method's options.
543
+ *
544
+ * This method replaces the deprecated `fetchQuery`, and — combined with
545
+ * `{ staleTime: 'static' }` — the deprecated `ensureQueryData`.
546
+ *
547
+ * @example
548
+ * ```ts
549
+ * try {
550
+ * const data = await queryClient.query({ queryKey, queryFn, staleTime: 10000 })
551
+ * } catch (error) {
552
+ * console.log(error)
553
+ * }
554
+ * ```
555
+ */
346
556
  async query<
347
557
  TQueryFnData,
348
558
  TError = DefaultError,
@@ -431,9 +641,31 @@ export class QueryClient {
431
641
  >(
432
642
  options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
433
643
  ): Promise<void> {
644
+ // eslint-disable-next-line no-restricted-syntax
434
645
  return this.fetchQuery(options).then(noop).catch(noop)
435
646
  }
436
647
 
648
+ /**
649
+ * Asynchronous method to fetch and cache an infinite query, resolving with an
650
+ * {@link InfiniteData} object or throwing with the error.
651
+ *
652
+ * Behaves like {@link QueryClient#query}, accepting the same options (minus
653
+ * `initialPageParam`), plus the required `initialPageParam`, and an optional `pages` /
654
+ * `getNextPageParam` pair used to refetch a fixed number of pages from the start.
655
+ *
656
+ * This method replaces the deprecated `fetchInfiniteQuery`, and — combined with
657
+ * `{ staleTime: 'static' }` — the deprecated `ensureInfiniteQueryData`.
658
+ *
659
+ * @example
660
+ * ```ts
661
+ * try {
662
+ * const data = await queryClient.infiniteQuery({ queryKey, queryFn, initialPageParam: 0 })
663
+ * console.log(data.pages)
664
+ * } catch (error) {
665
+ * console.log(error)
666
+ * }
667
+ * ```
668
+ */
437
669
  infiniteQuery<
438
670
  TQueryFnData,
439
671
  TError = DefaultError,
@@ -476,6 +708,7 @@ export class QueryClient {
476
708
  >,
477
709
  ): Promise<InfiniteData<TData, TPageParam>> {
478
710
  options._type = 'infinite'
711
+ // eslint-disable-next-line no-restricted-syntax
479
712
  return this.fetchQuery(options as any)
480
713
  }
481
714
 
@@ -497,6 +730,7 @@ export class QueryClient {
497
730
  TPageParam
498
731
  >,
499
732
  ): Promise<void> {
733
+ // eslint-disable-next-line no-restricted-syntax
500
734
  return this.fetchInfiniteQuery(options).then(noop).catch(noop)
501
735
  }
502
736
 
@@ -520,9 +754,22 @@ export class QueryClient {
520
754
  ): Promise<InfiniteData<TData, TPageParam>> {
521
755
  options._type = 'infinite'
522
756
 
757
+ // eslint-disable-next-line no-restricted-syntax
523
758
  return this.ensureQueryData(options as any)
524
759
  }
525
760
 
761
+ /**
762
+ * Resumes mutations that were paused because there was no network connection. Does nothing
763
+ * (resolving immediately) if the client is currently offline.
764
+ *
765
+ * @example
766
+ * ```ts
767
+ * import { QueryClient } from '@tanstack/query-core'
768
+ *
769
+ * const queryClient = new QueryClient()
770
+ * await queryClient.resumePausedMutations()
771
+ * ```
772
+ */
526
773
  resumePausedMutations(): Promise<unknown> {
527
774
  if (onlineManager.isOnline()) {
528
775
  return this.#mutationCache.resumePausedMutations()
@@ -530,22 +777,90 @@ export class QueryClient {
530
777
  return Promise.resolve()
531
778
  }
532
779
 
780
+ /**
781
+ * Returns the query cache this client is connected to.
782
+ *
783
+ * @example
784
+ * ```ts
785
+ * import { QueryClient } from '@tanstack/query-core'
786
+ *
787
+ * const queryClient = new QueryClient()
788
+ * const queryCache = queryClient.getQueryCache()
789
+ * const queries = queryCache.findAll({ queryKey: ['posts'] })
790
+ * ```
791
+ */
533
792
  getQueryCache(): QueryCache {
534
793
  return this.#queryCache
535
794
  }
536
795
 
796
+ /**
797
+ * Returns the mutation cache this client is connected to.
798
+ *
799
+ * @example
800
+ * ```ts
801
+ * import { QueryClient } from '@tanstack/query-core'
802
+ *
803
+ * const queryClient = new QueryClient()
804
+ * const mutationCache = queryClient.getMutationCache()
805
+ * const mutations = mutationCache.findAll({ status: 'pending' })
806
+ * ```
807
+ */
537
808
  getMutationCache(): MutationCache {
538
809
  return this.#mutationCache
539
810
  }
540
811
 
812
+ /**
813
+ * Returns the default options that were set when creating the client, or via
814
+ * {@link QueryClient#setDefaultOptions}.
815
+ *
816
+ * @example
817
+ * ```ts
818
+ * import { QueryClient } from '@tanstack/query-core'
819
+ *
820
+ * const queryClient = new QueryClient()
821
+ * const defaultOptions = queryClient.getDefaultOptions()
822
+ * ```
823
+ */
541
824
  getDefaultOptions(): DefaultOptions {
542
825
  return this.#defaultOptions
543
826
  }
544
827
 
828
+ /**
829
+ * Dynamically sets the default options for this client, overwriting any previously defined
830
+ * default options.
831
+ *
832
+ * @see {@link QueryClient#getDefaultOptions}
833
+ * @example
834
+ * ```ts
835
+ * import { QueryClient } from '@tanstack/query-core'
836
+ *
837
+ * const queryClient = new QueryClient()
838
+ * queryClient.setDefaultOptions({
839
+ * queries: {
840
+ * staleTime: Infinity,
841
+ * },
842
+ * })
843
+ * ```
844
+ */
545
845
  setDefaultOptions(options: DefaultOptions): void {
546
846
  this.#defaultOptions = options
547
847
  }
548
848
 
849
+ /**
850
+ * Sets default options for queries whose query key partially matches the given `queryKey`.
851
+ *
852
+ * If several registered query defaults match a given query key, they are merged together in
853
+ * registration order by {@link QueryClient#getQueryDefaults}, so register defaults from the
854
+ * most generic key to the least generic one — more specific defaults should be registered
855
+ * after more generic ones so they take precedence.
856
+ *
857
+ * @example
858
+ * ```ts
859
+ * queryClient.setQueryDefaults(['posts'], { queryFn: fetchPosts })
860
+ *
861
+ * await queryClient.query({ queryKey: ['posts'] })
862
+ * ```
863
+ */
549
864
  setQueryDefaults<
550
865
  TQueryFnData = unknown,
551
866
  TError = DefaultError,
@@ -566,6 +881,16 @@ export class QueryClient {
566
881
  })
567
882
  }
568
883
 
884
+ /**
885
+ * Returns the default options registered for queries whose query key partially matches the
886
+ * given `queryKey`, via {@link QueryClient#setQueryDefaults}. If multiple registered defaults
887
+ * match, they are merged together in registration order.
888
+ *
889
+ * @example
890
+ * ```ts
891
+ * const defaultOptions = queryClient.getQueryDefaults(['posts'])
892
+ * ```
893
+ */
569
894
  getQueryDefaults(
570
895
  queryKey: QueryKey,
571
896
  ): OmitKeyof<QueryObserverOptions<any, any, any, any, any>, 'queryKey'> {
@@ -584,6 +909,17 @@ export class QueryClient {
584
909
  return result
585
910
  }
586
911
 
912
+ /**
913
+ * Sets default options for mutations whose mutation key partially matches the given
914
+ * `mutationKey`. As with {@link QueryClient#setQueryDefaults}, the order of registration
915
+ * matters when several registered defaults match the same mutation key.
916
+ *
917
+ * @see {@link QueryClient#getMutationDefaults}
918
+ * @example
919
+ * ```ts
920
+ * queryClient.setMutationDefaults(['addPost'], { mutationFn: addPost })
921
+ * ```
922
+ */
587
923
  setMutationDefaults<
588
924
  TData = unknown,
589
925
  TError = DefaultError,
@@ -602,6 +938,16 @@ export class QueryClient {
602
938
  })
603
939
  }
604
940
 
941
+ /**
942
+ * Returns the default options registered for mutations whose mutation key partially matches
943
+ * the given `mutationKey`, via {@link QueryClient#setMutationDefaults}. If multiple registered
944
+ * defaults match, they are merged together in registration order.
945
+ *
946
+ * @example
947
+ * ```ts
948
+ * const defaultOptions = queryClient.getMutationDefaults(['addPost'])
949
+ * ```
950
+ */
605
951
  getMutationDefaults(
606
952
  mutationKey: MutationKey,
607
953
  ): OmitKeyof<MutationObserverOptions<any, any, any, any>, 'mutationKey'> {
@@ -621,6 +967,12 @@ export class QueryClient {
621
967
  return result
622
968
  }
623
969
 
970
+ /**
971
+ * Called by framework adapters (e.g. inside `useQuery`) to resolve the options passed by the
972
+ * caller into their final, defaulted form: merging `queryClient.setQueryDefaults` for the
973
+ * given `queryKey`, then the client's own `defaultOptions.queries`, then the caller's options
974
+ * on top. A no-op if the options are already defaulted (`_defaulted: true`).
975
+ */
624
976
  defaultQueryOptions<
625
977
  TQueryFnData = unknown,
626
978
  TError = DefaultError,
@@ -702,6 +1054,12 @@ export class QueryClient {
702
1054
  >
703
1055
  }
704
1056
 
1057
+ /**
1058
+ * The mutation counterpart of {@link QueryClient#defaultQueryOptions}. Called by framework
1059
+ * adapters (e.g. inside `useMutation`) to merge `queryClient.setMutationDefaults` for the
1060
+ * given `mutationKey`, then the client's `defaultOptions.mutations`, then the caller's options
1061
+ * on top. A no-op if the options are already defaulted (`_defaulted: true`).
1062
+ */
705
1063
  defaultMutationOptions<T extends MutationOptions<any, any, any, any>>(
706
1064
  options?: T,
707
1065
  ): T {
@@ -717,6 +1075,17 @@ export class QueryClient {
717
1075
  } as T
718
1076
  }
719
1077
 
1078
+ /**
1079
+ * Clears both the query cache and the mutation cache this client is connected to.
1080
+ *
1081
+ * @example
1082
+ * ```ts
1083
+ * import { QueryClient } from '@tanstack/query-core'
1084
+ *
1085
+ * const queryClient = new QueryClient()
1086
+ * queryClient.clear()
1087
+ * ```
1088
+ */
720
1089
  clear(): void {
721
1090
  this.#queryCache.clear()
722
1091
  this.#mutationCache.clear()