Skip to content

Commit

Permalink
Merge pull request rmosolgo#3206 from toneymathews/refactor-scalar-in…
Browse files Browse the repository at this point in the history
…t-to-spec

Bind scalar int as per spec
  • Loading branch information
Robert Mosolgo authored Nov 4, 2020
2 parents deae8fa + 15feb6c commit 4fdfd1f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def match?(pattern)
require "graphql/analysis_error"
require "graphql/coercion_error"
require "graphql/invalid_name_error"
require "graphql/integer_decoding_error"
require "graphql/integer_encoding_error"
require "graphql/string_encoding_error"
require "graphql/internal_representation"
Expand Down
17 changes: 17 additions & 0 deletions lib/graphql/integer_decoding_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
module GraphQL
# This error is raised when `Types::Int` is given an input value outside of 32-bit integer range.
#
# For really big integer values, consider `GraphQL::Types::BigInt`
#
# @see GraphQL::Types::Int which raises this error
class IntegerDecodingError < GraphQL::RuntimeTypeError
# The value which couldn't be decoded
attr_reader :integer_value

def initialize(value)
@integer_value = value
super("Integer out of bounds: #{value}. \nConsider using GraphQL::Types::BigInt instead.")
end
end
end
2 changes: 2 additions & 0 deletions lib/graphql/schema/default_type_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def self.call(type_error, ctx)
ctx.errors << type_error
when GraphQL::UnresolvedTypeError, GraphQL::StringEncodingError, GraphQL::IntegerEncodingError
raise type_error
when GraphQL::IntegerDecodingError
nil
end
end
end
Expand Down
11 changes: 9 additions & 2 deletions lib/graphql/types/int.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ class Int < GraphQL::Schema::Scalar
MIN = -(2**31)
MAX = (2**31) - 1

def self.coerce_input(value, _ctx)
value.is_a?(Integer) ? value : nil
def self.coerce_input(value, ctx)
return if !value.is_a?(Integer)

if value >= MIN && value <= MAX
value
else
err = GraphQL::IntegerDecodingError.new(value)
ctx.schema.type_error(err, ctx)
end
end

def self.coerce_result(value, ctx)
Expand Down
8 changes: 6 additions & 2 deletions spec/graphql/int_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

describe GraphQL::INT_TYPE do
describe "coerce_input" do
it "accepts ints" do
it "accepts ints within the bounds" do
assert_equal -(2**31), GraphQL::INT_TYPE.coerce_isolated_input(-(2**31))
assert_equal 1, GraphQL::INT_TYPE.coerce_isolated_input(1)
assert_equal (2**31)-1, GraphQL::INT_TYPE.coerce_isolated_input((2**31)-1)
end

it "rejects other types" do
it "rejects other types and ints outside the bounds" do
assert_nil GraphQL::INT_TYPE.coerce_isolated_input("55")
assert_nil GraphQL::INT_TYPE.coerce_isolated_input(true)
assert_nil GraphQL::INT_TYPE.coerce_isolated_input(6.1)
assert_nil GraphQL::INT_TYPE.coerce_isolated_input(2**31)
assert_nil GraphQL::INT_TYPE.coerce_isolated_input(-(2**31 + 1))
end

describe "handling boundaries" do
Expand Down

0 comments on commit 4fdfd1f

Please sign in to comment.