Class: GraphQL::Schema::Scalar

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

Constant Summary

Constants included from Member::GraphQLTypeNames

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

Instance Attribute Summary

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Attributes included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Attributes included from Member::HasAstNode

#ast_node

Class Method Summary collapse

Methods included from Member::ValidatesInput

coerce_isolated_input, coerce_isolated_result, valid_input?, valid_isolated_input?, validate_input

Methods inherited from Member

authorizes?

Methods included from Member::BaseDSLMethods

#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?

Methods included from FindInheritedValue

extended, included

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

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

Methods included from Member::Scoped

#inherited, #reauthorize_scoped_objects, #scope_items

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

extended, #inherited

Methods included from Member::HasDirectives

add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive

Class Method Details

.coerce_input(val, ctx) ⇒ Object

: (untyped val, GraphQL::Query::Context ctx) -> untyped



11
12
13
# File 'lib/graphql/schema/scalar.rb', line 11

def coerce_input(val, ctx)
  val
end

.coerce_result(val, ctx) ⇒ Object

: (untyped val, GraphQL::Query::Context ctx) -> untyped



16
17
18
# File 'lib/graphql/schema/scalar.rb', line 16

def coerce_result(val, ctx)
  val
end

.default_scalar(is_default = nil) ⇒ Object

: (?bool? is_default) -> bool?



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

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

.default_scalar? ⇒ Boolean

: -> bool

Returns:



47
48
49
# File 'lib/graphql/schema/scalar.rb', line 47

def default_scalar?
  @default_scalar ||= false
end

.kind ⇒ Object

: -> GraphQL::TypeKinds::TypeKind



21
22
23
# File 'lib/graphql/schema/scalar.rb', line 21

def kind
  GraphQL::TypeKinds::SCALAR
end

.specified_by_url(new_url = nil) ⇒ Object

: (?String? new_url) -> String?



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphql/schema/scalar.rb', line 26

def specified_by_url(new_url = nil)
  if new_url
    directive(GraphQL::Schema::Directive::SpecifiedBy, url: new_url)
  elsif (directive = directives.find { |dir| dir.graphql_name == "specifiedBy" })
    directive.arguments[:url] # rubocop:disable Development/ContextIsPassedCop
  elsif superclass.respond_to?(:specified_by_url)
    superclass.specified_by_url
  else
    nil
  end
end

.validate_non_null_input(value, ctx, max_errors: nil) ⇒ Object

Coerce value with coerce_input and report any problems.

GraphQL-Ruby calls this for variable and literal input values. When the value is valid, it returns a result with coerced_value, which is used as the argument value.

Overrides may return InputValidationResult.coerced(value) to provide a coerced value (including nil). If an override returns a valid result without a coerced value, coerce_input will be called at runtime, and any GraphQL::CoercionError it raises will be added as a field error.

Returns

  • GraphQL::Query::InputValidationResult, nil

:call-seq:

validate_non_null_input(value, ctx, max_errors:) -> GraphQL::Query::InputValidationResult | nil

: (untyped value, GraphQL::Query::Context ctx, ?max_errors: Integer?) -> GraphQL::Query::InputValidationResult?



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphql/schema/scalar.rb', line 68

def validate_non_null_input(value, ctx, max_errors: nil)
  coerced_result = begin
    coerce_input(value, ctx)
  rescue GraphQL::CoercionError => err
    err
  rescue StandardError => err
    ctx.query.handle_or_reraise(err)
  end

  if coerced_result.nil?
    Query::InputValidationResult.from_problem("Could not coerce value #{GraphQL::Language.serialize(value)} to #{graphql_name}")
  elsif coerced_result.is_a?(GraphQL::CoercionError)
    Query::InputValidationResult.from_problem(coerced_result.message, message: coerced_result.message, extensions: coerced_result.extensions)
  else
    Query::InputValidationResult.coerced(coerced_result)
  end
end