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

infinite-query-options.ts

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