Class: RuboCop::GraphQL::Field

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, NodePattern::Macros
Defined in:
lib/rubocop/graphql/field.rb,
lib/rubocop/graphql/field/block.rb,
lib/rubocop/graphql/field/kwargs.rb

Defined Under Namespace

Classes: Block, Kwargs

Constant Summary collapse

RUBY_KEYWORDS =

These constants were extracted from graphql-ruby in lib/graphql/schema/member/has_fields.rb

%i[class module def undef begin rescue ensure end if unless then elsif else
case when while until for break next redo retry in do return yield super
self nil true false and or not alias defined? BEGIN END __LINE__
__FILE__].freeze
GRAPHQL_RUBY_KEYWORDS =
%i[context object raw_value].freeze
CONFLICT_FIELD_NAMES =
Set.new(
  GRAPHQL_RUBY_KEYWORDS + RUBY_KEYWORDS + Object.instance_methods
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Field

Returns a new instance of Field.



41
42
43
# File 'lib/rubocop/graphql/field.rb', line 41

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



39
40
41
# File 'lib/rubocop/graphql/field.rb', line 39

def node
  @node
end

Instance Method Details

#blockObject



72
73
74
# File 'lib/rubocop/graphql/field.rb', line 72

def block
  @block ||= Field::Block.new(@node.parent)
end

#descriptionObject



60
61
62
# File 'lib/rubocop/graphql/field.rb', line 60

def description
  @description ||= field_description(@node) || kwargs.description || block.description
end

#field_description(node) ⇒ Object



35
36
37
# File 'lib/rubocop/graphql/field.rb', line 35

def_node_matcher :field_description, <<~PATTERN
  (send nil? :field _ _ {(:str $_)|(:dstr $...)} ...)
PATTERN

#field_name(node) ⇒ Object



24
25
26
# File 'lib/rubocop/graphql/field.rb', line 24

def_node_matcher :field_name, <<~PATTERN
  (send nil? :field (:sym $_) ...)
PATTERN

#field_with_body_name(node) ⇒ Object



29
30
31
32
# File 'lib/rubocop/graphql/field.rb', line 29

def_node_matcher :field_with_body_name, <<~PATTERN
  (block
  (send nil? :field (:sym $_) ...)...)
PATTERN

#kwargsObject



68
69
70
# File 'lib/rubocop/graphql/field.rb', line 68

def kwargs
  @kwargs ||= Field::Kwargs.new(@node)
end

#nameObject



45
46
47
# File 'lib/rubocop/graphql/field.rb', line 45

def name
  @name ||= field_name(@node) || field_with_body_name(@node)
end

#resolver_method_nameObject



64
65
66
# File 'lib/rubocop/graphql/field.rb', line 64

def resolver_method_name
  kwargs.resolver_method_name || name
end

#schema_memberObject



76
77
78
# File 'lib/rubocop/graphql/field.rb', line 76

def schema_member
  @schema_member ||= SchemaMember.new(root_node)
end

#underscore_nameObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/graphql/field.rb', line 49

def underscore_name
  @underscore_name ||= begin
    word = name.to_s.dup
    word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    word.tr!("-", "_")
    word.downcase!
    word
  end
end