<commit_before>---
title: Mutations
layout: guide
search: true
section: Queries
desc: Creating, Updating and Deleting
index: 3
---

Mutations are queries with side effects. Mutations are used to mutate your data. In order to use mutations you need to define a mutation root that allows for defining fields that can mutate your data.

```ruby
Schema = GraphQL::Schema.define do
  query QueryRoot
  mutation MutationRoot
end
```

Then add your mutations here:

```ruby
MutationRoot = GraphQL::ObjectType.define do
  name "Mutation"

  field :ratePost, PostType do
    description "Rates a post"
    argument :postId, !types.Int
    argument :stars, !types.Int

    resolve ->(o,args,c) {
      post = Post.find(args[:postId])
      post = post.rate(args[:stars])
      post
    }
  end
end
```

The mutation query would look like:

```graphql
mutation {
  ratePost(postId: 1, stars: 5) {
    stars
  }
}
```


Instead of specifying a long list of arguments, you can also specify an input type. This allows you to have all input fields in an Input type.


```ruby
MutationRoot = GraphQL::ObjectType.define do
  name "Mutation"
<commit_msg>Consistent root type naming in code examples<commit_after>---
title: Mutations
layout: guide
search: true
section: Queries
desc: Creating, Updating and Deleting
index: 3
---

Mutations are queries with side effects. Mutations are used to mutate your data. In order to use mutations you need to define a mutation root type that allows for defining fields that can mutate your data.

```ruby
Schema = GraphQL::Schema.define do
  query QueryType
  mutation MutationType
end
```

Then add your mutations here:

```ruby
MutationType = GraphQL::ObjectType.define do
  name "Mutation"

  field :ratePost, PostType do
    description "Rates a post"
    argument :postId, !types.Int
    argument :stars, !types.Int

    resolve ->(o,args,c) {
      post = Post.find(args[:postId])
      post = post.rate(args[:stars])
      post
    }
  end
end
```

The mutation query would look like:

```graphql
mutation {
  ratePost(postId: 1, stars: 5) {
    stars
  }
}
```


Instead of specifying a long list of arguments, you can also specify an input type. This allows you to have all input fields in an Input type.


```ruby
MutationType = GraphQL::ObjectType.define do
  name "Mutation"

{'fraction_non_alphanumeric': 0.09012131715771231, 'fraction_numerical': 0.0025996533795493936, 'mean_word_length': 3.655241935483871, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4198277', 'n_tokens_mistral': 742, 'n_tokens_neox': 688, 'n_words': 287}