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