Class: GraphQL::StaticValidation::ArgumentNamesAreUnique

Inherits:
Object
  • Object
show all
Includes:
Message::MessageHelper
Defined in:
lib/graphql/static_validation/rules/argument_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
# File 'lib/graphql/static_validation/rules/argument_names_are_unique.rb', line 7

def validate(context)
  context.visitor[GraphQL::Language::Nodes::Field] << ->(node, parent) {
    validate_arguments(node, context)
  }

  context.visitor[GraphQL::Language::Nodes::Directive] << ->(node, parent) {
    validate_arguments(node, context)
  }
end

#validate_arguments(node, context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphql/static_validation/rules/argument_names_are_unique.rb', line 17

def validate_arguments(node, context)
  argument_defns = node.arguments
  if argument_defns.any?
    args_by_name = Hash.new { |h, k| h[k] = [] }
    argument_defns.each { |a| args_by_name[a.name] << a }
    args_by_name.each do |name, defns|
      if defns.size > 1
        context.errors << message("There can be only one argument named \"#{name}\"", defns, context: context)
      end
    end
  end
end