Query Docs
Guides & Concepts
- Important Defaults
- Queries
- Query Keys
- Query Functions
- Network Mode
- Parallel Queries
- Dependent Queries
- Background Fetching Indicators
- Window Focus Refetching
- Disabling/Pausing Queries
- Query Retries
- Paginated Queries
- Infinite Queries
- Initial Query Data
- Placeholder Query Data
- Prefetching
- Mutations
- Query Invalidation
- Invalidation from Mutations
- Updates from Mutation
- Optimistic Updates
- Query Cancellation
- Scroll Restoration
- Filters
- SSR & Nuxt
- Caching
- Default Query Fn
- Suspense
- Custom Logger
- Custom Client
- Does this replace [Vuex, Pinia]?
AI/LLM: This documentation page is available in plain markdown format at/query/v4/docs/framework/vue/guides/dependent-queries.md
Guides & Concepts
Dependent Queries
useQuery dependent Query#
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:
js
// Get the user
const { data: user } = useQuery({
queryKey: ['user', email],
queryFn: () => getUserByEmail(email.value),
})
const userId = computed(() => user.value?.id)
const enabled = computed(() => !!user.value?.id)
// Then get the user's projects
const { isIdle, data: projects } = useQuery({
queryKey: ['projects', userId],
queryFn: () => getProjectsByUser(userId.value),
enabled, // The query will not execute until `enabled == true`
})The projects query will start in:
tsx
status: 'loading'
fetchStatus: 'idle'As soon as the user is available, the projects query will be enabled and will then transition to:
tsx
status: 'loading'
fetchStatus: 'fetching'Once we have the projects, it will go to:
tsx
status: 'success'
fetchStatus: 'idle'useQueries dependent Query#
Dynamic parallel query - useQueries can depend on a previous query also, here's how to achieve this:
tsx
// Get the users ids
const { data: userIds } = useQuery({
queryKey: ['users'],
queryFn: getUsersData,
select: users => users.map(user => user.id),
})
// Then get the users messages
const usersMessages = useQueries({
queries: userIds
? userIds.map(id => {
return {
queryKey: ['messages', id],
queryFn: () => getMessagesByUsers(id),
};
})
: [], // if users is undefined, an empty array will be returned
})Note that useQueries return an array of query results