Mutations
Mutation mengubah data — create, update, delete. Seperti POST/PUT/DELETE di REST. Convention: mutations return objek yang diubah.
Create
mutation {
createUser(input: {
name: "Budi Santoso"
email: "[email protected]"
age: 28
}) {
id
name
email
}
}
# Response
{
"data": {
"createUser": {
"id": "456",
"name": "Budi Santoso",
"email": "[email protected]"
}
}
}
Update
mutation UpdateUser($id: ID!, $input: UpdateUserInput!) {
updateUser(id: $id, input: $input) {
id
name
email
}
}
# Variables
{
"id": "456",
"input": { "name": "Budi S." }
}
Delete
mutation {
deleteUser(id: "456")
}
# Returns: { "data": { "deleteUser": true } }
Mutation Response Pattern
# Return union type untuk success/error
type MutationResponse {
success: Boolean!
message: String
user: User
}
type Mutation {
createUser(input: CreateUserInput!): MutationResponse!
}
# Response bisa berisi error tanpa HTTP error
{
"data": {
"createUser": {
"success": false,
"message": "Email sudah terdaftar",
"user": null
}
}
}
Best Practice
- Mutation name = verb + noun:
createUser,updatePost,deleteComment - Always return affected object (biar cache bisa di-update)
- Use input types (bukan banyak arguments)
- Mutations run sequentially (not parallel like queries)