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.

You can customize the error message for invalid input values by raising a GraphQL::CoercionError within coerce_input:

This will result in the message of the GraphQL::CoercionError being used in the error response:

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

raising a custom error message

TimeType = GraphQL::ScalarType.define do
  name "Time"
  description "Time since epoch in seconds"

  coerce_input ->(value, ctx) do
    begin
      Time.at(Float(value))
    rescue ArgumentError
      raise GraphQL::CoercionError, "cannot coerce `#{value.inspect}` to Float"
    end
  end

  coerce_result ->(value, ctx) { value.to_f }
end

custom error response

{"message"=>"cannot coerce `"2"` to Float", "locations"=>[{"line"=>3, "column"=>9}], "fields"=>["arg"]}

Defined Under Namespace

Modules: NoOpCoerce

Instance Attribute Summary

Attributes inherited from BaseType

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

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #coerce_isolated_input, #coerce_isolated_result, #default_relay?, #default_scalar?, #get_field, #initialize_copy, #introspection?, #list?, #non_null?, 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 Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Define::InstanceDefinable

#define, #initialize_copy, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeScalarType

Returns a new instance of ScalarType



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

def initialize
  super
  self.coerce = NoOpCoerce
end

Instance Method Details

#coerce=(proc) ⇒ Object



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

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

#coerce_input=(coerce_input_fn) ⇒ Object



81
82
83
84
85
# File 'lib/graphql/scalar_type.rb', line 81

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



87
88
89
90
91
92
93
# File 'lib/graphql/scalar_type.rb', line 87

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



95
96
97
98
99
# File 'lib/graphql/scalar_type.rb', line 95

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



101
102
103
# File 'lib/graphql/scalar_type.rb', line 101

def kind
  GraphQL::TypeKinds::SCALAR
end