Vue Query Overview
Vue Query provides hooks for fetching, caching, and updating asynchronous data in Vue applications. It’s built on top of TanStack Query’s framework-agnostic core and provides a Vue-specific API with full support for Vue’s reactivity system.Key Features
Vue Query brings powerful data management capabilities to your Vue applications:- Transport/Protocol Agnostic: Works with REST, GraphQL, promises, or any async data source
- Automatic Caching: Smart caching with configurable stale-while-revalidate strategies
- Auto Refetching: Refetch on window focus, network reconnect, polling, or custom intervals
- Parallel & Dependent Queries: Execute multiple queries simultaneously or chain them together
- Mutations with Optimistic Updates: Update data with automatic query invalidation and rollback
- Infinite Scroll Support: Built-in support for paginated and cursor-based queries
- Request Cancellation: Automatic cancellation of outdated requests
- SSR Support: Server-side rendering with hydration/dehydration
- Vue DevTools Integration: Debug your queries with the official Vue DevTools plugin
- Vue 2 & 3 Support: Works with Vue 2.6+, 2.7, and Vue 3 via vue-demi
Vue Reactivity Integration
Vue Query is designed to work seamlessly with Vue’s reactivity system:Core Concepts
Queries
Queries are declarative dependencies on asynchronous data sources. UseuseQuery to fetch and cache data:
Mutations
Mutations are used to create, update, or delete data:Query Invalidation
Invalidate queries to mark them as stale and trigger refetching:Architecture
import { createApp } from 'vue'
import { VueQueryPlugin } from '@tanstack/vue-query'
import App from './App.vue'
const app = createApp(App)
app.use(VueQueryPlugin)
app.mount('#app')
<script setup>
import { useQuery } from '@tanstack/vue-query'
const { data } = useQuery({
queryKey: ['todos'],
queryFn: fetchTodos,
})
</script>
Comparison with Other Solutions
vs Pinia/Vuex
Vue Query is specifically designed for asynchronous server state, while Pinia and Vuex excel at client-side state management. They solve different problems:| Feature | Vue Query | Pinia/Vuex |
|---|---|---|
| Server State | ✅ Automatic caching, refetching, synchronization | ❌ Manual implementation required |
| Client State | ❌ Use refs/reactive | ✅ Perfect for UI state, preferences |
| Caching | ✅ Built-in with TTL | ❌ Manual |
| Loading States | ✅ Automatic | ❌ Manual |
| Optimistic Updates | ✅ Built-in | ❌ Manual |
| Devtools | ✅ Dedicated devtools | ✅ Vue DevTools |
vs Apollo Client
Vue Query is protocol-agnostic while Apollo is GraphQL-specific:- Vue Query: Works with any async data source (REST, GraphQL, etc.)
- Apollo Client: Deeply integrated with GraphQL, includes code generation
If you’re exclusively using GraphQL, Apollo provides GraphQL-specific features like fragments and schema integration. For REST or mixed APIs, Vue Query is more flexible.
Vue 2 Support
Vue Query supports Vue 2.6+ through thevue-demi compatibility layer:
Framework Features
Vue Query leverages Vue-specific features:Shallow Refs
Improve performance for large datasets with shallow reactivity:Scope Disposal
Queries automatically clean up when the component unmounts:Vue Query automatically handles cleanup using
onScopeDispose, so you rarely need to manage it manually.TypeScript Support
Vue Query is written in TypeScript and provides full type safety:Next Steps
Installation
Install Vue Query and set up the plugin
Quick Start
Build your first query in minutes
TypeScript
Learn about type safety and inference
DevTools
Debug queries with Vue DevTools integration