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

infiniteQueryOptions.md

285 lines8.6 KB

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