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

infiniteQueryOptions.ts

310 lines11.5 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 `initialData` is omitted, may be
16 * `undefined`, or is a function that may return `undefined` β€” `data` may be `undefined` while the query is
17 * `pending`.
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 `queryFn` is not `skipToken` and
55 * `initialData` is omitted, may be `undefined`, or is a function that may return `undefined` β€” same as
56 * {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be `skipToken`.
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 `initialData` is omitted, may
76 * be `undefined`, or is a function that may return `undefined`. If you don't intend to run the query yet,
77 * set `enabled: false` β€” omitting `queryFn` alone still triggers a fetch that fails with "Missing queryFn"
78 * unless `enabled` is `false` or a default query function has been defined. A default query function only
79 * 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 >
92​
93/**
94 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be
95 * defined β€” `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
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>>)
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 known to be defined.
134 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`, with `initialData` known to be defined.
135 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
136 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
137 * automatically as the user scrolls.
138 * @see {@link useInfiniteQuery} to run an infinite query with these options.
139 * @example
140 * ```tsx
141 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
142 *
143 * export const projectsOptions = infiniteQueryOptions({
144 * queryKey: ['projects'],
145 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
146 * initialPageParam: 0,
147 * getNextPageParam: (lastPage) => lastPage.nextId,
148 * initialData: { pages: [], pageParams: [] },
149 * })
150 *
151 * function Projects() {
152 * // `data` is never `undefined`, thanks to `initialData` β€” even if a refetch fails, so the
153 * // list stays visible alongside the error.
154 * const { data, isError, error } = useInfiniteQuery(projectsOptions)
155 *
156 * return (
157 * <div>
158 * {isError ? <span>Error: {error.message}</span> : null}
159 * <ul>
160 * {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
161 * </ul>
162 * </div>
163 * )
164 * }
165 * ```
166 */
167export function infiniteQueryOptions<
168 TQueryFnData,
169 TError = DefaultError,
170 TData = InfiniteData<TQueryFnData>,
171 TQueryKey extends QueryKey = QueryKey,
172 TPageParam = unknown,
173>(
174 options: DefinedInitialDataInfiniteOptions<
175 TQueryFnData,
176 TError,
177 TData,
178 TQueryKey,
179 TPageParam
180 >,
181): DefinedInitialDataInfiniteOptions<
182 TQueryFnData,
183 TError,
184 TData,
185 TQueryKey,
186 TPageParam
187> &
188 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
189​
190/**
191 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
192 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
193 * `options.queryKey` is required and is the query key to generate options for.
194 * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
195 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
196 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
197 * automatically as the user scrolls.
198 * @see {@link useInfiniteQuery} to run an infinite query with these options.
199 * @example
200 * A parameterized factory, so the same options object can be reused per `postId`:
201 * ```tsx
202 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
203 *
204 * export const commentsOptions = (postId: string) =>
205 * infiniteQueryOptions({
206 * queryKey: ['post', postId, 'comments'],
207 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
208 * initialPageParam: 0,
209 * getNextPageParam: (lastPage) => lastPage.nextId,
210 * })
211 *
212 * function Comments({ postId }: { postId: string }) {
213 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
214 *
215 * if (isPending) return 'Loading...'
216 * if (isError) return <span>Error: {error.message}</span>
217 *
218 * return (
219 * <ul>
220 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
221 * </ul>
222 * )
223 * }
224 * ```
225 */
226export function infiniteQueryOptions<
227 TQueryFnData,
228 TError = DefaultError,
229 TData = InfiniteData<TQueryFnData>,
230 TQueryKey extends QueryKey = QueryKey,
231 TPageParam = unknown,
232>(
233 options: UnusedSkipTokenInfiniteOptions<
234 TQueryFnData,
235 TError,
236 TData,
237 TQueryKey,
238 TPageParam
239 >,
240): UnusedSkipTokenInfiniteOptions<
241 TQueryFnData,
242 TError,
243 TData,
244 TQueryKey,
245 TPageParam
246> &
247 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
248​
249/**
250 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
251 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
252 * `options.queryKey` is required and is the query key to generate options for.
253 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
254 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
255 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages (from a button click or
256 * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
257 * @see {@link useInfiniteQuery} to run an infinite query with these options.
258 * @example
259 * A parameterized factory, so the same options object can be reused per `postId`:
260 * ```tsx
261 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
262 *
263 * export const commentsOptions = (postId: string) =>
264 * infiniteQueryOptions({
265 * queryKey: ['post', postId, 'comments'],
266 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
267 * initialPageParam: 0,
268 * getNextPageParam: (lastPage) => lastPage.nextId,
269 * })
270 *
271 * function Comments({ postId }: { postId: string }) {
272 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
273 *
274 * if (isPending) return 'Loading...'
275 * if (isError) return <span>Error: {error.message}</span>
276 *
277 * return (
278 * <ul>
279 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
280 * </ul>
281 * )
282 * }
283 * ```
284 */
285export function infiniteQueryOptions<
286 TQueryFnData,
287 TError = DefaultError,
288 TData = InfiniteData<TQueryFnData>,
289 TQueryKey extends QueryKey = QueryKey,
290 TPageParam = unknown,
291>(
292 options: UndefinedInitialDataInfiniteOptions<
293 TQueryFnData,
294 TError,
295 TData,
296 TQueryKey,
297 TPageParam
298 >,
299): UndefinedInitialDataInfiniteOptions<
300 TQueryFnData,
301 TError,
302 TData,
303 TQueryKey,
304 TPageParam
305> &
306 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
307​
308export function infiniteQueryOptions(options: unknown) {
309 return options