Skip to content

Improve scalar result coercion execution errors #5375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/graphql/execution/interpreter/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions spec/graphql/schema/scalar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting difference here. The input example above (from static validation) includes query in the path and this doesn't.

Looking at the spec, I don't think query should be included? But that's a separate issue (if at all).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, query doesn't belong there. Related: #4711

}
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) }
Expand Down