Permalink
Browse files

Use Rescuable inside resolve.

  • Loading branch information...
dblock committed Oct 30, 2017
1 parent 027d75c commit a0b8f58b3f1ebed993a303bf379def1d8a83a25b
View
@@ -1,26 +1,42 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-10-30 13:50:00 -0400 using RuboCop version 0.51.0.
# on 2017-10-30 17:28:17 -0400 using RuboCop version 0.51.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 16
# Offense count: 3
Lint/UselessAssignment:
Exclude:
- 'app/graphql/helpers/rescuable.rb'
- 'spec/graphql/queries/rescue_from_active_record_record_invalid_query_spec.rb'
- 'spec/graphql/queries/rescue_from_standard_error_spec.rb'
# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 32
# Offense count: 21
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 112
Max: 145
# Offense count: 2
# Offense count: 6
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'app/controllers/graphql_controller.rb'
- 'app/graphql/helpers/rescuable.rb'
- 'app/models/application_record.rb'
- 'app/models/invoice.rb'
- 'config/application.rb'
- 'db/migrate/20171030211755_create_invoices.rb'
# Offense count: 3
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: line_count_dependent, lambda, literal
View
@@ -0,0 +1 @@
require_relative 'helpers/rescuable.rb'
@@ -0,0 +1,18 @@
class Rescuable
attr_reader :resolve_func
def initialize(resolve_func)
@resolve_func = resolve_func
end
def call(obj, args, ctx)
resolve_func.call(obj, args, ctx)
rescue ActiveRecord::RecordNotFound => e
nil
rescue ActiveRecord::RecordInvalid => e
error_messages = e.record.errors.full_messages.join("\n")
GraphQL::ExecutionError.new "Validation failed: #{error_messages}."
rescue StandardError => e
GraphQL::ExecutionError.new e.message
end
end
@@ -5,7 +5,7 @@
return_type InvoiceType
resolve ->(_object, inputs, _ctx) {
OpenStruct.new(
Invoice.new(
id: 1231,
fee_in_cents: inputs[:fee_in_cents]
)
View
@@ -5,16 +5,37 @@
argument :id, !types.Int
description 'Get an invoice by ID.'
resolve ->(_obj, args, _ctx) {
OpenStruct.new(
Invoice.new(
id: args[:id],
fee_in_cents: 20_000
)
}
end
# returning a GraphQL::ExecutionError directly
field :executionError, InvoiceType do
resolve ->(_object, _inputs, _ctx) {
resolve ->(_object, _args, _ctx) {
GraphQL::ExecutionError.new('This has not been implemented yet.')
}
end
# handling ActiveRecord and Standard errors
field :rescueFromActiveRecordRecordNotFound, InvoiceType do
argument :id, !types.Int
resolve Rescuable.new ->(_object, args, _ctx) {
Invoice.where(id: args[:id]).first
}
end
field :rescueFromActiveRecordRecordInvalid, InvoiceType do
resolve Rescuable.new ->(_object, _args, _ctx) {
Invoice.create!
}
end
field :rescueFromStandardError, InvoiceType do
resolve Rescuable.new ->(_object, _args, _ctx) {
raise 'Something went wrong.'
}
end
end
View
@@ -1,4 +1,5 @@
require_relative 'types'
require_relative 'helpers'
require_relative 'queries'
require_relative 'mutations'
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
View
@@ -0,0 +1,3 @@
class Invoice < ApplicationRecord
validates_presence_of :fee_in_cents
end
@@ -0,0 +1,21 @@
require 'rails_helper'
describe 'Rescue from ActiveRecord::RecordInvalid Query', type: :request do
include_context 'GraphQL Client'
let(:query) do
<<-GRAPHQL
query {
rescueFromActiveRecordRecordInvalid {
id
}
}
GRAPHQL
end
it 'handles ActiveRecord::RecordInvalid and returns error' do
expect do
result = client.execute(query)
end.to raise_error Graphlient::Errors::ExecutionError, "rescueFromActiveRecordRecordInvalid: Validation failed: Fee in cents can't be blank."
end
end
@@ -0,0 +1,20 @@
require 'rails_helper'
describe 'Rescue from ActiveRecord::RecordNotFound Query', type: :request do
include_context 'GraphQL Client'
let(:query) do
<<-GRAPHQL
query($id: Int!) {
rescueFromActiveRecordRecordNotFound(id: $id) {
id
}
}
GRAPHQL
end
it 'handles ActiveRecord::RecordNotFound and returns nil' do
result = client.execute(query, id: 42)
expect(result.data.rescue_from_active_record_record_not_found).to be nil
end
end
@@ -0,0 +1,21 @@
require 'rails_helper'
describe 'Rescue from StandardError', type: :request do
include_context 'GraphQL Client'
let(:query) do
<<-GRAPHQL
query {
rescueFromStandardError {
id
}
}
GRAPHQL
end
it 'handles StandardError and returns error' do
expect do
result = client.execute(query)
end.to raise_error Graphlient::Errors::ExecutionError, 'rescueFromStandardError: Something went wrong.'
end
end

0 comments on commit a0b8f58

Please sign in to comment.