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

Class Method Summary collapse

Class Method Details

.coerce(type, ast_node, variables) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql/query/literal_input.rb', line 6

def self.coerce(type, ast_node, variables)
  case ast_node
  when nil
    nil
  when Language::Nodes::NullValue
    nil
  when Language::Nodes::VariableIdentifier
    variables[ast_node.name]
  else
    case type
    when GraphQL::ScalarType
      type.coerce_input(ast_node, variables.context)
    when GraphQL::EnumType
      type.coerce_input(ast_node.name, variables.context)
    when GraphQL::NonNullType
      LiteralInput.coerce(type.of_type, ast_node, variables)
    when GraphQL::ListType
      if ast_node.is_a?(Array)
        ast_node.map { |element_ast| LiteralInput.coerce(type.of_type, element_ast, variables) }
      else
        [LiteralInput.coerce(type.of_type, ast_node, variables)]
      end
    when GraphQL::InputObjectType
      from_arguments(ast_node.arguments, type.arguments, variables)
    end
  end
end

.defaults_for(argument_defns) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/graphql/query/literal_input.rb', line 34

def self.defaults_for(argument_defns)
  if argument_defns.values.none?(&:default_value?)
    GraphQL::Query::Arguments::NO_ARGS
  else
    from_arguments([], argument_defns, nil)
  end
end

.from_arguments(ast_arguments, argument_defns, variables) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/graphql/query/literal_input.rb', line 42

def self.from_arguments(ast_arguments, argument_defns, variables)
  # Variables is nil when making .defaults_for
  context = variables ? variables.context : nil
  values_hash = {}
  indexed_arguments = ast_arguments.each_with_object({}) { |a, memo| memo[a.name] = a }

  argument_defns.each do |arg_name, arg_defn|
    ast_arg = indexed_arguments[arg_name]
    # First, check the argument in the AST.
    # If the value is a variable,
    # only add a value if the variable is actually present.
    # Otherwise, coerce the value in the AST, prepare the value and add it.
    if ast_arg
      value_is_a_variable = ast_arg.value.is_a?(GraphQL::Language::Nodes::VariableIdentifier)

      if (!value_is_a_variable || (value_is_a_variable && variables.key?(ast_arg.value.name)))

        value = coerce(arg_defn.type, ast_arg.value, variables)
        value = arg_defn.prepare(value, context)

        if value.is_a?(GraphQL::ExecutionError)
          value.ast_node = ast_arg
          raise value
        end

        values_hash[ast_arg.name] = value
      end
    end

    # Then, the definition for a default value.
    # If the definition has a default value and
    # a value wasn't provided from the AST,
    # then add the default value.
    if arg_defn.default_value? && !values_hash.key?(arg_name)
      value = arg_defn.default_value
      # `context` isn't present when pre-calculating defaults
      if context
        value = arg_defn.prepare(value, context)
        if value.is_a?(GraphQL::ExecutionError)
          value.ast_node = ast_arg
          raise value
        end
      end
      values_hash[arg_name] = value
    end
  end

  GraphQL::Query::Arguments.new(values_hash, argument_definitions: argument_defns)
end