Class: RuboCop::Cop::GraphQL::GraphqlName

Inherits:
Base
  • Object
show all
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/graphql/graphql_name.rb

Overview

Checks consistency of graphql_name usage

EnforcedStyle supports two modes:

‘only_override` : types and mutations should have graphql_name configured only if it’s

different from the default name

‘required` : all types and mutations should have graphql_name configured

Examples:

EnforcedStyle: only_override (default)

# good

class UserType < BaseType
  graphql_name 'Viewer'
end

# bad

class UserType < BaseType
  graphql_name 'User'
end

EnforcedStyle: required

# good

class UserType < BaseType
  graphql_name 'User'
end

# bad

class UserType < BaseType
end

Constant Summary collapse

MISSING_NAME =
"graphql_name should be configured."
UNNEEDED_OVERRIDE =
"graphql_name should be specified only for overrides."

Instance Method Summary collapse

Instance Method Details

#class_name(node) ⇒ Object



49
50
51
# File 'lib/rubocop/cop/graphql/graphql_name.rb', line 49

def_node_matcher :class_name, <<~PATTERN
  (class (const _ $_) ...)
PATTERN

#graphql_name(node) ⇒ Object



44
45
46
# File 'lib/rubocop/cop/graphql/graphql_name.rb', line 44

def_node_matcher :graphql_name, <<~PATTERN
  `(send nil? :graphql_name (str $_))
PATTERN

#on_class(node) ⇒ Object



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

def on_class(node)
  specified_name = graphql_name(node)

  case style
  when :required
    add_offense(node, message: MISSING_NAME) if specified_name.nil?
  when :only_override
    default_graphql_name = class_name(node).to_s.sub(/Type\Z/, "")
    add_offense(node, message: UNNEEDED_OVERRIDE) if specified_name == default_graphql_name
  end
end