GraphQL Mutations, With Working Examples
A GraphQL mutation is the operation you send to change data. It starts with the mutation keyword, calls a field on the schema's Mutation type, passes arguments, usually one input object held in a $variable, and selects which fields of the result to return. Top-level mutation fields run one after another, never in parallel.
| Part | Example | What it does |
|---|---|---|
| Keyword and name | mutation AddTask | Marks the operation as a write and names it for logs |
| Variable definitions | ($input: TaskInput!) | Declares typed values sent separately as JSON |
| Root field | addTask(input: $input) | A field defined on the Mutation type |
| Selection set | { id title done } | Fields of the result to send back |
| Variables JSON | {"input": {...}} | The actual values, kept out of the query string |
Updated
Use the builder below to see a full mutation for a small task list: the operation, the variables JSON your code would send with it, and a typical response. Type a task title and watch the variables update, so you can copy a request that is ready to run against a matching schema.
Mutation builder
- Create
- Update
- Delete
The input type groups the new task's fields. The selection set asks for the id the server assigned.
mutation AddTask($input: TaskInput!) {
addTask(input: $input) {
id
title
done
}
}{
"input": {
"title": "Write the launch post",
"done": false
}
}{
"data": {
"addTask": { "id": "t_101", "title": "Write the launch post", "done": false }
}
}The schema behind the examples
Mutation type and input types
Input types use the input keyword. TaskPatch has no required fields, so an update can send only what changed.
type Task {
id: ID!
title: String!
done: Boolean!
}
input TaskInput {
title: String!
done: Boolean = false
}
input TaskPatch {
title: String
done: Boolean
}
type Mutation {
addTask(input: TaskInput!): Task!
updateTask(id: ID!, input: TaskPatch!): Task
deleteTask(id: ID!): ID
}Sending it from JavaScript
POST the mutation with fetch
The document and the variables travel as two keys of one JSON body. Check errors even when the status is 200.
const ADD_TASK = `mutation AddTask($input: TaskInput!) {
addTask(input: $input) { id title done }
}`;
async function addTask(title) {
const res = await fetch("/graphql", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: ADD_TASK, variables: { input: { title } } }),
});
const { data, errors } = await res.json();
if (errors?.length) throw new Error(errors[0].message);
return data.addTask;
}Two writes in one operation
Aliases let the same field run twice. The second runs only after the first finishes, but a failure in it does not undo the first.
mutation TwoTasks {
first: addTask(input: { title: "Draft" }) { id }
second: addTask(input: { title: "Review" }) { id }
}
Anatomy of a mutation
On the server, the schema declares a Mutation type whose fields are the writes clients may call, each with arguments and a return type. On the client, the operation names one or more of those fields, passes arguments and picks the result fields it wants back.
If the field returns an object type, a selection set is required, just like in a query. Asking for the new id and any fields the screen shows lets the client update its view straight from the response.
Variables and input objects
Variables are declared after the operation name with a dollar sign and a type, such as $input: TaskInput!. The exclamation mark makes the variable required. The values travel in a separate variables object in the request body.
An input object type is declared with the input keyword instead of type. Its fields can only be scalars, enums, lists or other input types, which keeps the shape of what clients send simple to validate.
Errors from a write
If the document is invalid, for example a misspelled field or a missing required variable, the server answers with errors and no data, and nothing is written.
If the write itself fails during execution, the field comes back null and an errors entry carries a message and a path. Many schemas also return expected problems, such as a title that is too long, as fields of a payload type so the client can show them next to the form.
How it works
- Choose create, update or delete.
- Type a title for the task; the variables JSON updates as you type.
- Copy the mutation and the variables, and send them as the query and variables keys of one POST.
- Compare your server's reply with the sample response shown below them.
Tips
- Send values through variables rather than pasting them into the query string. It avoids escaping bugs and lets servers cache the parsed document.
- Return the changed object, including its id, so client caches can update the screen without a second request.
- Name every operation. Named mutations are far easier to find in server logs and error reports.
- Treat several root fields in one mutation as separate steps, not a transaction: an earlier field stays done when a later one fails.
Frequently asked questions
What is the difference between a query and a mutation?
Both use the same syntax and both can return data. By convention queries only read, and mutations write. The execution rule differs too: query fields may resolve in parallel, while top-level mutation fields run in series.
Why use an input type instead of plain arguments?
An input object groups related values into one typed argument, so the mutation signature stays short and you can add optional fields later without changing every caller.
Can I send a mutation with a GET request?
No. The GraphQL over HTTP guidance lets servers accept GET for query operations only. Mutations go as POST, which also keeps writes away from browser and CDN caches.
Is a mutation with several fields atomic?
No. graphql.org notes that serial execution is not a database transaction. If the second field fails, the first one's change stays, and the response holds data for one and an error for the other.
What should a delete mutation return?
The spec leaves that to you, but the field still needs an output type. Common choices are the deleted object's id, the removed object itself, or a payload type with the id and a success flag.
Related: GraphQL vs REST, GraphQL fragments, GraphQL subscriptions, JavaScript async and fetch.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- GraphQL Subscriptions, Step by Step
- GraphQL vs REST: How They Really Differ
- Hard Bounce vs Soft Bounce
- Headless CMS Compared: Six Options, One Blog
- High Contrast Color Palettes, With Ratios Checked
- Build a Home Care Website: Pages, Costs and a Working Example | How-to Guide
- Hosts File Location: Find and Edit It on Any System
- How Accountants Get More Clients from Google and ChatGPT | How-to Guide
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App