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

injectInfiniteQuery.md

350 lines11.0 KB

1---
2id: injectInfiniteQuery
3title: injectInfiniteQuery
4---
5​
6## Call Signature
7​
8```ts
9function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): DefinedCreateInfiniteQueryResult<TData, TError>;
10```
11​
12Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:83](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L83)
13​
14The options for `injectInfiniteQuery` are identical to `injectQuery`, with the addition of
15`initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`. Infinite queries can
16additively "load more" data onto an existing set of data, or "infinite scroll".
17​
18This overload is selected when `initialData` is set on the options returned by `injectInfiniteQueryFn`,
19so the resulting `data` signal is never `undefined` (unless a `select` changes `TData` to include `undefined`).
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#### injectInfiniteQueryFn
46​
47() => [`DefinedInitialDataInfiniteOptions`](../type-aliases/DefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
48​
49A function returning the [DefinedInitialDataInfiniteOptions](../type-aliases/DefinedInitialDataInfiniteOptions.md) to use β€”
50everything you can pass to `injectInfiniteQuery`, with `initialData` set. Similar to `computed` from
51Angular, this function runs in the reactive context, so signals read inside it drive the query.
52​
53#### options?
54​
55[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
56​
57Additional configuration.
58​
59### Returns
60​
61[`DefinedCreateInfiniteQueryResult`](../type-aliases/DefinedCreateInfiniteQueryResult.md)\<`TData`, `TError`\>
62​
63The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
64`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
65`data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
66`InfiniteData<TQueryFnData>` shape.
67​
68### Remarks
69​
70Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
71refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
72actions, or add conditions like `hasNextPage() && !isFetching()`.
73​
74### See
75​
76[infiniteQueryOptions](infiniteQueryOptions.md) to share these options between `injectInfiniteQuery` and imperative APIs
77like `queryClient.fetchInfiniteQuery`.
78​
79### Example
80​
81```angular-ts
82@Component({
83 selector: 'projects',
84 template: `
85 <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β€” even if a
86 refetch fails, so the list stays visible alongside the error. -->
87 <ul>
88 @for (page of projectsQuery.data().pages; track $index) {
89 @for (project of page.projects; track project.id) {
90 <li>{{ project.name }}</li>
91 }
92 }
93 </ul>
94 `,
95})
96export class Projects {
97 readonly projectsQuery = injectInfiniteQuery(() => ({
98 queryKey: ['projects'],
99 queryFn: ({ pageParam }) => fetchProjects(pageParam),
100 initialPageParam: 0,
101 getNextPageParam: (lastPage) => lastPage.nextId,
102 initialData: { pages: [], pageParams: [] },
103 }))
105```
106​
107## Call Signature
108​
109```ts
110function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
111```
112​
113Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:239](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L239)
114​
115Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a
116unique key. Infinite queries can additively "load more" data onto an existing set of data, or
117"infinite scroll".
118​
119### Type Parameters
120​
121#### TQueryFnData
122​
123`TQueryFnData`
124​
125#### TError
126​
127`TError` = `Error`
128​
129#### TData
130​
131`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
132​
133#### TQueryKey
134​
135`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
136​
137#### TPageParam
138​
139`TPageParam` = `unknown`
140​
141### Parameters
142​
143#### injectInfiniteQueryFn
144​
145() => [`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
146​
147A function returning the [UndefinedInitialDataInfiniteOptions](../type-aliases/UndefinedInitialDataInfiniteOptions.md) to use
148β€” everything you can pass to `injectInfiniteQuery`. Similar to `computed` from Angular, this function runs
149in the reactive context, so signals read inside it drive the query.
150​
151#### options?
152​
153[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
154​
155Additional configuration.
156​
157### Returns
158​
159[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
160​
161The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
162`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
163`data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
164`InfiniteData<TQueryFnData>` shape.
165​
166### Remarks
167​
168Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
169refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
170actions, or add conditions like `hasNextPage() && !isFetching()`. This is the only overload that accepts
171`queryFn: skipToken`, shown below.
172​
173### See
174​
175[infiniteQueryOptions](infiniteQueryOptions.md) to share these options between `injectInfiniteQuery` and imperative APIs
176like `queryClient.fetchInfiniteQuery`.
177​
178### Examples
179​
180Fetching the next page from a button click:
181```angular-ts
182@Component({
183 selector: 'projects-list',
184 template: `
185 <ul>
186 @for (page of projectsQuery.data()?.pages; track $index) {
187 @for (project of page.projects; track project.id) {
188 <li>{{ project.name }}</li>
189 }
190 }
191 </ul>
192 <button
193 [disabled]="!projectsQuery.hasNextPage() || projectsQuery.isFetching()"
194 (click)="projectsQuery.fetchNextPage()"
195 >
196 Load More
197 </button>
198 `,
199})
200export class ProjectsList {
201 readonly projectsQuery = injectInfiniteQuery(() => ({
202 queryKey: ['projects'],
203 queryFn: ({ pageParam }) => fetchProjects(pageParam),
204 initialPageParam: 0,
205 getNextPageParam: (lastPage) => lastPage.nextId,
206 }))
208```
209​
210Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a sentinel
211element after the list:
212```angular-ts
213@Component({
214 selector: 'projects-list',
215 template: `
216 <ul>
217 @for (page of projectsQuery.data()?.pages; track $index) {
218 @for (project of page.projects; track project.id) {
219 <li>{{ project.name }}</li>
220 }
221 }
222 </ul>
223 <div #sentinel>{{ projectsQuery.isFetchingNextPage() ? 'Loading more...' : '' }}</div>
224 `,
225})
226export class ProjectsList {
227 readonly sentinel = viewChild<ElementRef<HTMLElement>>('sentinel')
228​
229 readonly projectsQuery = injectInfiniteQuery(() => ({
230 queryKey: ['projects'],
231 queryFn: ({ pageParam }) => fetchProjects(pageParam),
232 initialPageParam: 0,
233 getNextPageParam: (lastPage) => lastPage.nextId,
234 }))
235​
236 constructor() {
237 effect((onCleanup) => {
238 const sentinel = this.sentinel()?.nativeElement
239 if (
240 sentinel == null ||
241 !this.projectsQuery.hasNextPage() ||
242 this.projectsQuery.isFetching()
243 ) {
244 return
245 }
246​
247 const observer = new IntersectionObserver(([entry]) => {
248 if (entry?.isIntersecting) this.projectsQuery.fetchNextPage()
249 })
250 observer.observe(sentinel)
251​
252 onCleanup(() => observer.disconnect())
253 })
254 }
256```
257​
258A query that's disabled, type safe, until `postId` is set β€” pass `skipToken` as `queryFn` instead of
259setting `enabled: false`:
260```angular-ts
261@Component({
262 selector: 'comments',
263 template: `
264 @if (postId() == null) {
265 Select a post
266 } @else if (commentsQuery.isPending()) {
267 Loading...
268 } @else if (commentsQuery.isError()) {
269 <span>Error: {{ commentsQuery.error()?.message }}</span>
270 } @else {
271 <ul>
272 @for (page of commentsQuery.data().pages; track $index) {
273 @for (comment of page.comments; track comment.id) {
274 <li>{{ comment.text }}</li>
275 }
276 }
277 </ul>
278 }
279 `,
280})
281export class Comments {
282 readonly postId = signal<string | undefined>(undefined)
283​
284 readonly commentsQuery = injectInfiniteQuery(() => ({
285 queryKey: ['post', this.postId(), 'comments'],
286 queryFn:
287 this.postId() != null
288 ? ({ pageParam }) => fetchComments(this.postId()!, pageParam)
289 : skipToken,
290 initialPageParam: 0,
291 getNextPageParam: (lastPage) => lastPage.nextId,
292 }))
294```
295​
296## Call Signature
297​
298```ts
299function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
300```
301​
302Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:267](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L267)
303​
304This overload accepts the general [CreateInfiniteQueryOptions](../interfaces/CreateInfiniteQueryOptions.md) shape rather than the
305`initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call site β€”
306useful when wrapping `injectInfiniteQuery` in your own helper function that forwards caller-provided
307options.
308​
309### Type Parameters
310​
311#### TQueryFnData
312​
313`TQueryFnData`
314​
315#### TError
316​
317`TError` = `Error`
318​
319#### TData
320​
321`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
322​
323#### TQueryKey
324​
325`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
326​
327#### TPageParam
328​
329`TPageParam` = `unknown`
330​
331### Parameters
332​
333#### injectInfiniteQueryFn
334​
335() => [`CreateInfiniteQueryOptions`](../interfaces/CreateInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
336​
337A function that returns infinite query options. Similar to `computed` from
338Angular, this function runs in the reactive context, so signals read inside it drive the query.
339​
340#### options?
341​
342[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
343​
344Additional configuration.
345​
346### Returns
347​
348[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
349​
350The infinite query result.