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