Sha256: e0099ddafa07979147ac87640f47caea973fa935b5a19ae8678515ea1aa17504

Contents?: true

Size: 938 Bytes

Versions: 121

Compression:

Stored size: 938 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, context: ctx)
          ctx.schema.type_error(err, ctx)
        end
      end

      default_scalar true
    end
  end
end

Version data entries

121 entries across 121 versions & 2 rubygems

Version Path
graphql-2.0.16 lib/graphql/types/int.rb
graphql-1.13.17 lib/graphql/types/int.rb
graphql-2.0.15 lib/graphql/types/int.rb
graphql-2.0.14 lib/graphql/types/int.rb
graphql-1.13.16 lib/graphql/types/int.rb
graphql-2.0.13 lib/graphql/types/int.rb
graphql-2.0.12 lib/graphql/types/int.rb
graphql-1.13.15 lib/graphql/types/int.rb
graphql-2.0.11 lib/graphql/types/int.rb
graphql-1.13.14 lib/graphql/types/int.rb
graphql-1.13.13 lib/graphql/types/int.rb
graphql-2.0.9 lib/graphql/types/int.rb
graphql-2.0.8 lib/graphql/types/int.rb
graphql-2.0.7 lib/graphql/types/int.rb
graphql_cody-1.13.0 lib/graphql/types/int.rb
graphql-1.13.12 lib/graphql/types/int.rb
graphql-2.0.6 lib/graphql/types/int.rb
graphql-2.0.5 lib/graphql/types/int.rb
graphql-2.0.4 lib/graphql/types/int.rb
graphql-2.0.3 lib/graphql/types/int.rb