Class: RuboCop::GraphQL::Field::Kwargs

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

Instance Method Summary collapse

Constructor Details

#initialize(field_node) ⇒ Kwargs

Returns a new instance of Kwargs.



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

def initialize(field_node)
  @nodes = field_kwargs(field_node) || []
end

Instance Method Details

#alias ⇒ Object



90
91
92
# File 'lib/rubocop/graphql/field/kwargs.rb', line 90

def alias
  @alias ||= @nodes.map { |kwarg| alias_kwarg(kwarg) }.compact.first
end

#alias_kwarg(node) ⇒ Object



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

def_node_matcher :alias_kwarg, <<~PATTERN
  (pair (sym :alias) (sym $ _))
PATTERN

#camelize ⇒ Object



94
95
96
# File 'lib/rubocop/graphql/field/kwargs.rb', line 94

def camelize
  @nodes.find { |kwarg| camelize_kwarg?(kwarg) }
end

#camelize_kwarg?(node) ⇒ Object



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

def_node_matcher :camelize_kwarg?, <<~PATTERN
  (pair (sym :camelize) ...)
PATTERN

#description ⇒ Object



102
103
104
# File 'lib/rubocop/graphql/field/kwargs.rb', line 102

def description
  @nodes.find { |kwarg| description_kwarg?(kwarg) }
end

#description_kwarg?(node) ⇒ Object



50
51
52
# File 'lib/rubocop/graphql/field/kwargs.rb', line 50

def_node_matcher :description_kwarg?, <<~PATTERN
  (pair (sym :description) ...)
PATTERN

#field_kwargs(node) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/rubocop/graphql/field/kwargs.rb', line 10

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

#hash_key ⇒ Object



98
99
100
# File 'lib/rubocop/graphql/field/kwargs.rb', line 98

def hash_key
  @nodes.find { |kwarg| hash_key_kwarg?(kwarg) }
end

#hash_key_kwarg?(node) ⇒ Object



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

def_node_matcher :hash_key_kwarg?, <<~PATTERN
  (pair (sym :hash_key) ...)
PATTERN

#method ⇒ Object



86
87
88
# File 'lib/rubocop/graphql/field/kwargs.rb', line 86

def method
  @nodes.find { |kwarg| method_kwarg?(kwarg) }
end

#method_kwarg?(node) ⇒ Object



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

def_node_matcher :method_kwarg?, <<~PATTERN
  (pair (sym :method) ...)
PATTERN

#mutation ⇒ Object



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

def mutation
  @nodes.find { |kwarg| mutation_kwarg?(kwarg) }
end

#mutation_kwarg?(node) ⇒ Object



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

def_node_matcher :mutation_kwarg?, <<~PATTERN
  (pair (sym :mutation) ...)
PATTERN

#resolver ⇒ Object



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

def resolver
  @nodes.find { |kwarg| resolver_kwarg?(kwarg) }
end

#resolver_class ⇒ Object

graphql-ruby builds the field's resolver class out of any one of these options, and the field then inherits configuration from that class.



82
83
84
# File 'lib/rubocop/graphql/field/kwargs.rb', line 82

def resolver_class
  resolver || mutation || subscription
end

#resolver_kwarg?(node) ⇒ Object



20
21
22
# File 'lib/rubocop/graphql/field/kwargs.rb', line 20

def_node_matcher :resolver_kwarg?, <<~PATTERN
  (pair (sym :resolver) ...)
PATTERN

#resolver_method_name ⇒ Object



106
107
108
109
# File 'lib/rubocop/graphql/field/kwargs.rb', line 106

def resolver_method_name
  @resolver_method_name ||=
    @nodes.flat_map { |kwarg| resolver_method_option(kwarg) }.compact.first
end

#resolver_method_option(node) ⇒ Object



55
56
57
# File 'lib/rubocop/graphql/field/kwargs.rb', line 55

def_node_matcher :resolver_method_option, <<~PATTERN
  (pair (sym :resolver_method) (sym $...))
PATTERN

#subscription ⇒ Object



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

def subscription
  @nodes.find { |kwarg| subscription_kwarg?(kwarg) }
end

#subscription_kwarg?(node) ⇒ Object



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

def_node_matcher :subscription_kwarg?, <<~PATTERN
  (pair (sym :subscription) ...)
PATTERN