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

request-waterfalls.md

209 lines4.9 KB

1---
2id: request-waterfalls
3title: Performance & Request Waterfalls
4ref: docs/framework/react/guides/request-waterfalls.md
5replace: { 'React Query': 'Solid Query' }
6---
7​
8[//]: # 'AdvancedSSRLink'
9[//]: # 'AdvancedSSRLink'
10[//]: # 'DependentExample'
11​
12```tsx
13// Get the user
14const userQuery = useQuery(() => ({
15 queryKey: ['user', email],
16 queryFn: getUserByEmail,
17}))
18​
19const userId = () => userQuery.data?.id
20​
21// Then get the user's projects
22const projectsQuery = useQuery(() => ({
23 queryKey: ['projects', userId()],
24 queryFn: getProjectsByUser,
25 // The query will not execute until the userId exists
26 enabled: !!userId(),
27}))
28```
29​
30[//]: # 'DependentExample'
31[//]: # 'ServerComponentsNote1'
32[//]: # 'ServerComponentsNote1'
33[//]: # 'SuspenseSerial'
34[//]: # 'SuspenseSerial'
35[//]: # 'NestedIntro'
36​
37Nested Component Waterfalls is when both a parent and a child component contains queries, and the parent does not render the child until its query is done.
38​
39[//]: # 'NestedIntro'
40[//]: # 'NestedExample'
41​
42```tsx
43import { Switch, Match } from 'solid-js'
44​
45function Article(props) {
46 const articleQuery = useQuery(() => ({
47 queryKey: ['article', props.id],
48 queryFn: getArticleById,
49 }))
50​
51 return (
52 <Switch>
53 <Match when={articleQuery.isPending}>
54 Loading article...
55 </Match>
56 <Match when={articleQuery.isSuccess}>
57 <ArticleHeader articleData={articleQuery.data} />
58 <ArticleBody articleData={articleQuery.data} />
59 <Comments id={props.id} />
60 </Match>
61 </Switch>
62 )
64​
65function Comments(props) {
66 const commentsQuery = useQuery(() => ({
67 queryKey: ['article-comments', props.id],
68 queryFn: getArticleCommentsById,
69 }))
70​
71 ...
73```
74​
75[//]: # 'NestedExample'
76[//]: # 'NestedHoistedExample'
77​
78```tsx
79import { Switch, Match, Show } from 'solid-js'
80​
81function Article(props) {
82 const articleQuery = useQuery(() => ({
83 queryKey: ['article', props.id],
84 queryFn: getArticleById,
85 }))
86​
87 const commentsQuery = useQuery(() => ({
88 queryKey: ['article-comments', props.id],
89 queryFn: getArticleCommentsById,
90 }))
91​
92 return (
93 <Switch>
94 <Match when={articleQuery.isPending}>Loading article...</Match>
95 <Match when={articleQuery.isSuccess}>
96 <ArticleHeader articleData={articleQuery.data} />
97 <ArticleBody articleData={articleQuery.data} />
98 <Switch>
99 <Match when={commentsQuery.isPending}>Loading comments...</Match>
100 <Match when={commentsQuery.isSuccess}>
101 <Comments commentsData={commentsQuery.data} />
102 </Match>
103 </Switch>
104 </Match>
105 </Switch>
106 )
108```
109​
110[//]: # 'NestedHoistedExample'
111[//]: # 'NestedHoistedOutro'
112​
113The two queries will now fetch in parallel.
114​
115[//]: # 'NestedHoistedOutro'
116[//]: # 'DependentNestedExample'
117​
118```tsx
119import { Switch, Match, For } from 'solid-js'
120​
121function Feed() {
122 const feedQuery = useQuery(() => ({
123 queryKey: ['feed'],
124 queryFn: getFeed,
125 }))
126​
127 return (
128 <Switch>
129 <Match when={feedQuery.isPending}>
130 Loading feed...
131 </Match>
132 <Match when={feedQuery.isSuccess}>
133 <For each={feedQuery.data}>
134 {(feedItem) => {
135 if (feedItem.type === 'GRAPH') {
136 return <GraphFeedItem feedItem={feedItem} />
137 }
138 return <StandardFeedItem feedItem={feedItem} />
139 }}
140 </For>
141 </Match>
142 </Switch>
143 )
145​
146function GraphFeedItem(props) {
147 const graphQuery = useQuery(() => ({
148 queryKey: ['graph', props.feedItem.id],
149 queryFn: getGraphDataById,
150 }))
151​
152 ...
154```
155​
156[//]: # 'DependentNestedExample'
157[//]: # 'ServerComponentsNote2'
158​
159In this example, we can't trivially flatten the waterfall by just hoisting the query to the parent, or even adding prefetching. Just like the dependent query example at the beginning of this guide, one option is to refactor our API to include the graph data in the `getFeed` query.
160​
161[//]: # 'ServerComponentsNote2'
162[//]: # 'LazyExample'
163​
164```tsx
165import { lazy, Switch, Match, For } from 'solid-js'
166​
167// This lazy loads the GraphFeedItem component, meaning
168// it wont start loading until something renders it
169const GraphFeedItem = lazy(() => import('./GraphFeedItem'))
170​
171function Feed() {
172 const feedQuery = useQuery(() => ({
173 queryKey: ['feed'],
174 queryFn: getFeed,
175 }))
176​
177 return (
178 <Switch>
179 <Match when={feedQuery.isPending}>
180 Loading feed...
181 </Match>
182 <Match when={feedQuery.isSuccess}>
183 <For each={feedQuery.data}>
184 {(feedItem) => {
185 if (feedItem.type === 'GRAPH') {
186 return <GraphFeedItem feedItem={feedItem} />
187 }
188 return <StandardFeedItem feedItem={feedItem} />
189 }}
190 </For>
191 </Match>
192 </Switch>
193 )
195​
196// GraphFeedItem.tsx
197function GraphFeedItem(props) {
198 const graphQuery = useQuery(() => ({
199 queryKey: ['graph', props.feedItem.id],
200 queryFn: getGraphDataById,
201 }))
202​
203 ...
205```
206​
207[//]: # 'LazyExample'
208[//]: # 'ServerComponentsNote3'
209[//]: # 'ServerComponentsNote3'