Sha256: dfdeb3e524939e9a057bcc06d0f7e52fa6216fb4395e1a37df97b23501e930af

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

module GraphQL
  # The parent type for scalars, eg {GraphQL::STRING_TYPE}, {GraphQL::INT_TYPE}
  #
  # @example defining a type for Time
  #   TimeType = GraphQL::ScalarType.define do
  #     name "Time"
  #     description "Time since epoch in seconds"
  #
  #     coerce_input ->(value) { Time.at(Float(value)) }
  #     coerce_result ->(value) { value.to_f }
  #   end
  #
  class ScalarType < GraphQL::BaseType
    accepts_definitions :coerce, :coerce_input, :coerce_result

    def coerce=(proc)
      self.coerce_input = proc
      self.coerce_result = proc
    end

    def validate_non_null_input(value)
      result = Query::InputValidationResult.new
      if coerce_non_null_input(value).nil?
        result.add_problem("Could not coerce value #{JSON.dump(value)} to #{name}")
      end
      result
    end

    def coerce_non_null_input(value)
      ensure_defined
      @coerce_input_proc.call(value)
    end

    def coerce_input=(proc)
      if !proc.nil?
        @coerce_input_proc = proc
      end
    end

    def coerce_result(value)
      ensure_defined
      @coerce_result_proc.call(value)
    end

    def coerce_result=(proc)
      if !proc.nil?
        @coerce_result_proc = proc
      end
    end

    def kind
      GraphQL::TypeKinds::SCALAR
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
graphql-0.18.4 lib/graphql/scalar_type.rb
graphql-0.18.3 lib/graphql/scalar_type.rb
graphql-0.18.2 lib/graphql/scalar_type.rb
graphql-0.18.1 lib/graphql/scalar_type.rb
graphql-0.18.0 lib/graphql/scalar_type.rb
graphql-0.17.2 lib/graphql/scalar_type.rb
graphql-0.17.1 lib/graphql/scalar_type.rb
graphql-0.17.0 lib/graphql/scalar_type.rb