Class: Rails::GraphQL::Type::Scalar

Inherits:
Rails::GraphQL::Type show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/rails/graphql/type/scalar.rb

Overview

GraphQL ScalarType

Scalar types represent primitive leaf values in a GraphQL type system. See spec.graphql.org/June2018/#ScalarTypeDefinition

This class works very similarly to ActiveModel::Type::Value, but instead of working with instances, we operate in the singleton way.

The ar_type defines to which ActiveRecord type the value is casted when serializing to hash, which indicates if a cast is necessary or not.

Defined Under Namespace

Classes: AnyScalar, BigintScalar, BinaryScalar, BooleanScalar, DateScalar, DateTimeScalar, DecimalScalar, FloatScalar, IdScalar, IntScalar, JsonScalar, StringScalar, TimeScalar

Constant Summary

Constants inherited from Rails::GraphQL::Type

KINDS

Class Method Summary collapse

Methods inherited from Rails::GraphQL::Type

=~, base_type, create!, decorate, find_by_gid, gid_base_class, input_type?, kind, kind_enum, leaf_type?, operational?, output_type?, to_gql_backtrace

Methods included from Helpers::WithDirectives

#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?, #validate!

Methods included from Helpers::WithGlobalID

#to_gid_param, #to_global_id

Methods included from Helpers::Registerable

#aliases, extended, #inherited, #register!, #registered?

Class Method Details

.as_json(value) ⇒ Object

Transforms the given value to its representation in a Hash object



55
56
57
# File 'lib/rails/graphql/type/scalar.rb', line 55

def as_json(value)
  value.to_s
end

.deserialize(value) ⇒ Object

Turn a user input of this given type into a Ruby object



60
61
62
# File 'lib/rails/graphql/type/scalar.rb', line 60

def deserialize(value)
  value.is_a?(::GQLParser::Token) ? as_json(value) : value
end

.inspectObject



64
65
66
67
68
69
# File 'lib/rails/graphql/type/scalar.rb', line 64

def inspect
  return super if self.eql?(Type::Scalar)
  directives = inspect_directives
  directives.prepend(' ') if directives.present?
  +"#<GraphQL::Scalar #{gql_name}#{directives}>"
end

.to_json(value) ⇒ Object

Transforms the given value to its representation in a JSON string



50
51
52
# File 'lib/rails/graphql/type/scalar.rb', line 50

def to_json(value)
  as_json(value).inspect
end

.valid_input?(value) ⇒ Boolean

Check if a given value is a valid non-deserialized input

Returns:

  • (Boolean)


40
41
42
# File 'lib/rails/graphql/type/scalar.rb', line 40

def valid_input?(value)
  valid_token?(value) || value.is_a?(String)
end

.valid_output?(value) ⇒ Boolean

Check if a given value is a valid non-serialized output

Returns:

  • (Boolean)


45
46
47
# File 'lib/rails/graphql/type/scalar.rb', line 45

def valid_output?(value)
  value.respond_to?(:to_s)
end