useQuery for Data Fetching
useQuery(QUERY), result, loading, error, variables, fetchPolicy options.
useQuery for Data Fetching is a free Vue Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Querying With useQuery
The useQuery composable runs a GraphQL query and returns reactive state. It automatically refetches when variables change and updates when the cache changes.
Basic Usage
Pass a gql document. useQuery returns result, loading, and error refs.
import { useQuery } from '@vue/apollo-composable'
import { gql } from '@apollo/client/core'
const { result, loading, error } = useQuery(gql(
'query { posts { id title } }'
))Reading the Result
result is a ref. Access the data via result.value — it holds the query response shaped exactly like the query.
const posts = computed(() => result.value?.posts ?? [])Loading and Error States
Use loading to show a spinner and error to show a message. Both are reactive refs that update as the request progresses.
<template>
<p v-if="loading">Loading...</p>
<p v-else-if="error">{{ error.message }}</p>
<ul v-else>
<li v-for="p in posts" :key="p.id">{{ p.title }}</li>
</ul>
</template>Query Variables
Pass variables as the second argument. The query re-runs whenever a variable value changes.
const { result } = useQuery(
gql('query($id: ID!) { post(id: $id) { title } }'),
{ id: 1 }
)Reactive Variables
For dynamic variables, pass a function returning the variables object, or a reactive ref. When a referenced ref changes, the query refetches automatically.
const id = ref(1)
const { result } = useQuery(
gql('query($id: ID!) { post(id: $id) { title } }'),
() => ({ id: id.value })
)The onResult Hook
onResult fires each time new data arrives — useful for side effects like analytics or syncing to another store.
const { onResult } = useQuery(QUERY)
onResult((res) => {
console.log('Got', res.data)
})Manual Refetch
useQuery returns a refetch function to re-run the query on demand — after an external change or a refresh button.
const { result, refetch } = useQuery(QUERY)
function reload() { refetch() }Pagination With fetchMore
fetchMore loads additional pages and merges them into the existing result using updateQuery — the basis of infinite scroll.
const { result, fetchMore } = useQuery(QUERY, { offset: 0 })
function loadMore() {
fetchMore({
variables: { offset: result.value.posts.length },
updateQuery: (prev, { fetchMoreResult }) => ({
posts: [...prev.posts, ...fetchMoreResult.posts]
})
})
}Fetch Policies
The fetchPolicy option controls cache use: cache-first (default) reads cache before network, network-only always hits the server, cache-and-network shows cache then refreshes.
useQuery(QUERY, null, {
fetchPolicy: 'cache-and-network'
})Conditional Queries
Use the enabled option to delay a query until a condition is met — for example, do not fetch until an id is selected.
const id = ref(null)
useQuery(QUERY, () => ({ id: id.value }), {
enabled: computed(() => id.value !== null)
})Quick Check
Test your knowledge of useQuery.
Recap
You learned useQuery:
- Returns reactive result, loading, and error.
- Read data via result.value.
- Pass variables as an object or a function for reactivity.
- refetch() re-runs, fetchMore() paginates, fetchPolicy controls cache use.
Frequently asked questions
Is the “useQuery for Data Fetching” lesson free?
Yes — the full text of “useQuery for Data Fetching” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “useQuery for Data Fetching”?
useQuery(QUERY), result, loading, error, variables, fetchPolicy options. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “useQuery for Data Fetching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Apollo Client Setup in Vue 3
- useQuery for Data Fetching
- useMutation for Data Changes
- Real-Time Subscriptions with useSubscription