Module: GraphQL::StaticValidation::ArgumentLiteralsAreCompatible

Defined in:
lib/graphql/static_validation/rules/argument_literals_are_compatible.rb

Instance Method Summary collapse

Instance Method Details

#on_argument(node, parent) ⇒ Object



5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/graphql/static_validation/rules/argument_literals_are_compatible.rb', line 5

def on_argument(node, parent)
  # Check the child arguments first;
  # don't add a new error if one of them reports an error
  super

  # Don't validate variables here
  if node.value.is_a?(GraphQL::Language::Nodes::VariableIdentifier)
    return
  end

  if @context.schema.error_bubbling || context.errors.none? { |err| err.path.take(@path.size) == @path }
    parent_defn = parent_definition(parent)

    if parent_defn && (arg_defn = parent_defn.arguments[node.name])
      validation_result = context.validate_literal(node.value, arg_defn.type)
      if !validation_result.valid?
        kind_of_node = node_type(parent)
        error_arg_name = parent_name(parent, parent_defn)
        string_value = if node.value == Float::INFINITY
          ""
        else
          " (#{GraphQL::Language::Printer.new.print(node.value)})"
        end

        problems = validation_result.problems
        first_problem = problems && problems.first
        if first_problem
          message = first_problem["message"]
          # This is some legacy stuff from when `CoercionError` was raised thru the stack
          if message
            coerce_extensions = first_problem["extensions"] || {
              "code" => "argumentLiteralsIncompatible"
            }
          end
        end

        error_options = {
          nodes: parent,
          type: kind_of_node,
          argument_name: node.name,
          argument: arg_defn,
          value: node.value
        }
        if coerce_extensions
          error_options[:coerce_extensions] = coerce_extensions
        end

        message ||= "Argument '#{node.name}' on #{kind_of_node} '#{error_arg_name}' has an invalid value#{string_value}. Expected type '#{arg_defn.type.to_type_signature}'."

        error = GraphQL::StaticValidation::ArgumentLiteralsAreCompatibleError.new(
          message,
          **error_options
        )

        add_error(error)
      end
    end
  end
end