What is GraphQL Mutation?
TL;DR
Write operation in GraphQL for creating, updating, or deleting data with type-safe inputs and return values.
GraphQL mutations are write operations that modify data on the server. Unlike queries which only read data, mutations create, update, or delete records.
Example mutation:
mutation CreateProduct($input: ProductInput!) {
createProduct(input: $input) {
id
name
price
createdAt
}
}
Mutations often use urql client with automatic type generation from your GraphQL schema. The mutation structure includes an input type defining the shape of data being sent, and a return type specifying what data comes back after the operation completes.
Key Benefits:
- Type safety: TypeScript types are generated from your schema, catching errors before runtime
- Optimistic updates: urql supports optimistic UI updates for instant user feedback
- Cache management: Automatic cache invalidation keeps your UI in sync with server state
- Error handling: Structured error responses with field-level validation messages
AI-Friendly: GraphQL mutations provide a clear contract for data modification. AI tools can read your schema to understand exactly what fields are required, what types they expect, and what the mutation returns. This makes it easy for AI to generate correct mutation code, suggest proper input validation, and handle error cases appropriately.