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, ctx) { Time.at(Float(value)) }
  coerce_result ->(value, ctx) { value.to_f }
end

Defined Under Namespace

Modules: NoOpCoerce

Instance Attribute Summary

Attributes inherited from BaseType

#default_relay, #default_scalar, #description, #introspection, #name

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #coerce_isolated_input, #coerce_isolated_result, #connection_type, #default_relay?, #default_scalar?, #define_connection, #define_edge, #edge_type, #get_field, #initialize_copy, #introspection?, resolve_related_type, #resolve_type, #to_definition, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #valid_isolated_input?, #validate_input, #validate_isolated_input

Methods included from Define::InstanceDefinable

#define, #initialize_copy, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeScalarType

Returns a new instance of ScalarType.



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

def initialize
  super
  self.coerce = NoOpCoerce
end

Instance Method Details

#coerce=(proc) ⇒ Object



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

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

#coerce_input=(coerce_input_fn) ⇒ Object



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

def coerce_input=(coerce_input_fn)
  if !coerce_input_fn.nil?
    @coerce_input_proc = ensure_two_arg(coerce_input_fn, :coerce_input)
  end
end

#coerce_result(value, ctx = nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/graphql/scalar_type.rb', line 63

def coerce_result(value, ctx = nil)
  if ctx.nil?
    warn_deprecated_coerce("coerce_isolated_result")
    ctx = GraphQL::Query::NullContext
  end
  @coerce_result_proc.call(value, ctx)
end

#coerce_result=(coerce_result_fn) ⇒ Object



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

def coerce_result=(coerce_result_fn)
  if !coerce_result_fn.nil?
    @coerce_result_proc = ensure_two_arg(coerce_result_fn, :coerce_result)
  end
end

#kindObject



77
78
79
# File 'lib/graphql/scalar_type.rb', line 77

def kind
  GraphQL::TypeKinds::SCALAR
end