Permalink
Browse files

Rescue at schema level with graphql-errors.

  • Loading branch information...
dblock committed Oct 30, 2017
1 parent 52ec523 commit 9e65eb479c1cbdd3e62865e13e7b43ea57c61d0d
Showing with 22 additions and 23 deletions.
  1. +1 −0 Gemfile
  2. +3 −0 Gemfile.lock
  3. +0 −1 app/graphql/helpers.rb
  4. +0 −18 app/graphql/helpers/rescuable.rb
  5. +3 −3 app/graphql/queries.rb
  6. +15 −1 app/graphql/schema.rb
View
@@ -1,6 +1,7 @@
source 'https://rubygems.org'
gem 'graphql'
gem 'graphql-errors'
gem 'rails', '~> 5.1.4'
gem 'sqlite3'
View
@@ -60,6 +60,8 @@ GEM
graphql-client (0.12.1)
activesupport (>= 3.0, < 6.0)
graphql (~> 1.6)
graphql-errors (0.1.0)
graphql (>= 1.6.0, < 2)
i18n (0.9.0)
concurrent-ruby (~> 1.0)
listen (3.1.5)
@@ -166,6 +168,7 @@ PLATFORMS
DEPENDENCIES
graphlient
graphql
graphql-errors
listen
rails (~> 5.1.4)
rspec-rails
View

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
View
@@ -22,19 +22,19 @@
# handling ActiveRecord and Standard errors
field :rescueFromActiveRecordRecordNotFound, InvoiceType do
argument :id, !types.Int
resolve Rescuable.new ->(_object, args, _ctx) {
resolve ->(_object, args, _ctx) {
Invoice.where(id: args[:id]).first
}
end
field :rescueFromActiveRecordRecordInvalid, InvoiceType do
resolve Rescuable.new ->(_object, _args, _ctx) {
resolve ->(_object, _args, _ctx) {
Invoice.create!
}
end
field :rescueFromStandardError, InvoiceType do
resolve Rescuable.new ->(_object, _args, _ctx) {
resolve ->(_object, _args, _ctx) {
raise 'Something went wrong.'
}
end
View
@@ -1,9 +1,23 @@
require_relative 'types'
require_relative 'helpers'
require_relative 'queries'
require_relative 'mutations'
Schema = GraphQL::Schema.define do
query Query
mutation Mutation
end
GraphQL::Errors.configure(Schema) do
rescue_from ActiveRecord::RecordNotFound do
nil
end
rescue_from ActiveRecord::RecordInvalid do |e|
error_messages = e.record.errors.full_messages.join("\n")
GraphQL::ExecutionError.new "Validation failed: #{error_messages}."
end
rescue_from StandardError do |e|
GraphQL::ExecutionError.new e.message
end
end

0 comments on commit 9e65eb4

Please sign in to comment.