Skip to content

Commit d62e7e6

Browse files
committed
docs: add parallel and dependant queries docs
1 parent 595e153 commit d62e7e6

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

docs/_sidebar.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@
2121
- [Queries](guides/queries.md)
2222
- [Query Keys](guides/query-keys.md)
2323
- [Query Functions](guides/query-functions.md)
24+
- [Parallel Queries](guides/parallel-queries.md)
25+
- [Dependent Queries](guides/dependent-queries.md)
2426
- [Custom clients (experimental)](guides/custom-clients.md)
2527
- [SSR & Nuxt.js (experimental)](guides/ssr.md)

docs/guides/dependent-queries.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Dependent (or serial) queries depend on previous ones to finish before they can execute. To achieve this, it's as easy as using the `enabled` option to tell a query when it is ready to run:
2+
3+
```js
4+
// Get the user
5+
const { data: user } = useQuery(["user", email], getUserByEmail);
6+
7+
const userId = computed(() => user.value?.id);
8+
const enabled = computed(() => !!user.value?.id);
9+
10+
// Then get the user's projects
11+
const { isIdle, data: projects } = useQuery(
12+
reactive(["projects", { userId }]),
13+
() => getProjectsByUser(userId.value),
14+
reactive({
15+
// The query will not execute until the userId exists
16+
enabled,
17+
})
18+
);
19+
20+
// isIdle will be `true` until `enabled` is true and the query begins to fetch.
21+
// It will then go to the `isLoading` stage and hopefully the `isSuccess` stage :)
22+
```

docs/guides/parallel-queries.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"Parallel" queries are queries that are executed in parallel, or at the same time so as to maximize fetching concurrency.
2+
3+
## Manual Parallel Queries
4+
5+
When the number of parallel queries does not change, there is **no extra effort** to use parallel queries. Just use any number of Vue Query's `useQuery` and `useInfiniteQuery` hooks side-by-side!
6+
7+
```js
8+
// The following queries will execute in parallel
9+
const usersQuery = useQuery('users', fetchUsers)
10+
const teamsQuery = useQuery('teams', fetchTeams)
11+
const projectsQuery = useQuery('projects', fetchProjects)
12+
...
13+
```
14+
15+
> When using Vue Query in suspense mode, this pattern of parallelism does not work, since the first query would suspend the setup function before the other queries run. To get around this, you'll either need to use the `useQueries` hook (which is suggested) or orchestrate your own parallelism with separate components for each `useQuery` instance (which is lame).
16+
17+
## Dynamic Parallel Queries with `useQueries`
18+
19+
If the number of queries you need to execute is changing from render to render, you cannot use manual querying since that would violate the rules of hooks. Instead, Vue Query provides a `useQueries` hook, which you can use to dynamically execute as many queries in parallel as you'd like.
20+
21+
`useQueries` accepts an **array of query options objects** and returns an **array of query results**:
22+
23+
```js
24+
const users = computed(...)
25+
const userQueries = useQueries(
26+
users.value.map(user => {
27+
return {
28+
queryKey: ['user', {userId: user.id}],
29+
queryFn: () => fetchUserById(user.id),
30+
}
31+
})
32+
)
33+
```

docs/guides/queries.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ The **unique key** you provide is used internally for refetching, caching, and s
1212
The query results returned by `useQuery` contains all of the information about the query that you'll need for templating and any other usage of the data:
1313

1414
```js
15-
import { useQuery } from "vue-query";
16-
17-
function App() {
18-
const result = useQuery("todos", fetchTodoList);
19-
}
15+
const result = useQuery("todos", fetchTodoList);
2016
```
2117

2218
The `result` object contains a few very important states you'll need to be aware of to be productive. A query can only be in one of the following states at any given moment:
@@ -71,10 +67,7 @@ import { useQuery } from "vue-query";
7167
export default defineComponent({
7268
name: "Todo",
7369
setup(props) {
74-
const { status, data, error } = useQuery(
75-
"todos",
76-
fetchTodoList
77-
);
70+
const { status, data, error } = useQuery("todos", fetchTodoList);
7871
return { status, data, error };
7972
},
8073
});
@@ -88,4 +81,4 @@ export default defineComponent({
8881
<li v-for="todo in data" :key="todo.id">{{ todo.title }}</li>
8982
</ul>
9083
</template>
91-
```
84+
```

0 commit comments

Comments
 (0)