Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

infiniteQueryOptions.ts

336 lines11.7 KB

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