Skip to content

Mutation Examples

These examples build on the schema from Sample Application: creating and updating records, file uploads and error handling.

Creating Records

mutation CreateUserWithProfile($userData: UserInput!, $profileData: UserProfileInput!) {
  createUser(newUser: $userData, profile: $profileData) {
    ok
    user {
      id
      username
      email
      firstName
      lastName
      profile {
        bio
        location
        website
      }
    }
    errors {
      field
      messages
    }
  }
}
{
  "userData": {
    "username": "newuser123",
    "email": "newuser@example.com",
    "firstName": "Jane",
    "lastName": "Smith",
    "password": "securePassword123"
  },
  "profileData": {
    "bio": "I'm a web developer passionate about modern technologies",
    "location": "New York, NY",
    "website": "https://janesmith.dev"
  }
}
mutation CreatePost($postData: PostInput!) {
  createPost(newPost: $postData) {
    ok
    post {
      id
      title
      slug
      content
      status
      author {
        username
      }
      category {
        name
      }
      tags {
        name
      }
    }
    errors {
      field
      messages
    }
  }
}
{
  "postData": {
    "title": "Advanced GraphQL Techniques",
    "slug": "advanced-graphql-techniques",
    "content": "In this post, we'll explore advanced GraphQL patterns...",
    "excerpt": "Learn advanced GraphQL patterns and best practices",
    "status": "draft",
    "category": "1",
    "tags": ["1", "2", "3"]
  }
}

Updating Records

mutation UpdatePost($postData: PostInput!) {
  updatePost(newPost: $postData) {
    ok
    post {
      id
      title
      content
      status
      updatedAt
    }
    errors {
      field
      messages
    }
  }
}
{
  "postData": {
    "id": "1",
    "title": "Advanced GraphQL Techniques - Updated",
    "content": "Updated content with new examples...",
    "status": "published"
  }
}

File Uploads

mutation UpdateUserAvatar($userId: ID!, $avatar: Upload!) {
  updateUser(newUser: {id: $userId, profile: {avatar: $avatar}}) {
    ok
    user {
      id
      username
      profile {
        avatar
      }
    }
    errors {
      field
      messages
    }
  }
}

Error Handling

{
  "data": {
    "createPost": {
      "ok": false,
      "post": null,
      "errors": [
        {
          "field": "title",
          "messages": ["This field is required."]
        },
        {
          "field": "slug",
          "messages": ["Post with this slug already exists."]
        },
        {
          "field": "category",
          "messages": ["This field is required."]
        }
      ]
    }
  }
}