Skip to content

Commit 7c2af23

Browse files
authored
Merge pull request #5375 from rmosolgo/improve-scalar-coercion-result-errors
Improve scalar result coercion execution errors
2 parents 4ecf14f + 890556b commit 7c2af23

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ def continue_field(value, owner_type, field, current_type, ast_node, next_select
675675
when "SCALAR", "ENUM"
676676
r = begin
677677
current_type.coerce_result(value, context)
678+
rescue GraphQL::ExecutionError => ex_err
679+
return continue_value(ex_err, field, is_non_null, ast_node, result_name, selection_result)
678680
rescue StandardError => err
679681
query.handle_or_reraise(err)
680682
end

spec/graphql/schema/scalar_spec.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,28 @@ class CustomScalar < GraphQL::Schema::Scalar
119119
def self.coerce_input(val, ctx)
120120
raise GraphQL::CoercionError, "#{val.inspect} can't be Custom value"
121121
end
122+
123+
def self.coerce_result(val, ctx)
124+
raise GraphQL::CoercionError, "#{val.inspect} can't be Custom value"
125+
end
122126
end
123127

124128
class Query < GraphQL::Schema::Object
125129
field :f1, String do
126130
argument :arg, CustomScalar
127131
end
132+
133+
field :f2, CustomScalar
134+
135+
def f2
136+
"bad"
137+
end
128138
end
129139

130140
query(Query)
131141
end
132142

133-
it "makes a nice validation error" do
143+
it "makes a nice validation error for input coercion" do
134144
result = CoercionErrorSchema.execute("{ f1(arg: \"a\") }")
135145
expected_error = {
136146
"message" => "\"a\" can't be Custom value",
@@ -143,8 +153,17 @@ class Query < GraphQL::Schema::Object
143153
}
144154
assert_equal [expected_error], result["errors"]
145155
end
146-
end
147156

157+
it "makes a nice validation error for reuslt coercion" do
158+
result = CoercionErrorSchema.execute("{ f2 }")
159+
expected_error = {
160+
"message" => "\"bad\" can't be Custom value",
161+
"locations" => [{"line"=>1, "column"=>3}],
162+
"path" => ["f2"],
163+
}
164+
assert_equal [expected_error], result["errors"]
165+
end
166+
end
148167

149168
describe "validate_input with good input" do
150169
let(:result) { GraphQL::Types::Int.validate_input(150, GraphQL::Query::NullContext.instance) }

0 commit comments

Comments
 (0)