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

infiniteQueryOptions.md

342 lines13.3 KB

1---
2id: infiniteQueryOptions
3title: infiniteQueryOptions
4---
5​
6## Overview
7​
8```ts
9function infiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
10function infiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UnusedSkipTokenInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): UnusedSkipTokenInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
11function infiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
12```
13​
14- [`DefinedInitialDataInfiniteOptions` β†’ `DefinedInitialDataInfiniteOptions & QueryKeyWithDataTag`](#call-signature-1): You can generally pass everything to `infiniteQueryOptions` that you can also pass to `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
15- [`UnusedSkipTokenInfiniteOptions` β†’ `UnusedSkipTokenInfiniteOptions & QueryKeyWithDataTag`](#call-signature-2): You can generally pass everything to `infiniteQueryOptions` that you can also pass to `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
16- [`UndefinedInitialDataInfiniteOptions` β†’ `UndefinedInitialDataInfiniteOptions & QueryKeyWithDataTag`](#call-signature-3): You can generally pass everything to `infiniteQueryOptions` that you can also pass to `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
17​
18See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
19​
20<a id="call-signature-1"></a>
21​
22## Call Signature
23​
24```ts
25function infiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
26```
27​
28Defined in: [packages/angular-query-experimental/src/infinite-query-options.ts:176](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/infinite-query-options.ts#L176)
29​
30You can generally pass everything to `infiniteQueryOptions` that you can also pass to
31`injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
32`queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
33for.
34​
35This overload is selected when `initialData` is set.
36​
37### Type Parameters
38​
39#### TQueryFnData
40​
41`TQueryFnData`
42​
43#### TError
44​
45`TError` = `Error`
46​
47#### TData
48​
49`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
50​
51#### TQueryKey
52​
53`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
54​
55#### TPageParam
56​
57`TPageParam` = `unknown`
58​
59### Parameters
60​
61#### options
62​
63[`DefinedInitialDataInfiniteOptions`](../type-aliases/DefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
64​
65The [DefinedInitialDataInfiniteOptions](../type-aliases/DefinedInitialDataInfiniteOptions.md) to use β€” everything you can pass to
66`injectInfiniteQuery`, with `initialData` set.
67​
68### Returns
69​
70[`DefinedInitialDataInfiniteOptions`](../type-aliases/DefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>, `TError`\>
71​
72The same options object, typed so that `queryKey` carries the inferred data type.
73​
74### Remarks
75​
76See [injectInfiniteQuery](injectInfiniteQuery.md) for examples that fetch further pages, from a button click or
77automatically as the user scrolls.
78​
79### See
80​
81[injectInfiniteQuery](injectInfiniteQuery.md) to run an infinite query with these options.
82​
83### Example
84​
85```angular-ts
86import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
87​
88export const projectsOptions = infiniteQueryOptions({
89 queryKey: ['projects'],
90 queryFn: ({ pageParam }) => fetchProjects(pageParam),
91 initialPageParam: 0,
92 getNextPageParam: (lastPage) => lastPage.nextId,
93 initialData: { pages: [], pageParams: [] },
94})
95​
96@Component({
97 selector: 'projects',
98 template: `
99 <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β€” even if a
100 refetch fails, so the list stays visible alongside the error. -->
101 <ul>
102 @for (page of projectsQuery.data().pages; track $index) {
103 @for (project of page.projects; track project.id) {
104 <li>{{ project.name }}</li>
105 }
106 }
107 </ul>
108 `,
109})
110export class Projects {
111 readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)
113```
114​
115<a id="call-signature-2"></a>
116​
117## Call Signature
118​
119```ts
120function infiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UnusedSkipTokenInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): UnusedSkipTokenInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
121```
122​
123Defined in: [packages/angular-query-experimental/src/infinite-query-options.ts:247](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/infinite-query-options.ts#L247)
124​
125You can generally pass everything to `infiniteQueryOptions` that you can also pass to
126`injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
127`queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
128for.
129​
130### Type Parameters
131​
132#### TQueryFnData
133​
134`TQueryFnData`
135​
136#### TError
137​
138`TError` = `Error`
139​
140#### TData
141​
142`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
143​
144#### TQueryKey
145​
146`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
147​
148#### TPageParam
149​
150`TPageParam` = `unknown`
151​
152### Parameters
153​
154#### options
155​
156[`UnusedSkipTokenInfiniteOptions`](../type-aliases/UnusedSkipTokenInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
157​
158The [UnusedSkipTokenInfiniteOptions](../type-aliases/UnusedSkipTokenInfiniteOptions.md) to use β€” everything you can pass to
159`injectInfiniteQuery`.
160​
161### Returns
162​
163[`UnusedSkipTokenInfiniteOptions`](../type-aliases/UnusedSkipTokenInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>, `TError`\>
164​
165The same options object, typed so that `queryKey` carries the inferred data type.
166​
167### Remarks
168​
169See [injectInfiniteQuery](injectInfiniteQuery.md) for examples that fetch further pages, from a button click or
170automatically as the user scrolls.
171​
172### See
173​
174[injectInfiniteQuery](injectInfiniteQuery.md) to run an infinite query with these options.
175​
176### Example
177​
178A parameterized factory, so the same options object can be reused per `postId`:
179```angular-ts
180import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
181​
182export const commentsOptions = (postId: string) =>
183 infiniteQueryOptions({
184 queryKey: ['post', postId, 'comments'],
185 queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
186 initialPageParam: 0,
187 getNextPageParam: (lastPage) => lastPage.nextId,
188 })
189​
190@Component({
191 selector: 'comments',
192 template: `
193 @if (commentsQuery.isPending()) {
194 Loading...
195 } @else if (commentsQuery.isError()) {
196 <span>Error: {{ commentsQuery.error()?.message }}</span>
197 } @else {
198 <ul>
199 @for (page of commentsQuery.data().pages; track $index) {
200 @for (comment of page.comments; track comment.id) {
201 <li>{{ comment.text }}</li>
202 }
203 }
204 </ul>
205 }
206 `,
207})
208export class Comments {
209 readonly postId = signal('1')
210 readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
212```
213​
214<a id="call-signature-3"></a>
215​
216## Call Signature
217​
218```ts
219function infiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
220```
221​
222Defined in: [packages/angular-query-experimental/src/infinite-query-options.ts:318](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/infinite-query-options.ts#L318)
223​
224You can generally pass everything to `infiniteQueryOptions` that you can also pass to
225`injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
226`queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
227for.
228​
229### Type Parameters
230​
231#### TQueryFnData
232​
233`TQueryFnData`
234​
235#### TError
236​
237`TError` = `Error`
238​
239#### TData
240​
241`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
242​
243#### TQueryKey
244​
245`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
246​
247#### TPageParam
248​
249`TPageParam` = `unknown`
250​
251### Parameters
252​
253#### options
254​
255[`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
256​
257The [UndefinedInitialDataInfiniteOptions](../type-aliases/UndefinedInitialDataInfiniteOptions.md) to use β€” everything you can pass to
258`injectInfiniteQuery`.
259​
260### Returns
261​
262[`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>, `TError`\>
263​
264The same options object, typed so that `queryKey` carries the inferred data type.
265​
266### Remarks
267​
268See [injectInfiniteQuery](injectInfiniteQuery.md) for examples that fetch further pages (from a button click or
269automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
270​
271### See
272​
273[injectInfiniteQuery](injectInfiniteQuery.md) to run an infinite query with these options.
274​
275### Example
276​
277A parameterized factory, so the same options object can be reused per `postId`:
278```angular-ts
279import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
280​
281export const commentsOptions = (postId: string) =>
282 infiniteQueryOptions({
283 queryKey: ['post', postId, 'comments'],
284 queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
285 initialPageParam: 0,
286 getNextPageParam: (lastPage) => lastPage.nextId,
287 })
288​
289@Component({
290 selector: 'comments',
291 template: `
292 @if (commentsQuery.isPending()) {
293 Loading...
294 } @else if (commentsQuery.isError()) {
295 <span>Error: {{ commentsQuery.error()?.message }}</span>
296 } @else {
297 <ul>
298 @for (page of commentsQuery.data().pages; track $index) {
299 @for (comment of page.comments; track comment.id) {
300 <li>{{ comment.text }}</li>
301 }
302 }
303 </ul>
304 }
305 `,
306})
307export class Comments {
308 readonly postId = signal('1')
309 readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
311```
312​
313<a id="parameters-summary"></a>
314​
315## Parameters
316​
317### options
318​
319[`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
320​
321The [UndefinedInitialDataInfiniteOptions](../type-aliases/UndefinedInitialDataInfiniteOptions.md) to use β€” everything you can pass to
322`injectInfiniteQuery`.
323​
324<a id="options-properties"></a>
325​
326#### `options` properties
327​
328Built from [`CreateInfiniteQueryOptions`](../interfaces/CreateInfiniteQueryOptions.md#properties). See the type above for what it changes.
329​
330<a id="returns-summary"></a>
331​
332## Returns
333​
334[`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>, `TError`\>
335​
336The same options object, typed so that `queryKey` carries the inferred data type.
337​
338<a id="result-properties"></a>
339​
340### Result properties
341​
342Built from [`CreateInfiniteQueryOptions`](../interfaces/CreateInfiniteQueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.