Graphql-ruby: Unknown attribute `client_mutation_id` when executing a mutation

Created on 3 Sep 2018  路  6Comments  路  Source: rmosolgo/graphql-ruby

What kind of magic is this?

class Types::Mutation < Types::BaseObject
  field :createUser, mutation: Mutations::CreateUser
  ...
end

class Mutations::CreateUser < Mutations::BaseMutation
  argument :email, String, required: true
  argument :password, String, required: true

  type Types::User

  def resolve args
    # binding.pry
    User.create!(args.slice(:email, :password) )
  rescue ActiveRecord::RecordInvalid => e
    GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}")
  end
end

Runing mutation:

Runing mutation

Got: can't write unknown attribute client_mutation_id, but user was created!

Running pry

From: /home/user/code/walexypro/app/graphql/mutations/create_user.rb @ line 8 Mutations::CreateUser#resolve:

     7: def resolve args
 =>  8:   binding.pry
     9:   User.create!(args.slice(:email, :password) )
    10: rescue ActiveRecord::RecordInvalid => e
    11:   GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}")
    12: end

[WallexyPro][development] #<Mutations::CreateUser:0x000055a7ab7d0ac0>:0> args
=> {:email=>"[email protected]", :password=>"123123123"}
[WallexyPro][development] #<Mutations::CreateUser:0x000055a7ab7d0ac0>:0> User.create!(args )

User created with no problems. adding args.slice(:email, :password) not helps too.

  def resolve args
    # binding.pry
    # User.create!(args)
    User.first
  # rescue ActiveRecord::RecordInvalid => e
    # GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}")
  end

  def resolve email:, password:
    # binding.pry
    # User.create!(args)
    User.first
  # rescue ActiveRecord::RecordInvalid => e
    # GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}")
  end

Two variants above got same error can't write unknown attribute client_mutation_id

I have only one question WTF happens here?

Most helpful comment

try

- User.first
+ { user: User.first }

So that field :user, ... will look up the :user key in that Hash.

All 6 comments

If I'm not mistaken, client_mutation_id is part of a Relay Input Object and not part of a basic mutation. Can you try to extend your mutations from GraphQL::Schema::RelayClassicMutation instead of Mutations::BaseMutation and see what happens?

Yeah, I think @Willianvdv is on to something. Does your Mutations::BaseMutation extend RelayClassicMutation? If so, you need to return a hash and use field ... to describe the return type instead of type ....

If you want to use type ... maybe you shold extend GraphQL::Schema::Mutation instead. See also https://github.com/rmosolgo/graphql-ruby/issues/1638

@Willianvdv thanks for renaming this issue to something descriptive and helpful 馃槈

class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation
end

class Mutations::CreateUser < Mutations::BaseMutation
  argument :email, String, required: true
  argument :password, String, required: true

  field :user, Types::User, null: false

  def resolve email:, password:
    # binding.pry
    # User.create!(args)
  # rescue ActiveRecord::RecordInvalid => e
    # GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}")
    User.first
  end
end

try

- User.first
+ { user: User.first }

So that field :user, ... will look up the :user key in that Hash.

@rmosolgo thanks, this works

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmc2015 picture dmc2015  路  3Comments

pareeohnos picture pareeohnos  路  3Comments

rylanc picture rylanc  路  3Comments

jesster2k10 picture jesster2k10  路  3Comments

jtippett picture jtippett  路  3Comments