Sha256: d99dd37e8d191450e02a46061997b2b32577bf510e7369d740748ece5d6f73cd

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 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
    defined_by_config :name, :coerce, :coerce_input, :coerce_result, :description
    attr_accessor :name, :description

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

    def valid_non_null_input?(value)
      !coerce_non_null_input(value).nil?
    end

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

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

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

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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
graphql-0.11.0 lib/graphql/scalar_type.rb
graphql-0.10.9 lib/graphql/scalar_type.rb
graphql-0.10.8 lib/graphql/scalar_type.rb
graphql-0.10.7 lib/graphql/scalar_type.rb
graphql-0.10.6 lib/graphql/scalar_type.rb
graphql-0.10.5 lib/graphql/scalar_type.rb
graphql-0.10.4 lib/graphql/scalar_type.rb
graphql-0.10.3 lib/graphql/scalar_type.rb
graphql-0.10.2 lib/graphql/scalar_type.rb
graphql-0.10.1 lib/graphql/scalar_type.rb