Class: GraphQL::ScalarType

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

Overview

GraphQL::ScalarType

Scalars are plain values. They are leaf nodes in a GraphQL query tree.

Built-in Scalars

GraphQL comes with standard built-in scalars:

Constant .define helper
GraphQL::STRING_TYPE types.String
GraphQL::INT_TYPE types.Int
GraphQL::FLOAT_TYPE types.Float
GraphQL::ID_TYPE types.ID
GraphQL::BOOLEAN_TYPE types.Boolean

(types is an instance of GraphQL::Definition::TypeDefiner; .String, .Float, etc are methods which return built-in scalars.)

Custom Scalars

You can define custom scalars for your GraphQL server. It requires some special functions:

  • coerce_input is used to prepare incoming values for GraphQL execution. (Incoming values come from variables or literal values in the query string.)
  • coerce_result is used to turn Ruby values back into serializable values for query responses.

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

Attributes inherited from BaseType

#description, #name

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

#define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Instance Method Details

#coerce=(proc) ⇒ Object



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

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

#coerce_input=(proc) ⇒ Object



57
58
59
60
61
# File 'lib/graphql/scalar_type.rb', line 57

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

#coerce_non_null_input(value) ⇒ Object



53
54
55
# File 'lib/graphql/scalar_type.rb', line 53

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

#coerce_result(value) ⇒ Object



63
64
65
# File 'lib/graphql/scalar_type.rb', line 63

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

#coerce_result=(proc) ⇒ Object



67
68
69
70
71
# File 'lib/graphql/scalar_type.rb', line 67

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

#kindObject



73
74
75
# File 'lib/graphql/scalar_type.rb', line 73

def kind
  GraphQL::TypeKinds::SCALAR
end

#validate_non_null_input(value, warden) ⇒ Object



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

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