Skip to content

Commit 1f13a76

Browse files
authored
Merge pull request #17 from nejcrogelsek/feature/15-dependent-queries
dependent queries
2 parents 4e360c8 + 78c3b07 commit 1f13a76

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ We can query by id in two ways (look commits under branch name: `feature/12-quer
9292

9393
Sometimes a single component needs to call multiple APIs to fetch the necessary data. With react-query with simply call useQuery twice.
9494

95+
**10. react-query - Dependent queries**
96+
97+
Dependent query - is dependent on the results of another query.
98+
9599
## Available Scripts
96100

97101
In the project directory, you can run:

db.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,17 @@
2929
"id": 3,
3030
"name": "Rachel"
3131
}
32+
],
33+
"users": [
34+
{
35+
"id": "test@gmail.com",
36+
"channelId": "codevolution"
37+
}
38+
],
39+
"channels": [
40+
{
41+
"id": "codevolution",
42+
"courses": ["react", "vue", "angular"]
43+
}
3244
]
3345
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import axios from 'axios'
2+
import { FC } from 'react'
3+
import { useQuery } from 'react-query'
4+
5+
interface Props {
6+
email: string
7+
}
8+
9+
const fetchUserByEmail = (email: string) => {
10+
return axios.get(`http://localhost:4000/users/${email}`)
11+
}
12+
13+
const fetchCoursesByChannelId = (channelId: string) => {
14+
return axios.get(`http://localhost:4000/channels/${channelId}`)
15+
}
16+
17+
const DependentQueries: FC<Props> = ({ email }: Props) => {
18+
const { data: user } = useQuery(['user', email], () => fetchUserByEmail(email))
19+
const channelId = user?.data.channelId
20+
const { data: channel } = useQuery(['courses', channelId], () => fetchCoursesByChannelId(channelId), {
21+
enabled: !!channelId,
22+
})
23+
return (
24+
<div className="dependent-queries">
25+
<h1>DependentQueries</h1>
26+
{channel?.data.courses.map((course: string, index: number) => (
27+
<div key={index}>{course}</div>
28+
))}
29+
</div>
30+
)
31+
}
32+
33+
export default DependentQueries

src/routes/Routes.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import RQSuperHeroes from 'pages/RQSuperHeroes/RQSuperHeroes'
88
import RQSuperHero from 'pages/RQSuperHero/RQSuperHero'
99
import ParallelQueries from 'pages/ParallelQueries/ParallelQueries'
1010
import DynamicParallelQueries from 'pages/DynamicParallelQueries/DynamicParallelQueries'
11+
import DependentQueries from 'pages/DependentQueries/DependentQueries'
1112

1213
export enum RouteType {
1314
PUBLIC,
@@ -51,6 +52,11 @@ export const AppRoutes: AppRoute[] = [
5152
path: '/dynamic-parallel-queries',
5253
children: <DynamicParallelQueries heroIds={[1, 3]} />,
5354
},
55+
{
56+
type: RouteType.PUBLIC,
57+
path: '/dependent-queries',
58+
children: <DependentQueries email="test@gmail.com" />,
59+
},
5460
]
5561

5662
const Routes: FC = () => {

0 commit comments

Comments
 (0)