useMutation for Data Changes
useMutation(MUTATION), mutate(), cache update strategies, optimistic responses.
useMutation for Data Changes is a free Vue Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.
Changing Data With Mutations
Queries read data; mutations change it — create, update, delete. The useMutation composable runs a GraphQL mutation and gives you a mutate function to call when ready.
Basic useMutation
Pass a mutation document. Unlike useQuery, it does not run immediately — it returns a mutate function you invoke on an event.
import { useMutation } from '@vue/apollo-composable'
import { gql } from '@apollo/client/core'
const { mutate } = useMutation(gql(
'mutation { addPost { id } }'
))Triggering the Mutation
Call mutate() from an event handler, such as a form submit. It returns a Promise resolving with the result.
async function submit() {
await mutate()
}Passing Variables
Pass an object of variables to mutate. The mutation document declares them with the $ syntax.
const { mutate } = useMutation(gql(
'mutation($title: String!) { addPost(title: $title) { id title } }'
))
function add(title) {
return mutate({ title })
}Loading and Error
useMutation also returns loading and error refs so you can disable the submit button and show failures.
<template>
<button :disabled="loading" @click="submit">Save</button>
<p v-if="error">{{ error.message }}</p>
</template>Reacting to Success
The onDone hook fires after a successful mutation — clear a form, show a toast, or navigate away.
const { mutate, onDone } = useMutation(ADD_POST)
onDone((res) => {
console.log('Created', res.data.addPost.id)
})Updating the Cache
After a mutation, the Apollo cache may be stale. The update function lets you modify the cache directly so the UI reflects the change without a refetch.
useMutation(ADD_POST, {
update(cache, { data }) {
const existing = cache.readQuery({ query: GET_POSTS })
cache.writeQuery({
query: GET_POSTS,
data: { posts: [...existing.posts, data.addPost] }
})
}
})Why update Beats Refetch
A cache update avoids an extra network round-trip. The new item appears instantly because you wrote it into the cache that every useQuery reads from.
Refetching Queries Instead
If manual cache editing is too fiddly, use refetchQueries to re-run specific queries after the mutation completes.
useMutation(ADD_POST, {
refetchQueries: [{ query: GET_POSTS }]
})Optimistic Responses
An optimisticResponse updates the UI before the server replies, assuming success. If the request fails, Apollo rolls the change back automatically.
mutate(
{ title: 'New' },
{
optimisticResponse: {
addPost: { __typename: 'Post', id: 'temp', title: 'New' }
}
}
)Why Optimistic UI Matters
Optimistic updates make the app feel instant — the user sees their action take effect immediately. Combined with update, the temporary item is replaced by the real server data when it arrives.
Quick Check
Test your knowledge of mutations.
Recap
You learned mutations:
- useMutation returns a mutate function you call on events.
- Pass variables to mutate(); use loading/error and onDone.
- The update function edits the cache after a mutation, avoiding a refetch.
- optimisticResponse updates the UI instantly with automatic rollback.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 42
- Lessons
- 156
Frequently Asked Questions
Is the “useMutation for Data Changes” lesson free?
Yes — any 3 lessons of the Vue Academy learning path, including “useMutation for Data Changes”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.
What will I learn in “useMutation for Data Changes”?
useMutation(MUTATION), mutate(), cache update strategies, optimistic responses. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “useMutation for Data Changes” 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