Sha256: 9176636dc3580bdaa96806f852444b03a2b384e2800c7ef35af61fe0cef23680

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Rails
  module GraphQL
    class Type
      # The ID scalar type represents a unique identifier, often used to
      # refetch an object or as the key for a cache. The ID type is serialized
      # in the same way as a +StringScalar+.
      #
      # See http://spec.graphql.org/June2018/#sec-ID
      class Scalar::IdScalar < Scalar
        self.spec_object = true

        rename! 'ID'

        desc <<~DESC
          The ID scalar type represents a unique identifier and it is serialized in the same
          way as a String but it accepts both numeric and string based values as input.
        DESC

        class << self
          def valid_input?(value)
            valid_token?(value, :string) || valid_token?(value, :int) ||
              value.is_a?(String) || value.is_a?(Integer)
          end

          def as_json(value)
            value = value.to_s unless value.is_a?(String)
            value = value.encode(Encoding::UTF_8) unless value.encoding.eql?(Encoding::UTF_8)
            value
          end

          def deserialize(value)
            valid_token?(value, :string) ? value[1..-2] : value
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-graphql-1.0.2 lib/rails/graphql/type/scalar/id_scalar.rb
rails-graphql-1.0.1 lib/rails/graphql/type/scalar/id_scalar.rb
rails-graphql-1.0.0 lib/rails/graphql/type/scalar/id_scalar.rb
rails-graphql-1.0.0.rc2 lib/rails/graphql/type/scalar/id_scalar.rb
rails-graphql-1.0.0.rc1 lib/rails/graphql/type/scalar/id_scalar.rb
rails-graphql-1.0.0.beta lib/rails/graphql/type/scalar/id_scalar.rb