Module: GraphQL::StaticValidation::ArgumentsAreDefined

Defined in:
lib/graphql/static_validation/rules/arguments_are_defined.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
# File 'lib/graphql/static_validation/rules/arguments_are_defined.rb', line 5

def on_argument(node, parent)
  parent_defn = case parent
  when GraphQL::Language::Nodes::InputObject
    arg_defn = context.argument_definition
    if arg_defn.nil?
      nil
    else
      arg_ret_type = arg_defn.type.unwrap
      if arg_ret_type.kind.input_object?
        arg_ret_type
      else
        nil
      end
    end
  when GraphQL::Language::Nodes::Directive
    context.schema.directives[parent.name]
  when GraphQL::Language::Nodes::Field
    context.field_definition
  else
    raise "Unexpected argument parent: #{parent.class} (##{parent})"
  end

  if parent_defn && context.warden.arguments(parent_defn).any? { |arg| arg.name == node.name }
    super
  elsif parent_defn
    kind_of_node = node_type(parent)
    error_arg_name = parent_name(parent, parent_defn)
    add_error(GraphQL::StaticValidation::ArgumentsAreDefinedError.new(
      "#{kind_of_node} '#{error_arg_name}' doesn't accept argument '#{node.name}'",
      nodes: node,
      name: error_arg_name,
      type: kind_of_node,
      argument: node.name
    ))
  else
    # Some other weird error
    super
  end
end