GraphQL Mutations
Expert guidance for designing effective GraphQL mutations.
Quick Reference
| Pattern |
Use When |
Structure |
| Result payload |
All mutations |
mutationName(input): MutationNamePayload! |
| Field-specific errors |
Validation failures |
errors: [FieldError!]! in payload |
| Input objects |
Complex arguments |
input: MutationNameInput! |
| Noun + Verb naming |
State changes |
createUser, deletePost, closeCard |
| Idempotent mutations |
Safe retries |
Design for repeatable calls |
| Optimistic UI |
Client-side updates |
Return predicted result |
What Do You Need?
- Payload design - Return types, error handling
- Input objects - Structuring mutation arguments
- Error patterns - Field-specific vs top-level errors
- Naming - Mutation naming conventions
- Side effects - Handling async operations
Specify a number or describe your mutation scenario.
Routing
| Response |
Reference to Read |
| 1, "payload", "return", "response" |
payloads.md |
| 2, "input", "argument", "parameter" |
inputs.md |
| 3, "error", "validation", "field error" |
errors.md |
| 4, "naming", "convention" |
naming.md |
| 5, general mutations |
Read relevant references |
Critical Rules
- Always return a payload: Never just a boolean or the object
- Use input objects for complex arguments: Don't use many scalars
- Field-specific errors in response: Let clients handle per-field failures
- Noun + verb naming: createUser, deleteUser, not user
- Mutations are POST-only: Never use GET for mutations
- Design for idempotency: Safe to call multiple times
Mutation Template
# Input object for complex arguments
input CreateUserInput {
name: String!
email: String!
password: String!
}
# Payload with result and errors
type CreateUserPayload {
user: User
errors: [UserError!]!
}
# Field-specific error type
type UserError {
field: [String!]! # Path to field: ["email"] or ["user", "emails", 0]
message: String!
}
# Mutation definition
type Mutation {
"""
Creates a new user account
"""
createUser(input: CreateUserInput!): CreateUserPayload!
}
Mutation Implementation
// Good: Mutation with proper payload and field errors
func (r *mutationResolver) CreateUser(ctx context.Context, input CreateUserInput) (*CreateUserPayload, error) {
// Validate
var errs []UserError
if input.Name == "" {
errs = append(errs, UserError{
Field: []string{"name"},
Message: "Name is required",
})
}
if !isValidEmail(input.Email) {
errs = append(errs, UserError{
Field: []string{"email"},
Message: "Invalid email format",
})
}
if len(errs) > 0 {
return &CreateUserPayload{Errors: errs}, nil
}
// Create
user, err := r.db.CreateUser(input)
if err != nil {
if errors.Is(err, db.ErrDuplicate) {
return &CreateUserPayload{
Errors: []UserError{{
Field: []string{"email"},
Message: "Email already exists",
}},
}, nil
}
return nil, fmt.Errorf("failed to create user")
}
return &CreateUserPayload{User: user, Errors: []UserError{}}, nil
}
Common Mutation Patterns
Create
type Mutation {
createUser(input: CreateUserInput!): CreateUserPayload!
}
type CreateUserPayload {
user: User
errors: [UserError!]!
}
Update
type Mutation {
updateUser(id: ID!, input: UpdateUserInput!): UpdateUserPayload!
}
type UpdateUserPayload {
user: User
errors: [UserError!]!
}
Delete
type Mutation {
deleteUser(id: ID!): DeleteUserPayload!
}
type DeleteUserPayload {
deletedUserId: ID
errors: [UserError!]!
}
State Change (Noun + Verb)
type Mutation {
"""
Closes a card (marks as closed, not deleted)
"""
closeCard(id: ID!): CloseCardPayload!
}
type CloseCardPayload {
card: Card
errors: [UserError!]!
}
Error Handling Patterns
| Error Type |
Response Pattern |
| Validation errors |
Return in payload errors field |
| Duplicate unique key |
Return in payload errors field |
| Not found |
Return in payload errors field |
| Permission denied |
Return in payload errors field |
| Internal server error |
Return nil, wrap error (don't expose) |
HTTP Semantics
| Concern |
Guidance |
| HTTP method |
Always POST for mutations |
| Caching |
Mutations are never cached |
| Idempotency |
Design mutations to be safely repeatable |
| Side effects |
Document non-obvious side effects |
| Async operations |
Return payload with job ID, query for status |
Common Mutation Mistakes
| Mistake |
Severity |
Fix |
| Returning just boolean |
Medium |
Use payload with result |
| No field-specific errors |
High |
Add errors array to payload |
| Too many scalar arguments |
Medium |
Use input object |
| Verb + noun naming |
Low |
Use noun + verb (createUser) |
| Using GET for mutations |
Critical |
Always use POST |
| No validation errors in payload |
High |
Return validation failures |
Reference Index
| File |
Topics |
| payloads.md |
Result types, error patterns, response structure |
| inputs.md |
Input objects, nested inputs, validation |
| errors.md |
Field errors, error types, client handling |
| naming.md |
Conventions, verb selection, consistency |
Success Criteria
Mutations are well-designed when:
- All mutations return a payload type
- Field-specific errors returned in payload
- Input objects used for complex arguments
- Noun + verb naming (createUser, deletePost)
- POST only (never GET)
- Idempotent where possible
- Validation errors returned, not thrown
- No internal errors exposed to clients
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: graphql-mutations3description: GraphQL mutation design including payload patterns, field-specific errors, input objects, and HTTP semantics. Use when designing or implementing GraphQL mutations. Use when this capability is needed.4---56# GraphQL Mutations78Expert guidance for designing effective GraphQL mutations.910## Quick Reference1112| Pattern | Use When | Structure |13|---------|----------|-----------|14| Result payload | All mutations | `mutationName(input): MutationNamePayload!` |15| Field-specific errors | Validation failures | `errors: [FieldError!]!` in payload |16| Input objects | Complex arguments | `input: MutationNameInput!` |17| Noun + Verb naming | State changes | `createUser`, `deletePost`, `closeCard` |18| Idempotent mutations | Safe retries | Design for repeatable calls |19| Optimistic UI | Client-side updates | Return predicted result |2021## What Do You Need?22231. **Payload design** - Return types, error handling242. **Input objects** - Structuring mutation arguments253. **Error patterns** - Field-specific vs top-level errors264. **Naming** - Mutation naming conventions275. **Side effects** - Handling async operations2829Specify a number or describe your mutation scenario.3031## Routing3233| Response | Reference to Read |34|----------|-------------------|35| 1, "payload", "return", "response" | [payloads.md](./references/payloads.md) |36| 2, "input", "argument", "parameter" | [inputs.md](./references/inputs.md) |37| 3, "error", "validation", "field error" | [errors.md](./references/errors.md) |38| 4, "naming", "convention" | [naming.md](./references/naming.md) |39| 5, general mutations | Read relevant references |4041## Critical Rules4243- **Always return a payload**: Never just a boolean or the object44- **Use input objects for complex arguments**: Don't use many scalars45- **Field-specific errors in response**: Let clients handle per-field failures46- **Noun + verb naming**: createUser, deleteUser, not user47- **Mutations are POST-only**: Never use GET for mutations48- **Design for idempotency**: Safe to call multiple times4950## Mutation Template5152```graphql53# Input object for complex arguments54input CreateUserInput {55 name: String!56 email: String!57 password: String!58}5960# Payload with result and errors61type CreateUserPayload {62 user: User63 errors: [UserError!]!64}6566# Field-specific error type67type UserError {68 field: [String!]! # Path to field: ["email"] or ["user", "emails", 0]69 message: String!70}7172# Mutation definition73type Mutation {74 """75 Creates a new user account76 """77 createUser(input: CreateUserInput!): CreateUserPayload!78}79```8081## Mutation Implementation8283```go84// Good: Mutation with proper payload and field errors85func (r *mutationResolver) CreateUser(ctx context.Context, input CreateUserInput) (*CreateUserPayload, error) {86 // Validate87 var errs []UserError88 if input.Name == "" {89 errs = append(errs, UserError{90 Field: []string{"name"},91 Message: "Name is required",92 })93 }94 if !isValidEmail(input.Email) {95 errs = append(errs, UserError{96 Field: []string{"email"},97 Message: "Invalid email format",98 })99 }100 if len(errs) > 0 {101 return &CreateUserPayload{Errors: errs}, nil102 }103104 // Create105 user, err := r.db.CreateUser(input)106 if err != nil {107 if errors.Is(err, db.ErrDuplicate) {108 return &CreateUserPayload{109 Errors: []UserError{{110 Field: []string{"email"},111 Message: "Email already exists",112 }},113 }, nil114 }115 return nil, fmt.Errorf("failed to create user")116 }117118 return &CreateUserPayload{User: user, Errors: []UserError{}}, nil119}120```121122## Common Mutation Patterns123124### Create125```graphql126type Mutation {127 createUser(input: CreateUserInput!): CreateUserPayload!128}129130type CreateUserPayload {131 user: User132 errors: [UserError!]!133}134```135136### Update137```graphql138type Mutation {139 updateUser(id: ID!, input: UpdateUserInput!): UpdateUserPayload!140}141142type UpdateUserPayload {143 user: User144 errors: [UserError!]!145}146```147148### Delete149```graphql150type Mutation {151 deleteUser(id: ID!): DeleteUserPayload!152}153154type DeleteUserPayload {155 deletedUserId: ID156 errors: [UserError!]!157}158```159160### State Change (Noun + Verb)161```graphql162type Mutation {163 """164 Closes a card (marks as closed, not deleted)165 """166 closeCard(id: ID!): CloseCardPayload!167}168169type CloseCardPayload {170 card: Card171 errors: [UserError!]!172}173```174175## Error Handling Patterns176177| Error Type | Response Pattern |178|------------|------------------|179| Validation errors | Return in payload errors field |180| Duplicate unique key | Return in payload errors field |181| Not found | Return in payload errors field |182| Permission denied | Return in payload errors field |183| Internal server error | Return nil, wrap error (don't expose) |184185## HTTP Semantics186187| Concern | Guidance |188|---------|----------|189| HTTP method | Always POST for mutations |190| Caching | Mutations are never cached |191| Idempotency | Design mutations to be safely repeatable |192| Side effects | Document non-obvious side effects |193| Async operations | Return payload with job ID, query for status |194195## Common Mutation Mistakes196197| Mistake | Severity | Fix |198|---------|----------|-----|199| Returning just boolean | Medium | Use payload with result |200| No field-specific errors | High | Add errors array to payload |201| Too many scalar arguments | Medium | Use input object |202| Verb + noun naming | Low | Use noun + verb (createUser) |203| Using GET for mutations | Critical | Always use POST |204| No validation errors in payload | High | Return validation failures |205206## Reference Index207208| File | Topics |209|------|--------|210| [payloads.md](./references/payloads.md) | Result types, error patterns, response structure |211| [inputs.md](./references/inputs.md) | Input objects, nested inputs, validation |212| [errors.md](./references/errors.md) | Field errors, error types, client handling |213| [naming.md](./references/naming.md) | Conventions, verb selection, consistency |214215## Success Criteria216217Mutations are well-designed when:218- All mutations return a payload type219- Field-specific errors returned in payload220- Input objects used for complex arguments221- Noun + verb naming (createUser, deletePost)222- POST only (never GET)223- Idempotent where possible224- Validation errors returned, not thrown225- No internal errors exposed to clients226227---228> Converted and distributed by [TomeVault](https://tomevault.io/claim/jovermier) — claim your Tome and manage your conversions.229<!-- tomevault:4.0:skill_md:2026-04-13 -->