Graphql-ruby: [1.8] Mutation error handler

Created on 9 Apr 2018  路  6Comments  路  Source: rmosolgo/graphql-ruby

Hi all,
What do we use to throw an error message in resolve function of Mutation?

I tried GraphQL::ExecutionError but get the error:
```

Failed to implement RegisterPayload.user, tried:

  • #<Class:0x00005579c8a25340>#user, which did not exist
  • GraphQL::ExecutionError#user, which did not exist
  • Looking up hash key "user" on Please check all input fields are correct, but it wasn't a Hash

To implement this field, define one of the methods above (and check for typos)

graphql (1.8.0.pre10) lib/graphql/schema/field/dynamic_resolve.rb, line 23
````

Most helpful comment

+1 use raise instead of return took me 3 hours to realize this

All 6 comments

Could you share the code that gave you the error?

I expect that you have two options to add to the "errors" key from mutations:

  • raise a GraphQL::ExecutionError
  • return a GraphQL::ExecutionError

Please share a snippet of code that gave you that error and we can keep looking!

Thanks for your fast reply.
I used return GraphQL::ExecutionError.new() as the old version. But I've just tried raise and it works. I'll close this issue.

Hi @rmosolgo , I have another question, in v1.8, what alternative code for a query resolve with arguments like?

field :search_posts, types[PostType] do
  argument :category, types.String
  resolve ->(obj, args, ctx) {
    args[:category]
    # => maybe a string, eg "Programming"
    if args[:category]
      Post.where(category: category).limit(10)
    else
      Post.all.limit(10)
    end
  }
end

Arguments are splatted as Ruby keyword args, so here's the equivalent code in a class-based type definition:

field :search_posts, [PostType], null: true do
  argument :category, String, required: false 
end 

def search_posts(category: nil) 
  if category
    Post.where(category: category).limit(10)
  else 
    Post.all.limit(10)
  end 
end 

I used the nil default value because category is not required.

Thanks, I read this before but I didn't think it could be used in this situation.
Btw, If my project has a lot of queries, should I use Interface to separate fields of Types::QueryType to multiple files?

+1 use raise instead of return took me 3 hours to realize this

Was this page helpful?
0 / 5 - 0 ratings