class GraphQL::ScalarType

@api deprecated

Public Class Methods

new() click to toggle source
Calls superclass method GraphQL::BaseType::new
# File lib/graphql/scalar_type.rb, line 16
def initialize
  super
  self.coerce = NoOpCoerce
end

Public Instance Methods

coerce=(proc) click to toggle source
# File lib/graphql/scalar_type.rb, line 21
def coerce=(proc)
  self.coerce_input = proc
  self.coerce_result = proc
end
coerce_input=(coerce_input_fn) click to toggle source
# File lib/graphql/scalar_type.rb, line 26
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) click to toggle source
# File lib/graphql/scalar_type.rb, line 32
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) click to toggle source
# File lib/graphql/scalar_type.rb, line 40
def coerce_result=(coerce_result_fn)
  if !coerce_result_fn.nil?
    @coerce_result_proc = ensure_two_arg(coerce_result_fn, :coerce_result)
  end
end
kind() click to toggle source
# File lib/graphql/scalar_type.rb, line 46
def kind
  GraphQL::TypeKinds::SCALAR
end

Private Instance Methods

coerce_non_null_input(value, ctx) click to toggle source
# File lib/graphql/scalar_type.rb, line 56
def coerce_non_null_input(value, ctx)
  @coerce_input_proc.call(raw_coercion_input(value), ctx)
end
ensure_two_arg(callable, method_name) click to toggle source
# File lib/graphql/scalar_type.rb, line 52
def ensure_two_arg(callable, method_name)
  GraphQL::BackwardsCompatibility.wrap_arity(callable, from: 1, to: 2, name: "#{name}.#{method_name}(val, ctx)")
end
raw_coercion_input(value) click to toggle source
# File lib/graphql/scalar_type.rb, line 60
def raw_coercion_input(value)
  if value.is_a?(GraphQL::Language::Nodes::InputObject)
    value.to_h
  elsif value.is_a?(Array)
    value.map { |element| raw_coercion_input(element) }
  else
    value
  end
end
validate_non_null_input(value, ctx) click to toggle source
# File lib/graphql/scalar_type.rb, line 70
def validate_non_null_input(value, ctx)
  result = Query::InputValidationResult.new

  coerced_result = begin
    coerce_non_null_input(value, ctx)
  rescue GraphQL::CoercionError => err
    err
  end

  if value.is_a?(GraphQL::Language::Nodes::Enum) || coerced_result.nil?
    result.add_problem("Could not coerce value #{GraphQL::Language.serialize(value)} to #{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