Class: RuboCop::Cop::GraphQL::MultipleFieldDefinitions

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, GraphQL::Heredoc, GraphQL::NodePattern
Defined in:
lib/rubocop/cop/graphql/multiple_field_definitions.rb

Overview

Checks whether fields with multiple definitions are grouped together.

Examples:

# good

class UserType < BaseType
  field :first_name, String, null: true
  field :first_name, Name, null: true

  def first_name
    object.contact_data.first_name
  end
end

# bad

class UserType < BaseType
  field :first_name, String, null: true

  def first_name
    object.contact_data.first_name
  end
  field :first_name, Name, null: true
end

Constant Summary collapse

RESTRICT_ON_SEND =
%i[field].freeze

Instance Method Summary collapse

Methods included from GraphQL::Heredoc

#heredoc?, #range_including_heredoc

Methods included from GraphQL::NodePattern

#argument?, #field?, #field_definition?, #field_definition_with_body?

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless field?(node)

  node = node.parent if field_definition_with_body?(node.parent)

  field = RuboCop::GraphQL::Field.new(node)

  multiple_definitions = multiple_definitions(field)

  return if multiple_definitions.size == 1 || node != multiple_definitions.last

  if has_ungrouped_definitions?(multiple_definitions)
    add_offense(node, message: MULTIPLE_DEFINITIONS_MSG) do |corrector|
      group_multiple_definitions(corrector, multiple_definitions)
    end
  end
end