Class: RuboCop::Cop::GraphQL::FieldUniqueness

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

Overview

Detects duplicate field definitions within the same type

Examples:

# good

class UserType < BaseType
  field :name, String, null: true
  field :phone, String, null: true do
    argument :something, String, required: false
  end
end

# bad

class UserType < BaseType
  field :name, String, null: true
  field :phone, String, null: true
  field :phone, String, null: true do
    argument :something, String, required: false
  end
end

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from GraphQL::NodeUniqueness

#current_class_full_name

Instance Method Details

#on_class(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/graphql/field_uniqueness.rb', line 35

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

  field_names_by_class = Hash.new { |h, k| h[k] = Set.new }

  field_declarations(node).each do |current|
    current_class = current_class_full_name(current)
    field_names = field_names_by_class[current_class]
    current_field_name = field_name(current)

    unless field_names.include?(current_field_name)
      field_names.add(current_field_name)
      next
    end

    register_offense(current)
  end
end