title: how to handle errors in graphql mutations in rails
body: <p>I'm using graphql for the first time in a rails application and i want to handle model validation errors.</p>
  
  <p>My implementation is as follows:</p>
  
  <p><strong>Model code:</strong></p>
  
  <pre><code>class User &lt; ApplicationRecord
    validates :name, :phone_number, presence: true
  end
  </code></pre>
  
  <p><strong>mutation_type.rb</strong></p>
  
  <pre><code>Types::MutationType = GraphQL::ObjectType.define do
    name "Mutation"
  
    field :createUser, Types::UserType do
      description 'Allows you to create a new user'
  
      argument :name, !types.String
      argument :phone_number, !types.String
  
      resolve -&gt;(obj, args, ctx) {
        user = User.create!(args.to_h)
        user
      }
    end
  end
  </code></pre>
  
  <p><strong>user_type.rb</strong></p>
  
  <pre><code>Types::UserType = GraphQL::ObjectType.define do
    name 'User'
    description 'Represents a user'
  
    field :id, !types.Int, 'The ID of a user'
  
    field :name, !types.String, 'The name of the user'
  
    field :phone_number, !types.String, 'The phone number of a user'
  
    field :errors, types[types.String], "Reasons the object couldn't be created or updated" do
      resolve -&gt; (obj, args, ctx) { obj.errors.full_messages }
    end
  
  end
  </code></pre>
  
  <p>Now in <code>graphiql</code>, when i try to create a user with invalid entry. It does not show the <code>errors</code> in the error field i have added in the <code>user_type</code> and it shows this</p>
  
  <pre><code>{
    "status": 422,
    "error": "Unprocessable Entity",
    "exception": "#&lt;ActiveRecord::RecordInvalid: Validation failed: Phone number can't be blank&gt;",
    "traces": {
      "Application Trace": [
        {
          "id": 15,
          "trace": "app/graphql/types/mutation_type.rb:11:in `block (3 levels) in &lt;top (required)&gt;'"
        },
        {
  ....     
  </code></pre>
  
  <p>Can someone suggest me what am I doing wrong or missing something?</p>