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

injectQueries.md

178 lines13.1 KB

1---
2id: injectQueries
3title: injectQueries
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8```ts
9function injectQueries<T extends any[], TCombinedResult = T extends [] ? [] : T extends [Head] ? [GenericGetDefinedOrUndefinedQueryResult<Head, InferDataAndError<Head>["data"], CreateQueryResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>, DefinedCreateQueryResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>>] : T extends [Head, ...Tails[]] ? [...Tails[]] extends [] ? [] : [...Tails[]] extends [Head] ? [GenericGetDefinedOrUndefinedQueryResult<Head, InferDataAndError<Head>["data"], CreateQueryResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>, DefinedCreateQueryResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>>, GenericGetDefinedOrUndefinedQueryResult<Head, InferDataAndError<Head>["data"], CreateQueryResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>, DefinedCreateQueryResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>>] : [...Tails[]] extends [Head, ...Tails[]] ? [...Tails[]] extends [] ? [] : [...Tails[]] extends [Head] ? [GenericGetDefinedOrUndefinedQueryResult<Head, ...[...], CreateQueryResult<..., ...>, DefinedCreateQueryResult<..., ...>>, GenericGetDefinedOrUndefinedQueryResult<Head, ...[...], CreateQueryResult<..., ...>, DefinedCreateQueryResult<..., ...>>, GenericGetDefinedOrUndefinedQueryResult<Head, ...[...], CreateQueryResult<..., ...>, DefinedCreateQueryResult<..., ...>>] : [...Tails[]] extends [Head, ...Tails[]] ? [...(...)[]] extends [] ? [] : ... extends ... ? ... : ... : [...{ [K in (...)]: (...) }[]] : [...{ [K in string | number | symbol]: GenericGetDefinedOrUndefinedQueryResult<Tails[K], InferDataAndError<(...)>["data"], CreateQueryResult<(...)[(...)], (...)[(...)]>, DefinedCreateQueryResult<(...)[(...)], (...)[(...)]>> }[]] : { [K in string | number | symbol]: GenericGetDefinedOrUndefinedQueryResult<T[K], InferDataAndError<T[K]>["data"], CreateQueryResult<InferDataAndError<T[K]>["data"], InferDataAndError<T[K]>["error"]>, DefinedCreateQueryResult<InferDataAndError<T[K]>["data"], InferDataAndError<T[K]>["error"]>> }>(optionsFn: () => InjectQueriesOptions<T, TCombinedResult>): Signal<TCombinedResult>;
10```
11​
12Defined in: [packages/angular-query/src/inject-queries.ts:153](https://github.com/TanStack/query/blob/main/packages/angular-query/src/inject-queries.ts#L153)
13​
14Injects a signal to fetch a variable number of queries.
15​
16The `queries` key accepts an array with query option objects mostly identical to `injectQuery`'s. Having
17the same query key more than once in the array of query objects may cause some data to be shared between
18queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
19structure.
20​
21The `combine` option can be used to combine the results of the queries into a single value.
22​
23## Type Parameters
24​
25### T
26​
27`T` *extends* `any`[]
28​
29### TCombinedResult
30​
31`TCombinedResult` = `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GenericGetDefinedOrUndefinedQueryResult`\<`Head`, `InferDataAndError`\<`Head`\>\[`"data"`\], [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>, [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GenericGetDefinedOrUndefinedQueryResult`\<`Head`, `InferDataAndError`\<`Head`\>\[`"data"`\], [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>, [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>\>, `GenericGetDefinedOrUndefinedQueryResult`\<`Head`, `InferDataAndError`\<`Head`\>\[`"data"`\], [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>, [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GenericGetDefinedOrUndefinedQueryResult`\<`Head`, ...\[...\], [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<..., ...\>, [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<..., ...\>\>, `GenericGetDefinedOrUndefinedQueryResult`\<`Head`, ...\[...\], [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<..., ...\>, [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<..., ...\>\>, `GenericGetDefinedOrUndefinedQueryResult`\<`Head`, ...\[...\], [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<..., ...\>, [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<..., ...\>\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \[...\{ \[K in string \| number \| symbol\]: GenericGetDefinedOrUndefinedQueryResult\<Tails\[K\], InferDataAndError\<(...)\>\["data"\], CreateQueryResult\<(...)\[(...)\], (...)\[(...)\]\>, DefinedCreateQueryResult\<(...)\[(...)\], (...)\[(...)\]\>\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GenericGetDefinedOrUndefinedQueryResult\<T\[K\], InferDataAndError\<T\[K\]\>\["data"\], CreateQueryResult\<InferDataAndError\<T\[K\]\>\["data"\], InferDataAndError\<T\[K\]\>\["error"\]\>, DefinedCreateQueryResult\<InferDataAndError\<T\[K\]\>\["data"\], InferDataAndError\<T\[K\]\>\["error"\]\>\> \}
32​
33## Parameters
34​
35### optionsFn
36​
37() => [`InjectQueriesOptions`](../interfaces/InjectQueriesOptions.md)\<`T`, `TCombinedResult`\>
38​
39A function returning the queries' options β€” an array of query option objects under
40`queries`, and an optional `combine`. Similar to `computed` from Angular, this function runs in the
41reactive context, so signals read inside it (e.g. to build the `queries` array) drive the queries.
42​
43<a id="optionsFn-properties"></a>
44​
45#### `optionsFn` properties
46​
47| Property | Type | Description |
48| ------ | ------ | ------ |
49| <a id="optionsFn-property-combine"></a> `combine?` | (`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GenericGetDefinedOrUndefinedQueryResult`\<`Head`, `InferDataAndError`\<`Head`\>\[`"data"`\], [`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>, [`DefinedQueryObserverResult`](../type-aliases/DefinedQueryObserverResult.md)\<`InferDataAndError`\<`Head`\>\[`"data"`\], `InferDataAndError`\<`Head`\>\[`"error"`\]\>\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GenericGetDefinedOrUndefinedQueryResult`\<`Head`, `InferDataAndError`\<...\>\[`"data"`\], [`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<...\[...\], ...\[...\]\>, [`DefinedQueryObserverResult`](../type-aliases/DefinedQueryObserverResult.md)\<...\[...\], ...\[...\]\>\>, `GenericGetDefinedOrUndefinedQueryResult`\<`Head`, `InferDataAndError`\<...\>\[`"data"`\], [`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<...\[...\], ...\[...\]\>, [`DefinedQueryObserverResult`](../type-aliases/DefinedQueryObserverResult.md)\<...\[...\], ...\[...\]\>\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ..., ...\] : ... *extends* ... ? ... : ... : \[...\{ \[K in (...) \| (...) \| (...)\]: GenericGetDefinedOrUndefinedQueryResult\<(...), (...), (...), (...)\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GenericGetDefinedOrUndefinedQueryResult\<T\[K\], InferDataAndError\<T\[K\]\>\["data"\], QueryObserverResult\<InferDataAndError\<(...)\[(...)\]\>\["data"\], InferDataAndError\<(...)\[(...)\]\>\["error"\]\>, DefinedQueryObserverResult\<InferDataAndError\<(...)\[(...)\]\>\["data"\], InferDataAndError\<(...)\[(...)\]\>\["error"\]\>\> \}) => `TCombinedResult` | Combines the plain results of all the queries into a single value, which `injectQueries` returns instead of the results array. The value is recomputed reactively. |
50| <a id="optionsFn-property-queries"></a> `queries` | \| readonly \[\{ \[K in string \| number \| symbol\]: GetCreateQueryOptionsForCreateQueries\<T\[K\]\> \}\] \| readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetCreateQueryOptionsForCreateQueries`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetCreateQueryOptionsForCreateQueries`\<`Head`\>, `GetCreateQueryOptionsForCreateQueries`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ..., ...\] : ... *extends* ... ? ... : ... : readonly `unknown`[] *extends* \[`...Tails[]`\] ? \[`...Tails[]`\] : \[`...(...)[]`\] *extends* ...[] ? ...[] : ...[] : readonly `unknown`[] *extends* `T` ? `T` : `T` *extends* `QueryObserverOptionsForCreateQueries`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] ? `QueryObserverOptionsForCreateQueries`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] : `QueryObserverOptionsForCreateQueries`\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]\] | The array of query options to run, one query per element. Entries can be added, removed, or replaced reactively. |
51​
52## Returns
53​
54`Signal`\<`TCombinedResult`\>
55​
56A `Signal` with the combined result. Without `combine`, this is an array with all the query
57results, in the same order as the input. When `combine` is provided, this is the value returned by
58`combine` instead.
59​
60## Remarks
61​
62Unlike `injectQuery`, `injectQueries` cannot infer the `data` argument of an _inline_ `select`
63from its sibling `queryFn`. Because `injectQueries` infers the type of the whole `queries` array at once,
64the `select` parameter of a query object written inline cannot be contextually typed from that same
65object's `queryFn`, so it falls back to `unknown` β€” a
66[known TypeScript limitation](https://github.com/TanStack/query/issues/6556). Annotate the `select`
67parameter explicitly, or define the query with [queryOptions](queryOptions.md), which resolves its types in a single
68object _before_ it reaches `injectQueries`, to work around this β€” see the example below.
69​
70## See
71​
72https://tanstack.com/query/latest/docs/framework/angular/guides/parallel-queries
73​
74## Examples
75​
76```angular-ts
77@Component({
78 selector: 'posts',
79 template: `
80 <ul>
81 @for (query of postQueries(); track $index) {
82 @if (query.isPending()) {
83 <li>Loading...</li>
84 } @else if (query.isError()) {
85 <li>Error: {{ query.error()?.message }}</li>
86 } @else {
87 <li>{{ query.data().title }}</li>
88 }
89 }
90 </ul>
91 `,
92})
93export class Posts {
94 readonly ids = signal([1, 2, 3])
95​
96 readonly postQueries = injectQueries(() => ({
97 queries: this.ids().map((id) => ({
98 queryKey: ['post', id],
99 queryFn: () => fetchPost(id),
100 staleTime: Infinity,
101 })),
102 }))
104```
105​
106Combining results into a single value:
107```angular-ts
108@Component({
109 selector: 'posts',
110 template: `
111 @if (combined().isPending) {
112 Loading...
113 } @else if (combined().isError) {
114 Error loading posts
115 } @else {
116 <ul>
117 @for (post of combined().data; track post?.id) {
118 <li>{{ post?.title }}</li>
119 }
120 </ul>
121 }
122 `,
123})
124export class Posts {
125 readonly ids = signal([1, 2, 3])
126​
127 readonly combined = injectQueries(() => ({
128 queries: this.ids().map((id) => ({
129 queryKey: ['post', id],
130 queryFn: () => fetchPost(id),
131 })),
132 combine: (postQueries) => ({
133 data: postQueries.map((query) => query.data),
134 isPending: postQueries.some((query) => query.isPending),
135 isError: postQueries.some((query) => query.isError),
136 }),
137 }))
139```
140​
141Typing `select` via [queryOptions](queryOptions.md). Note that spreading a `queryOptions` result and overriding
142`select` inline still falls back to `unknown` β€” wrap the spread in `queryOptions` again so the override is
143resolved before it reaches `injectQueries`:
144```angular-ts
145const postOptions = (id: number) =>
146 queryOptions({
147 queryKey: ['post', id],
148 queryFn: () => fetchPost(id),
149 })
150​
151@Component({
152 selector: 'post-title',
153 template: `<h1>{{ fixed()[0].data() }}</h1>`,
154})
155export class PostTitle {
156 readonly id = signal(1)
157​
158 readonly broken = injectQueries(() => ({
159 queries: [
160 {
161 ...postOptions(this.id()),
162 // ❌ `data` is `unknown` here
163 select: (data) => data.title,
164 },
165 ],
166 }))
167​
168 readonly fixed = injectQueries(() => ({
169 queries: [
170 queryOptions({
171 ...postOptions(this.id()),
172 // βœ… `data` is `Post`
173 select: (data) => data.title,
174 }),
175 ],
176 }))
178```