Class: RuboCop::Cop::GraphQL::FieldDefinitions

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

Overview

Checks consistency of field definitions

EnforcedStyle supports two modes:

‘group_definitions` : all field definitions should be grouped together.

‘define_resolver_after_definition` : if resolver method exists it should be defined right after the field definition.

Examples:

EnforcedStyle: group_definitions (default)

# good

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

  def first_name
    object.contact_data.first_name
  end

  def last_name
    object.contact_data.last_name
  end
end

EnforcedStyle: define_resolver_after_definition

# good

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

  def first_name
    object.contact_data.first_name
  end

  field :last_name, String, null: true

  def last_name
    object.contact_data.last_name
  end
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::Sorbet

#has_sorbet_signature?, #sorbet_signature, #sorbet_signature_for

Methods included from GraphQL::NodePattern

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

Instance Method Details

#field_kwargs(node) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rubocop/cop/graphql/field_definitions.rb', line 59

def_node_matcher :field_kwargs, <<~PATTERN
  (send nil? :field
    ...
    (hash
      $...
    )
  )
PATTERN

#on_class(node) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/graphql/field_definitions.rb', line 76

def on_class(node)
  return if style != :group_definitions

  schema_member = RuboCop::GraphQL::SchemaMember.new(node)

  if (body = schema_member.body)
    check_grouped_field_declarations(body)
  end
end

#on_module(node) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/rubocop/cop/graphql/field_definitions.rb', line 86

def on_module(node)
  return if style != :group_definitions

  schema_member = RuboCop::GraphQL::SchemaMember.new(node)

  if (body = schema_member.body)
    check_grouped_field_declarations(body)
  end
end

#on_send(node) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rubocop/cop/graphql/field_definitions.rb', line 68

def on_send(node)
  return if !field?(node) || style != :define_resolver_after_definition

  node = node.parent if field_definition_with_body?(node.parent)
  field = RuboCop::GraphQL::Field.new(node)
  check_resolver_is_defined_after_definition(field)
end