diff --git a/lib/graphql/execution/interpreter/runtime.rb b/lib/graphql/execution/interpreter/runtime.rb index 6753720a98..085043b11c 100644 --- a/lib/graphql/execution/interpreter/runtime.rb +++ b/lib/graphql/execution/interpreter/runtime.rb @@ -675,6 +675,8 @@ def continue_field(value, owner_type, field, current_type, ast_node, next_select when "SCALAR", "ENUM" r = begin current_type.coerce_result(value, context) + rescue GraphQL::ExecutionError => ex_err + return continue_value(ex_err, field, is_non_null, ast_node, result_name, selection_result) rescue StandardError => err query.handle_or_reraise(err) end diff --git a/spec/graphql/schema/scalar_spec.rb b/spec/graphql/schema/scalar_spec.rb index 8c1171cbec..7c285c4c00 100644 --- a/spec/graphql/schema/scalar_spec.rb +++ b/spec/graphql/schema/scalar_spec.rb @@ -119,18 +119,28 @@ class CustomScalar < GraphQL::Schema::Scalar def self.coerce_input(val, ctx) raise GraphQL::CoercionError, "#{val.inspect} can't be Custom value" end + + def self.coerce_result(val, ctx) + raise GraphQL::CoercionError, "#{val.inspect} can't be Custom value" + end end class Query < GraphQL::Schema::Object field :f1, String do argument :arg, CustomScalar end + + field :f2, CustomScalar + + def f2 + "bad" + end end query(Query) end - it "makes a nice validation error" do + it "makes a nice validation error for input coercion" do result = CoercionErrorSchema.execute("{ f1(arg: \"a\") }") expected_error = { "message" => "\"a\" can't be Custom value", @@ -143,8 +153,17 @@ class Query < GraphQL::Schema::Object } assert_equal [expected_error], result["errors"] end - end + it "makes a nice validation error for reuslt coercion" do + result = CoercionErrorSchema.execute("{ f2 }") + expected_error = { + "message" => "\"bad\" can't be Custom value", + "locations" => [{"line"=>1, "column"=>3}], + "path" => ["f2"], + } + assert_equal [expected_error], result["errors"] + end + end describe "validate_input with good input" do let(:result) { GraphQL::Types::Int.validate_input(150, GraphQL::Query::NullContext.instance) }