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

infiniteQueryOptions.ts

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