Class: GraphQL::Schema::Scalar

Inherits:
Member
  • Object
show all
Extended by:
Member::AcceptsDefinition, Member::CachedGraphQLDefinition::DeprecatedToGraphQL, Member::ValidatesInput
Defined in:
lib/graphql/schema/scalar.rb

Constant Summary

Constants included from Member::HasDirectives

Member::HasDirectives::NO_DIRECTIVES

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Class Method Summary collapse

Methods included from Member::ValidatesInput

coerce_isolated_input, coerce_isolated_result, valid_input?, valid_isolated_input?, validate_input

Methods included from Member::CachedGraphQLDefinition::DeprecatedToGraphQL

to_graphql

Methods included from Member::CachedGraphQLDefinition

#deprecated_to_graphql, #graphql_definition, #initialize_copy, #type_class

Methods included from Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Member::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::Scoped

#scope_items

Methods included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

#ast_node

Methods included from Member::HasDirectives

#directive, #directives, #remove_directive

Class Method Details

.coerce_input(val, ctx) ⇒ Object


9
10
11
# File 'lib/graphql/schema/scalar.rb', line 9

def coerce_input(val, ctx)
  val
end

.coerce_result(val, ctx) ⇒ Object


13
14
15
# File 'lib/graphql/schema/scalar.rb', line 13

def coerce_result(val, ctx)
  val
end

.default_scalar(is_default = nil) ⇒ Object


35
36
37
38
39
40
# File 'lib/graphql/schema/scalar.rb', line 35

def default_scalar(is_default = nil)
  if !is_default.nil?
    @default_scalar = is_default
  end
  @default_scalar
end

.default_scalar?Boolean

Returns:


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

def default_scalar?
  @default_scalar ||= false
end

.kindObject


31
32
33
# File 'lib/graphql/schema/scalar.rb', line 31

def kind
  GraphQL::TypeKinds::SCALAR
end

.to_graphqlObject


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql/schema/scalar.rb', line 19

def to_graphql
  type_defn = GraphQL::ScalarType.new
  type_defn.name = graphql_name
  type_defn.description = description
  type_defn.coerce_result = method(:coerce_result)
  type_defn.coerce_input = method(:coerce_input)
  type_defn.[:type_class] = self
  type_defn.default_scalar = default_scalar
  type_defn.ast_node = ast_node
  type_defn
end

.validate_non_null_input(value, ctx) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/graphql/schema/scalar.rb', line 46

def validate_non_null_input(value, ctx)
  result = Query::InputValidationResult.new
  coerced_result = begin
    ctx.query.with_error_handling do
      coerce_input(value, ctx)
    end
  rescue GraphQL::CoercionError => err
    err
  end

  if coerced_result.nil?
    str_value = if value == Float::INFINITY
      ""
    else
      " #{GraphQL::Language.serialize(value)}"
    end
    result.add_problem("Could not coerce value#{str_value} to #{graphql_name}")
  elsif coerced_result.is_a?(GraphQL::CoercionError)
    result.add_problem(coerced_result.message, message: coerced_result.message, extensions: coerced_result.extensions)
  end
  result
end