Module: GraphQL::StaticValidation::VariableNamesAreUnique

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

Instance Method Summary collapse

Instance Method Details

#on_operation_definition(node, parent) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphql/static_validation/rules/variable_names_are_unique.rb', line 5

def on_operation_definition(node, parent)
  var_defns = node.variables
  if var_defns.any?
    vars_by_name = Hash.new { |h, k| h[k] = [] }
    var_defns.each { |v| vars_by_name[v.name] << v }
    vars_by_name.each do |name, defns|
      if defns.size > 1
        add_error(GraphQL::StaticValidation::VariableNamesAreUniqueError.new(
          "There can only be one variable named \"#{name}\"",
          nodes: defns,
          name: name
        ))
      end
    end
  end
  super
end