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 Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?

Methods included from DefinitionHelpers::DefinedByConfig

included

Methods included from DefinitionHelpers::NonNullWithBang

#!

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#coerce=(proc) ⇒ Object



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

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

#coerce_input=(proc) ⇒ Object



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

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

#coerce_non_null_input(value) ⇒ Object



26
27
28
# File 'lib/graphql/scalar_type.rb', line 26

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

#coerce_result(value) ⇒ Object



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

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

#coerce_result=(proc) ⇒ Object



38
39
40
# File 'lib/graphql/scalar_type.rb', line 38

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

#kindObject



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

def kind
  GraphQL::TypeKinds::SCALAR
end

#valid_non_null_input?(value) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/graphql/scalar_type.rb', line 22

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