Class: GraphQL::ScalarType

Inherits:
BaseType show all
Defined in:
lib/graphql/scalar_type.rb

Overview

The parent type for scalars, eg STRING_TYPE, INT_TYPE

Examples:

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

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #connection_type, #define_connection, #define_edge, #edge_type, #get_field, resolve_related_type, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input

Methods included from Define::InstanceDefinable

#definition_proc=, included, #metadata

Methods included from Define::NonNullWithBang

#!

Instance Method Details

#coerce=(proc) ⇒ Object



16
17
18
19
# File 'lib/graphql/scalar_type.rb', line 16

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

#coerce_input=(proc) ⇒ Object



34
35
36
37
38
# File 'lib/graphql/scalar_type.rb', line 34

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

#coerce_non_null_input(value) ⇒ Object



29
30
31
32
# File 'lib/graphql/scalar_type.rb', line 29

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

#coerce_result(value) ⇒ Object



40
41
42
43
# File 'lib/graphql/scalar_type.rb', line 40

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

#coerce_result=(proc) ⇒ Object



45
46
47
48
49
# File 'lib/graphql/scalar_type.rb', line 45

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

#kindObject



51
52
53
# File 'lib/graphql/scalar_type.rb', line 51

def kind
  GraphQL::TypeKinds::SCALAR
end

#validate_non_null_input(value) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/graphql/scalar_type.rb', line 21

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