function 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>;
Defined in: packages/angular-query/src/inject-queries.ts:153
Injects a signal to fetch a variable number of queries.
The queries key accepts an array with query option objects mostly identical to injectQuery's. Having
the same query key more than once in the array of query objects may cause some data to be shared between
queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
structure.
The combine option can be used to combine the results of the queries into a single value.
Type Parameters#
T#
T extends any[]
TCombinedResult#
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"]>> }
Parameters#
optionsFn#
() => InjectQueriesOptions<T, TCombinedResult>
A function returning the queries' options β an array of query option objects under
queries, and an optional combine. Similar to computed from Angular, this function runs in the
reactive context, so signals read inside it (e.g. to build the queries array) drive the queries.
optionsFn properties#
| Property | Type | Description |
|---|---|---|
combine? | (result: T extends [] ? [] : T extends [Head] ? [GenericGetDefinedOrUndefinedQueryResult<Head, InferDataAndError<Head>["data"], QueryObserverResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>, DefinedQueryObserverResult<InferDataAndError<Head>["data"], InferDataAndError<Head>["error"]>>] : T extends [Head, ...Tails[]] ? [...Tails[]] extends [] ? [] : [...Tails[]] extends [Head] ? [GenericGetDefinedOrUndefinedQueryResult<Head, InferDataAndError<...>["data"], QueryObserverResult<...[...], ...[...]>, DefinedQueryObserverResult<...[...], ...[...]>>, GenericGetDefinedOrUndefinedQueryResult<Head, InferDataAndError<...>["data"], QueryObserverResult<...[...], ...[...]>, DefinedQueryObserverResult<...[...], ...[...]>>] : [...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. |
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. |
Returns#
Signal<TCombinedResult>
A Signal with the combined result. Without combine, this is an array with all the query
results, in the same order as the input. When combine is provided, this is the value returned by
combine instead.
Remarks#
Unlike injectQuery, injectQueries cannot infer the data argument of an inline select
from its sibling queryFn. Because injectQueries infers the type of the whole queries array at once,
the select parameter of a query object written inline cannot be contextually typed from that same
object's queryFn, so it falls back to unknown β a
known TypeScript limitation. Annotate the select
parameter explicitly, or define the query with queryOptions, which resolves its types in a single
object before it reaches injectQueries, to work around this β see the example below.
See#
https://tanstack.com/query/latest/docs/framework/angular/guides/parallel-queries
Examples#
@Component({
selector: 'posts',
template: `
<ul>
@for (query of postQueries(); track $index) {
@if (query.isPending()) {
<li>Loading...</li>
} @else if (query.isError()) {
<li>Error: {{ query.error()?.message }}</li>
} @else {
<li>{{ query.data().title }}</li>
}
}
</ul>
`,
})
export class Posts {
readonly ids = signal([1, 2, 3])
readonly postQueries = injectQueries(() => ({
queries: this.ids().map((id) => ({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
staleTime: Infinity,
})),
}))
}
Combining results into a single value:
@Component({
selector: 'posts',
template: `
@if (combined().isPending) {
Loading...
} @else if (combined().isError) {
Error loading posts
} @else {
<ul>
@for (post of combined().data; track post?.id) {
<li>{{ post?.title }}</li>
}
</ul>
}
`,
})
export class Posts {
readonly ids = signal([1, 2, 3])
readonly combined = injectQueries(() => ({
queries: this.ids().map((id) => ({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
})),
combine: (postQueries) => ({
data: postQueries.map((query) => query.data),
isPending: postQueries.some((query) => query.isPending),
isError: postQueries.some((query) => query.isError),
}),
}))
}
Typing select via queryOptions. Note that spreading a queryOptions result and overriding
select inline still falls back to unknown β wrap the spread in queryOptions again so the override is
resolved before it reaches injectQueries:
const postOptions = (id: number) =>
queryOptions({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
})
@Component({
selector: 'post-title',
template: `<h1>{{ fixed()[0].data() }}</h1>`,
})
export class PostTitle {
readonly id = signal(1)
readonly broken = injectQueries(() => ({
queries: [
{
...postOptions(this.id()),
// β `data` is `unknown` here
select: (data) => data.title,
},
],
}))
readonly fixed = injectQueries(() => ({
queries: [
queryOptions({
...postOptions(this.id()),
// β
`data` is `Post`
select: (data) => data.title,
}),
],
}))
}