Sha256: 41cad73b2afc9b7ab489f5ebcec92c363da74942b5ebd7232dc14dd8f10726e4

Contents?: true

Size: 924 Bytes

Versions: 23

Compression:

Stored size: 924 Bytes

Contents

# frozen_string_literal: true

module GraphQL
  module Types
    # @see {Types::BigInt} for handling integers outside 32-bit range.
    class Int < GraphQL::Schema::Scalar
      description "Represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1."

      MIN = -(2**31)
      MAX = (2**31) - 1

      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)
        value = value.to_i
        if value >= MIN && value <= MAX
          value
        else
          err = GraphQL::IntegerEncodingError.new(value)
          ctx.schema.type_error(err, ctx)
        end
      end

      default_scalar true
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
graphql-1.11.10 lib/graphql/types/int.rb
graphql-1.12.18 lib/graphql/types/int.rb
graphql-1.11.9 lib/graphql/types/int.rb
graphql-1.12.17 lib/graphql/types/int.rb
graphql-1.12.16 lib/graphql/types/int.rb
graphql-1.12.15 lib/graphql/types/int.rb
graphql-1.12.14 lib/graphql/types/int.rb
graphql-1.12.13 lib/graphql/types/int.rb
graphql-1.12.12 lib/graphql/types/int.rb
graphql-1.12.11 lib/graphql/types/int.rb
graphql-1.12.10 lib/graphql/types/int.rb
graphql-1.12.9 lib/graphql/types/int.rb
graphql-1.12.8 lib/graphql/types/int.rb
graphql-1.12.7 lib/graphql/types/int.rb
graphql-1.12.6 lib/graphql/types/int.rb
graphql-1.12.5 lib/graphql/types/int.rb
graphql-1.11.8 lib/graphql/types/int.rb
graphql-1.12.4 lib/graphql/types/int.rb
graphql-1.12.3 lib/graphql/types/int.rb
graphql-1.12.2 lib/graphql/types/int.rb