-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Improve Scalar result coercion spec compliance #5306
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
# This error is raised when a scalar type cannot coerce a value to its expected type. It is considered legacy because it's raised as a RuntimeTypeError from `Schema.type_error` when `Schema.spec_compliant_scalar_coercion_errors` is not enabled. | ||
class ScalarCoercionError < GraphQL::RuntimeTypeError | ||
# The value which couldn't be coerced | ||
attr_reader :value | ||
|
||
# @return [GraphQL::Schema::Field] The field that returned a type error | ||
attr_reader :field | ||
|
||
# @return [Array<String, Integer>] Where the field appeared in the GraphQL response | ||
attr_reader :path | ||
|
||
def initialize(message, value:, context:) | ||
@value = value | ||
@field = context[:current_field] | ||
@path = context[:current_path] | ||
|
||
super(message) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,5 @@ | ||
# frozen_string_literal: true | ||
module GraphQL | ||
class StringEncodingError < GraphQL::RuntimeTypeError | ||
attr_reader :string, :field, :path | ||
def initialize(str, context:) | ||
@string = str | ||
@field = context[:current_field] | ||
@path = context[:current_path] | ||
message = "String #{str.inspect} was encoded as #{str.encoding}".dup | ||
if @path | ||
message << " @ #{@path.join(".")}" | ||
end | ||
if @field | ||
message << " (#{@field.path})" | ||
end | ||
message << ". GraphQL requires an encoding compatible with UTF-8." | ||
super(message) | ||
end | ||
class StringEncodingError < GraphQL::ScalarCoercionError | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,21 +26,77 @@ | |
assert_equal(-(2**31), GraphQL::Types::Int.coerce_result(-(2**31), context)) | ||
end | ||
|
||
it "replaces values, if configured to do so" do | ||
assert_equal Dummy::Schema::MAGIC_INT_COERCE_VALUE, GraphQL::Types::Int.coerce_result(99**99, context) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this have to be removed? It seems like the override in the example schema could be retained, but a method call would have to be changed.... or is this behavior not possible anymore? (Maybe the old override could be retained as-is if an alias accessor was added to the new IntegerEncodingError) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it doesn't have to be I don't think. I'll check |
||
|
||
it "raises on values out of bounds" do | ||
err_ctx = GraphQL::Query.new(Dummy::Schema, "{ __typename }").context | ||
assert_raises(GraphQL::IntegerEncodingError) { GraphQL::Types::Int.coerce_result(2**31, err_ctx) } | ||
err = assert_raises(GraphQL::IntegerEncodingError) { GraphQL::Types::Int.coerce_result(-(2**31 + 1), err_ctx) } | ||
assert_equal "Integer out of bounds: -2147483649. Consider using ID or GraphQL::Types::BigInt instead.", err.message | ||
assert_equal "Int cannot represent non 32-bit signed integer value: -2147483649", err.message | ||
|
||
err = assert_raises GraphQL::IntegerEncodingError do | ||
Dummy::Schema.execute("{ hugeInteger }") | ||
end | ||
expected_err = "Integer out of bounds: 2147483648 @ hugeInteger (Query.hugeInteger). Consider using ID or GraphQL::Types::BigInt instead." | ||
assert_equal expected_err, err.message | ||
assert_equal "Int cannot represent non 32-bit signed integer value: 2147483648", err.message | ||
end | ||
|
||
describe "with Schema.spec_compliant_scalar_coercion_errors" do | ||
class IntScalarSchema < GraphQL::Schema | ||
class Query < GraphQL::Schema::Object | ||
field :int, GraphQL::Types::Int do | ||
argument :value, GraphQL::Types::Int | ||
end | ||
|
||
field :bad_int, GraphQL::Types::Int | ||
|
||
def int(value:) | ||
value | ||
end | ||
|
||
def bad_int | ||
2**31 # Out of range | ||
end | ||
end | ||
|
||
query(Query) | ||
end | ||
|
||
class IntSpecCompliantErrors < IntScalarSchema | ||
spec_compliant_scalar_coercion_errors true | ||
end | ||
|
||
class IntNonSpecComplaintErrors < IntScalarSchema | ||
spec_compliant_scalar_coercion_errors false | ||
end | ||
|
||
it "returns GraphQL execution errors with spec_compliant_scalar_coercion_errors enabled" do | ||
query = "{ badInt }" | ||
result = IntSpecCompliantErrors.execute(query) | ||
|
||
assert_equal( | ||
{ | ||
"errors" => [ | ||
{ | ||
"message" => "Int cannot represent non 32-bit signed integer value: 2147483648", | ||
"locations" => [{ "line" => 1, "column" => 3 }], | ||
"path" => ["badInt"], | ||
}, | ||
], | ||
"data" => { | ||
"badInt" => nil, | ||
} | ||
}, | ||
result.to_h | ||
) | ||
end | ||
|
||
it "raises Ruby exceptions with spec_compliant_scalar_coercion_errors disabled" do | ||
query = "{ badInt }" | ||
|
||
error = assert_raises(GraphQL::IntegerEncodingError) do | ||
IntNonSpecComplaintErrors.execute(query) | ||
end | ||
|
||
assert_equal("Int cannot represent non 32-bit signed integer value: 2147483648", error.message) | ||
end | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any possible issues here? No tests break but this will change error messages, though it will improve and make them more spec compliant.