Open History to see the last commit
1import type {
2 DefaultError,
3 InfiniteData,
4 InitialDataFunction,
5 NonUndefinedGuard,
6 OmitKeyof,
7 QueryKey,
8 QueryKeyWithDataTag,
9 SkipToken,
10} from '@tanstack/query-core'
11import type { UseInfiniteQueryOptions } from './types'
12β
13/**
14 * The options accepted by the `infiniteQueryOptions` overload selected when no
15 * `initialData` is set β `data` may be `undefined` while the query is
16 * `pending`.
17 *
18 * @template TQueryFnData - The type of a single page, as your `queryFn`
19 * resolves it.
20 * @template TError - The type of errors your `queryFn` may throw.
21 * @template TData - The type `data` ends up as after `select` runs β defaults
22 * to `InfiniteData<TQueryFnData>`, the shape of all fetched pages plus their
23 * page params.
24 * @template TQueryKey - The type of your `queryKey`.
25 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch
26 * a given page.
27 */
28export type UndefinedInitialDataInfiniteOptions<
29 TQueryFnData,
30 TError = DefaultError,
31 TData = InfiniteData<TQueryFnData>,
32 TQueryKey extends QueryKey = QueryKey,
33 TPageParam = unknown,
34> = UseInfiniteQueryOptions<
35 TQueryFnData,
36 TError,
37 TData,
38 TQueryKey,
39 TPageParam
40> & {
41 /**
42 * If set, this value will be used as the initial data for the query cache (as
43 * long as the query hasn't been created or cached yet). If set to a function,
44 * the function will be called **once** during the shared/root query
45 * initialization, and be expected to synchronously return the initial data.
46 * Initial data is considered stale by default unless a `staleTime` has been
47 * set. `initialData` **is persisted** to the cache.
48 */
49 initialData?:
50 | undefined
51 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
52 | InitialDataFunction<
53 NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
54 >
55}
56β
57/**
58 * The options accepted by the `infiniteQueryOptions` overload selected when no
59 * `initialData` is set and `queryFn` is not `skipToken` β same as
60 * {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be
61 * `skipToken`.
62 *
63 * @template TQueryFnData - The type of a single page, as your `queryFn`
64 * resolves it.
65 * @template TError - The type of errors your `queryFn` may throw.
66 * @template TData - The type `data` ends up as after `select` runs β defaults
67 * to `InfiniteData<TQueryFnData>`, the shape of all fetched pages plus their
68 * page params.
69 * @template TQueryKey - The type of your `queryKey`.
70 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch
71 * a given page.
72 */
73export type UnusedSkipTokenInfiniteOptions<
74 TQueryFnData,
75 TError = DefaultError,
76 TData = InfiniteData<TQueryFnData>,
77 TQueryKey extends QueryKey = QueryKey,
78 TPageParam = unknown,
79> = OmitKeyof<
80 UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>,
81 'queryFn'
82> & {
83 /**
84 * `skipToken` is not allowed as a value here β this overload is selected when
85 * no `initialData` is set. If you don't intend to run the query yet, set
86 * `enabled: false` β omitting `queryFn` alone still triggers a fetch that
87 * fails with "Missing queryFn" unless `enabled` is `false` or a default query
88 * function has been defined. A default query function only supplies
89 * `queryFn`; it doesn't defer the fetch on its own.
90 */
91 queryFn?: Exclude<
92 UseInfiniteQueryOptions<
93 TQueryFnData,
94 TError,
95 TData,
96 TQueryKey,
97 TPageParam
98 >['queryFn'],
99 SkipToken | undefined
100 >
101}
102β
103/**
104 * The options accepted by the `infiniteQueryOptions` overload selected when
105 * `initialData` is set β `data` is never `undefined` (unless a `select` changes
106 * `TData` to include `undefined`).
107 *
108 * @template TQueryFnData - The type of a single page, as your `queryFn`
109 * resolves it.
110 * @template TError - The type of errors your `queryFn` may throw.
111 * @template TData - The type `data` ends up as after `select` runs β defaults
112 * to `InfiniteData<TQueryFnData>`, the shape of all fetched pages plus their
113 * page params.
114 * @template TQueryKey - The type of your `queryKey`.
115 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch
116 * a given page.
117 */
118export type DefinedInitialDataInfiniteOptions<
119 TQueryFnData,
120 TError = DefaultError,
121 TData = InfiniteData<TQueryFnData>,
122 TQueryKey extends QueryKey = QueryKey,
123 TPageParam = unknown,
124> = UseInfiniteQueryOptions<
125 TQueryFnData,
126 TError,
127 TData,
128 TQueryKey,
129 TPageParam
130> & {
131 /**
132 * If set, this value will be used as the initial data for the query cache (as
133 * long as the query hasn't been created or cached yet). If set to a function,
134 * the function will be called **once** during the shared/root query
135 * initialization, and be expected to synchronously return the initial data.
136 * Initial data is considered stale by default unless a `staleTime` has been
137 * set. `initialData` **is persisted** to the cache.
138 */
139 initialData:
140 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
141 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
142 | undefined
143}
144β
145/**
146 * You can generally pass everything to `infiniteQueryOptions` that you can also
147 * pass to `useInfiniteQuery`. These options can be shared across hooks and
148 * imperative APIs such as `queryClient.infiniteQuery`. `options.queryKey` is
149 * required and is the query key to generate options for.
150 *
151 * This overload is selected when `initialData` is set.
152 *
153 * @remarks
154 * See {@link useInfiniteQuery} for examples that fetch further pages, from a
155 * button click or automatically as the user scrolls.
156 * @example
157 * ```tsx
158 * import {
159 * infiniteQueryOptions,
160 * useInfiniteQuery,
161 * } from '@tanstack/react-query'
162 *
163 * export const projectsOptions = infiniteQueryOptions({
164 * queryKey: ['projects'],
165 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
166 * initialPageParam: 0,
167 * getNextPageParam: (lastPage) => lastPage.nextId,
168 * initialData: { pages: [], pageParams: [] },
169 * })
170 *
171 * function Projects() {
172 * // `data` is never `undefined`, thanks to `initialData` β even if a refetch fails, so the
173 * // list stays visible alongside the error.
174 * const { data, isError, error } = useInfiniteQuery(projectsOptions)
175 *
176 * return (
177 * <div>
178 * {isError ? <span>Error: {error.message}</span> : null}
179 * <ul>
180 * {data.pages.map((page) =>
181 * page.projects.map((p) => <li key={p.id}>{p.name}</li>),
182 * )}
183 * </ul>
184 * </div>
185 * )
186 * }
187 * ```
188 *
189 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β
190 * everything you can pass to `useInfiniteQuery`, with `initialData` set.
191 * @returns The same options object, typed so that `queryKey` carries the
192 * inferred data type.
193 * @see {@link useInfiniteQuery} to run an infinite query with these options.
194 */
195export function infiniteQueryOptions<
196 TQueryFnData,
197 TError = DefaultError,
198 TData = InfiniteData<TQueryFnData>,
199 TQueryKey extends QueryKey = QueryKey,
200 TPageParam = unknown,
201>(
202 options: DefinedInitialDataInfiniteOptions<
203 TQueryFnData,
204 TError,
205 TData,
206 TQueryKey,
207 TPageParam
208 >,
209): DefinedInitialDataInfiniteOptions<
210 TQueryFnData,
211 TError,
212 TData,
213 TQueryKey,
214 TPageParam
215> &
216 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
217β
218/**
219 * You can generally pass everything to `infiniteQueryOptions` that you can also
220 * pass to `useInfiniteQuery`. These options can be shared across hooks and
221 * imperative APIs such as `queryClient.infiniteQuery`. `options.queryKey` is
222 * required and is the query key to generate options for.
223 *
224 * @remarks
225 * See {@link useInfiniteQuery} for examples that fetch further pages, from a
226 * button click or automatically as the user scrolls.
227 * @example
228 * A parameterized factory, so the same options object can be reused per `postId`:
229 * ```tsx
230 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/react-query'
231 *
232 * export const commentsOptions = (postId: string) =>
233 * infiniteQueryOptions({
234 * queryKey: ['post', postId, 'comments'],
235 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
236 * initialPageParam: 0,
237 * getNextPageParam: (lastPage) => lastPage.nextId,
238 * })
239 *
240 * function Comments({ postId }: { postId: string }) {
241 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
242 *
243 * if (isPending) return 'Loading...'
244 * if (isError) return <span>Error: {error.message}</span>
245 *
246 * return (
247 * <ul>
248 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
249 * </ul>
250 * )
251 * }
252 * ```
253 *
254 * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β
255 * everything you can pass to `useInfiniteQuery`.
256 * @returns The same options object, typed so that `queryKey` carries the
257 * inferred data type.
258 * @see {@link useInfiniteQuery} to run an infinite query with these options.
259 */
260export function infiniteQueryOptions<
261 TQueryFnData,
262 TError = DefaultError,
263 TData = InfiniteData<TQueryFnData>,
264 TQueryKey extends QueryKey = QueryKey,
265 TPageParam = unknown,
266>(
267 options: UnusedSkipTokenInfiniteOptions<
268 TQueryFnData,
269 TError,
270 TData,
271 TQueryKey,
272 TPageParam
273 >,
274): UnusedSkipTokenInfiniteOptions<
275 TQueryFnData,
276 TError,
277 TData,
278 TQueryKey,
279 TPageParam
280> &
281 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
282β
283/**
284 * You can generally pass everything to `infiniteQueryOptions` that you can also
285 * pass to `useInfiniteQuery`. These options can be shared across hooks and
286 * imperative APIs such as `queryClient.infiniteQuery`. `options.queryKey` is
287 * required and is the query key to generate options for.
288 *
289 * @remarks
290 * See {@link useInfiniteQuery} for examples that fetch further pages (from a
291 * button click or automatically as the user scrolls) and that use `skipToken`
292 * to disable the query until `postId` is set.
293 * @example
294 * A parameterized factory, so the same options object can be reused per `postId`:
295 * ```tsx
296 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/react-query'
297 *
298 * export const commentsOptions = (postId: string) =>
299 * infiniteQueryOptions({
300 * queryKey: ['post', postId, 'comments'],
301 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
302 * initialPageParam: 0,
303 * getNextPageParam: (lastPage) => lastPage.nextId,
304 * })
305 *
306 * function Comments({ postId }: { postId: string }) {
307 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
308 *
309 * if (isPending) return 'Loading...'
310 * if (isError) return <span>Error: {error.message}</span>
311 *
312 * return (
313 * <ul>
314 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
315 * </ul>
316 * )
317 * }
318 * ```
319 *
320 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β
321 * everything you can pass to `useInfiniteQuery`.
322 * @returns The same options object, typed so that `queryKey` carries the
323 * inferred data type.
324 * @see {@link useInfiniteQuery} to run an infinite query with these options.
325 */
326export function infiniteQueryOptions<
327 TQueryFnData,
328 TError = DefaultError,
329 TData = InfiniteData<TQueryFnData>,
330 TQueryKey extends QueryKey = QueryKey,
331 TPageParam = unknown,
332>(
333 options: UndefinedInitialDataInfiniteOptions<
334 TQueryFnData,
335 TError,
336 TData,
337 TQueryKey,
338 TPageParam
339 >,
340): UndefinedInitialDataInfiniteOptions<
341 TQueryFnData,
342 TError,
343 TData,
344 TQueryKey,
345 TPageParam
346> &
347 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
348β
349export function infiniteQueryOptions(options: unknown) {
350 return options
351}