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

infiniteQueryOptions.ts

319 lines11.3 KB

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