Module: GraphQL::StaticValidation::VariablesAreInputTypes

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

Instance Method Summary collapse

Instance Method Details

#on_variable_definition(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
# File 'lib/graphql/static_validation/rules/variables_are_input_types.rb', line 5

def on_variable_definition(node, parent)
  type_name = get_type_name(node.type)
  type = context.query.types.type(type_name)

  if type.nil?
    @all_possible_input_type_names ||= begin
      names = []
      context.types.all_types.each { |(t)|
        if t.kind.input?
          names << t.graphql_name
        end
      }
      names
    end
    add_error(GraphQL::StaticValidation::VariablesAreInputTypesError.new(
      "#{type_name} isn't a defined input type (on $#{node.name})#{context.did_you_mean_suggestion(type_name, @all_possible_input_type_names)}",
      nodes: node,
      name: node.name,
      type: type_name
    ))
  elsif !type.kind.input?
    add_error(GraphQL::StaticValidation::VariablesAreInputTypesError.new(
      "#{type.graphql_name} isn't a valid input type (on $#{node.name})",
      nodes: node,
      name: node.name,
      type: type_name
    ))
  end

  super
end