Class: RuboCop::Cop::GraphQL::ArgumentUniqueness

Inherits:
Base
  • Object
show all
Includes:
GraphQL::NodeUniqueness
Defined in:
lib/rubocop/cop/graphql/argument_uniqueness.rb

Overview

This cop detects duplicate argument definitions

Examples:

# good

class BanUser < BaseMutation
  argument :user_id, ID, required: true
end

# bad

class BanUser < BaseMutation
  argument :user_id, ID, required: true
  argument :user_id, ID, required: true
end

Constant Summary collapse

MSG =
"Argument names should only be defined once per block. "\
"Argument `%<current>s` is duplicated%<field_name>s."

Instance Method Summary collapse

Methods included from GraphQL::NodeUniqueness

#current_class_full_name

Instance Method Details

#on_class(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/graphql/argument_uniqueness.rb', line 28

def on_class(node)
  return if ::RuboCop::GraphQL::Class.new(node).nested?

  # { "MyClassName" => { "test_field" => <Set: {"field_arg_name"}> } }
  argument_names_by_field_by_class = Hash.new do |h, k|
    h[k] = Hash.new do |h1, k1|
      h1[k1] = Set.new
    end
  end

  argument_declarations(node).each do |current|
    current_field_name = field_name(current)
    current_argument_name = argument_name(current)
    class_name = current_class_full_name(current)

    argument_names = if current_field_name
                       argument_names_by_field_by_class[class_name][current_field_name]
                     else
                       argument_names_by_field_by_class[class_name][:root]
                     end

    unless argument_names.include?(current_argument_name)
      argument_names.add(current_argument_name)
      next
    end

    register_offense(current)
  end
end