Class: GraphQL::StaticValidation::VariableNamesAreUnique

Inherits:
Object
  • Object
show all
Includes:
Message::MessageHelper
Defined in:
lib/graphql/static_validation/rules/variable_names_are_unique.rb

Instance Method Summary collapse

Methods included from Message::MessageHelper

#message

Instance Method Details

#validate(context) ⇒ Object


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

def validate(context)
  context.visitor[GraphQL::Language::Nodes::OperationDefinition] << ->(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
          context.errors << message("There can only be one variable named \"#{name}\"", defns, context: context)
        end
      end
    end
  }
end