Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to combine two TanStack Query results into a single object? #9999

Unanswered
jon9090 asked this question in Q&A
Discussion options

I have two separate queries using @tanstack/angular-query:

roles = injectQuery(() => ({
 enabled: this.user.data(),
 queryKey: ['roles'],
 queryFn: () =>
 lastValueFrom(getRoles({ roles: this.user.data().roles })),
}));
user = injectQuery(() => ({
 queryKey: ['user'],
 queryFn: () => lastValueFrom(getUser()),
}));

I want to keep these queries separate, but in the UI I want to consume them as one combined object, e.g.:

<div *ngIf="user.data() as user">
 roles: {{ user.roles.length }}
</div>

Ideally something like:

userActive = injectQueries(() => ({
 queries: [this.user, this.roles],
 select: (user, roles) => this.toUser(user, roles),
}));
toUser(user, roles) {
 return { ...user, roles: roles };
}

But I can't figure out how to combine the results like this.
I also tried a computed signal, but computed() returns null until both queries load, and I'd prefer to stay within the TanStack Query API:

activeUser = computed(() => {
 const user = this.user.data();
 const roles = this.roles.data();
 if (!user) return;
 if (!roles) return;
 return this.toUser(user, roles);
});

Is there a built-in way in TanStack Query for Angular to derive/merge the results of two queries into one object (similar to injectQueries + select), while still keeping both queries independent?

You must be logged in to vote

Replies: 1 comment

Comment options

not sure if injectQueries supports combine but the query-core does, so the idea is:

userActive = injectQueries(() => ({
 queries: [this.user, this.roles],
 combine: ([user, roles]) => this.toUser(user, roles),
}));

looking at the type signature of injectQueries, it does support combine too:

export interface InjectQueriesOptions<
T extends Array<any>,
TCombinedResult = QueriesResults<T>,
> {
queries:
| readonly [...QueriesOptions<T>]
| readonly [
...{ [K in keyof T]: GetCreateQueryOptionsForCreateQueries<T[K]> },
]
combine?: (result: QueriesResults<T>) => TCombinedResult
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants
Converted from issue

This discussion was converted from issue #9915 on December 28, 2025 10:34.

AltStyle によって変換されたページ (->オリジナル) /