Class: GraphQL::Query::LiteralInput

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/query/literal_input.rb

Overview

Turn query string values into something useful for query execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, incoming_value, variables) ⇒ LiteralInput

Returns a new instance of LiteralInput.



6
7
8
9
10
# File 'lib/graphql/query/literal_input.rb', line 6

def initialize(type, incoming_value, variables)
  @type = type
  @value = incoming_value
  @variables = variables
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/graphql/query/literal_input.rb', line 5

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/graphql/query/literal_input.rb', line 5

def value
  @value
end

#variablesObject (readonly)

Returns the value of attribute variables.



5
6
7
# File 'lib/graphql/query/literal_input.rb', line 5

def variables
  @variables
end

Class Method Details

.coerce(type, value, variables) ⇒ Object



41
42
43
44
# File 'lib/graphql/query/literal_input.rb', line 41

def self.coerce(type, value, variables)
  input = self.new(type, value, variables)
  input.graphql_value
end

.from_arguments(ast_arguments, argument_defns, variables) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/graphql/query/literal_input.rb', line 46

def self.from_arguments(ast_arguments, argument_defns, variables)
  values_hash = {}
  argument_defns.each do |arg_name, arg_defn|
    ast_arg = ast_arguments.find { |ast_arg| ast_arg.name == arg_name }
    arg_value = nil
    if ast_arg
      arg_value = coerce(arg_defn.type, ast_arg.value, variables)
    end
    if arg_value.nil?
      arg_value = arg_defn.default_value
    end
    values_hash[arg_name] = arg_value
  end
  GraphQL::Query::Arguments.new(values_hash)
end

Instance Method Details

#graphql_valueObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/graphql/query/literal_input.rb', line 12

def graphql_value
  if value.is_a?(GraphQL::Language::Nodes::VariableIdentifier)
    variables[value.name] # Already cleaned up with RubyInput
  elsif type.kind.input_object?
    input_values = {}
    inner_type = type.unwrap
    inner_type.input_fields.each do |arg_name, arg_defn|
      ast_arg = value.pairs.find { |ast_arg| ast_arg.name == arg_name }
      raw_value = resolve_argument_value(ast_arg, arg_defn, variables)
      reduced_value = coerce(arg_defn.type, raw_value, variables)
      input_values[arg_name] = reduced_value
    end
    input_values
  elsif type.kind.list?
    inner_type = type.of_type
    value.map { |item| coerce(inner_type, item, variables) }
  elsif type.kind.non_null?
    inner_type = type.of_type
    coerce(inner_type, value, variables)
  elsif type.kind.scalar?
    type.coerce_input!(value)
  elsif type.kind.enum?
    value_name = value.name # it's a Nodes::Enum
    type.coerce_input!(value_name)
  else
    raise "Unknown input #{value} of type #{type}"
  end
end