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

parallel-queries.md

134 lines3.7 KB

1---
2id: parallel-queries
3title: Parallel Queries
4---
5​
6Parallel queries are queries that run at the same time so the UI does not wait for one request before starting the next.
7​
8## Manual Parallel Queries
9​
10When the number of queries is fixed, create multiple query controllers on the same host. They will all subscribe when the host connects.
11​
12```ts
13import { LitElement, html } from 'lit'
14import { createQueryController } from '@tanstack/lit-query'
15​
16class DashboardView extends LitElement {
17 private readonly users = createQueryController(this, {
18 queryKey: ['users'],
19 queryFn: fetchUsers,
20 })
21​
22 private readonly teams = createQueryController(this, {
23 queryKey: ['teams'],
24 queryFn: fetchTeams,
25 })
26​
27 private readonly projects = createQueryController(this, {
28 queryKey: ['projects'],
29 queryFn: fetchProjects,
30 })
31​
32 render() {
33 const users = this.users()
34 const teams = this.teams()
35 const projects = this.projects()
36​
37 if (users.isPending || teams.isPending || projects.isPending) {
38 return html`Loading...`
39 }
40​
41 if (users.isError || teams.isError || projects.isError) {
42 return html`Unable to load dashboard`
43 }
44​
45 return html`
46 <dashboard-summary
47 .users=${users.data}
48 .teams=${teams.data}
49 .projects=${projects.data}
50 ></dashboard-summary>
51 `
52 }
54```
55​
56Each controller receives the same `ReactiveControllerHost`. If no explicit `QueryClient` is passed, each controller resolves the nearest connected `QueryClientProvider`.
57​
58## Dynamic Parallel Queries
59​
60When the number of queries changes with host state, use [`createQueriesController`](../reference/functions/createQueriesController.md). It accepts a `queries` array and returns an accessor for the array of query results.
61​
62Use an options getter when the query list depends on reactive host fields:
63​
64```ts
65import { LitElement, html } from 'lit'
66import { createQueriesController } from '@tanstack/lit-query'
67​
68class UsersDetails extends LitElement {
69 static properties = {
70 userIds: { attribute: false },
71 }
72​
73 userIds: Array<string> = []
74​
75 private readonly users = createQueriesController(this, () => ({
76 queries: this.userIds.map((id) => ({
77 queryKey: ['user', id],
78 queryFn: () => fetchUserById(id),
79 })),
80 }))
81​
82 render() {
83 const userQueries = this.users()
84​
85 return html`
86 <ul>
87 ${userQueries.map((query, index) => {
88 if (query.isPending) return html`<li>Loading...</li>`
89 if (query.isError) return html`<li>Error loading user</li>`
90​
91 return html`<li>${this.userIds[index]}: ${query.data.name}</li>`
92 })}
93 </ul>
94 `
95 }
97```
98​
99The order of the results matches the order of the input queries.
100​
101## Combining Results
102​
103Use `combine` when a component wants one derived value instead of an array of query results:
104​
105```ts
106private readonly dashboard = createQueriesController(this, {
107 queries: [
108 { queryKey: ['stats'], queryFn: fetchStats },
109 { queryKey: ['projects'], queryFn: fetchProjects },
110 ],
111 combine: ([stats, projects]) => ({
112 activeUsers: stats.data?.activeUsers ?? 0,
113 projects: projects.data ?? [],
114 isPending: stats.isPending || projects.isPending,
115 isError: stats.isError || projects.isError,
116 }),
117})
118```
119​
120```ts
121render() {
122 const dashboard = this.dashboard()
123​
124 if (dashboard.isPending) return html`Loading...`
125 if (dashboard.isError) return html`Unable to load dashboard`
126​
127 return html`
128 <p>Total projects: ${dashboard.projects.length}</p>
129 <p>Active users: ${dashboard.activeUsers}</p>
130 `
132```
133​
134Having the same query key more than once in the `queries` array can cause those entries to share cached data. Deduplicate repeated keys first if each rendered row needs independent query state.