rubocop-graphql 1.7.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,19 @@ module RuboCop
6
6
  # This cop checks if a type (object, input, interface, scalar, union,
7
7
  # mutation, subscription, and resolver) has a description.
8
8
  #
9
+ # Only classes that actually declare a GraphQL type are checked: those whose
10
+ # superclass resolves to a GraphQL base (`Object`, `InputObject`, `Union`,
11
+ # `Enum`, `Scalar`, or anything ending in `Mutation`, `Subscription` or
12
+ # `Resolver`), plus modules that `include` an `*Interface` base. Plain Ruby
13
+ # classes that happen to live alongside types - error classes, analyzers,
14
+ # validators, loaders, generators - are skipped, so they no longer need to be
15
+ # silenced one by one.
16
+ #
17
+ # Two kinds of real type declarations are also skipped, because neither
18
+ # surfaces a description in the schema: abstract `Base*` types that other
19
+ # types inherit from, and (unless `IgnoreRootTypes` is disabled) the root
20
+ # operation types `Query`, `Mutation` and `Subscription`.
21
+ #
9
22
  # @example
10
23
  # # good
11
24
  #
@@ -20,44 +33,212 @@ module RuboCop
20
33
  # # ...
21
34
  # end
22
35
  #
36
+ # @example
37
+ # # good - not a GraphQL type, so no description is expected
38
+ #
39
+ # class TrackingInfoNotAvailable < StandardError; end
40
+ # class UserLoader < GraphQL::Batch::Loader; end
41
+ #
42
+ # @example
43
+ # # good - abstract base and root operation types carry no description
44
+ #
45
+ # class Types::BaseObject < GraphQL::Schema::Object; end
46
+ # class Types::Query < Types::BaseObject; end
47
+ #
48
+ # @example AdditionalTypeBases: [] (default)
49
+ # # good - `ApplicationType` is not a recognized GraphQL base, so this
50
+ # # class is not checked
51
+ #
52
+ # class Types::UserType < ApplicationType
53
+ # end
54
+ #
55
+ # @example AdditionalTypeBases: ['ApplicationType']
56
+ # # bad - `ApplicationType` is now treated as a GraphQL base
57
+ #
58
+ # class Types::UserType < ApplicationType
59
+ # end
60
+ #
61
+ # @example AdditionalTypeBaseSuffixes: [] (default)
62
+ # # good - only `Object` exactly, or with a `Base` prefix, is a recognized
63
+ # # object base, so a plain Ruby `ValueObject` is not mistaken for one
64
+ #
65
+ # class Money < ValueObject
66
+ # end
67
+ #
68
+ # @example AdditionalTypeBaseSuffixes: ['Object']
69
+ # # bad - every base whose name ends in `Object` is now treated as a GraphQL
70
+ # # base, which suits an app whose bases carry a domain prefix
71
+ #
72
+ # class Types::UserType < PermissionedObject
73
+ # end
74
+ #
23
75
  class ObjectDescription < Base
24
- include RuboCop::GraphQL::NodePattern
25
76
  include RuboCop::GraphQL::DescriptionMethod
26
77
 
27
78
  MSG = "Missing type description"
28
79
 
29
- # @!method interface?(node)
30
- def_node_matcher :interface?, <<~PATTERN
31
- (send nil? :include (const ...))
32
- PATTERN
80
+ # Base class names that mark a GraphQL type when they match exactly, or with
81
+ # a `Base` prefix (`GraphQL::Schema::Object`, `Types::BaseObject`).
82
+ # Ambiguous words are deliberately not matched as suffixes, so a plain Ruby
83
+ # `ValueObject` base is not mistaken for a GraphQL one; an app whose own bases
84
+ # do carry such a suffix opts in via `AdditionalTypeBaseSuffixes`. `Interface`
85
+ # is absent on purpose: graphql-ruby interfaces are modules, so a *class*
86
+ # inheriting an `*::Interface` constant is always some other abstract base.
87
+ EXACT_BASES = %w[Object InputObject Union Enum Scalar].freeze
88
+
89
+ # These read unambiguously as GraphQL even inside a longer name, so they are
90
+ # matched as suffixes (`RelayClassicMutation`, `Base::PermissionedMutation`).
91
+ # `AdditionalTypeBaseSuffixes` extends this list per app.
92
+ SUFFIX_BASES = %w[Mutation Subscription Resolver].freeze
93
+
94
+ # A base named exactly `Base` (`Resolvers::Base`) is only a GraphQL base when
95
+ # it sits in a namespace that says so.
96
+ GRAPHQL_NAMESPACES = %w[
97
+ Types Mutations Subscriptions Resolvers Inputs Interfaces Unions Enums Scalars
98
+ ].freeze
99
+
100
+ # GraphQL reserves these names for the root operation types.
101
+ ROOT_TYPE_NAMES = %w[Query Mutation Subscription].freeze
102
+
103
+ # `Base`, or `Base` followed by another word - the naming convention for the
104
+ # abstract types that other types inherit from (`BaseObject`, `BaseInterface`).
105
+ ABSTRACT_BASE_NAME = /\ABase(?:[A-Z]|\z)/
106
+
107
+ # Sorbet's `T` namespace is reserved, and `T::Enum` / `T::Struct` collide with
108
+ # the base names above.
109
+ SORBET_NAMESPACE = :T
33
110
 
34
111
  def on_class(node)
35
- return if child_nodes(node).find { |child_node| has_description?(child_node) }
112
+ return unless graphql_type_class?(node)
113
+ return if exempt_type?(node)
114
+ return if described_or_root_named?(node)
36
115
 
37
116
  add_offense(node.identifier)
38
117
  end
39
118
 
40
119
  def on_module(node)
41
- return if child_nodes(node).none? { |child_node| interface?(child_node) }
120
+ return if abstract_base?(node)
121
+ return unless undescribed_interface_module?(node)
42
122
 
43
- if child_nodes(node).none? { |child_node| has_description?(child_node) }
44
- add_offense(node.identifier)
45
- end
123
+ add_offense(node.identifier)
46
124
  end
47
125
 
48
126
  private
49
127
 
50
- def has_description?(node)
51
- description_method_call?(node)
128
+ # Abstract bases and the root operation types are real GraphQL declarations,
129
+ # but neither surfaces a description in the schema.
130
+ def exempt_type?(node)
131
+ return true if abstract_base?(node)
132
+
133
+ ignore_root_types? && ROOT_TYPE_NAMES.include?(type_name(node))
134
+ end
135
+
136
+ def abstract_base?(node)
137
+ ABSTRACT_BASE_NAME.match?(type_name(node))
138
+ end
139
+
140
+ def type_name(node)
141
+ node.identifier.short_name.to_s
142
+ end
143
+
144
+ def graphql_type_class?(node)
145
+ superclass = node.parent_class
146
+ return false if superclass.nil? || !superclass.const_type?
147
+ return false if sorbet_namespace?(superclass.namespace)
148
+
149
+ graphql_base?(superclass)
150
+ end
151
+
152
+ # `namespace` is a `cbase` node (not a `const`) for a top-level `::Foo`.
153
+ def sorbet_namespace?(namespace)
154
+ namespace&.const_type? && namespace.short_name == SORBET_NAMESPACE
155
+ end
156
+
157
+ def graphql_base?(const_node)
158
+ name = const_node.short_name.to_s
159
+ return true if additional_type_bases.include?(name)
160
+ return graphql_namespace?(const_node) if name == "Base"
161
+ return true if name.end_with?(*SUFFIX_BASES, *additional_type_base_suffixes)
162
+
163
+ EXACT_BASES.include?(name.delete_prefix("Base"))
164
+ end
165
+
166
+ def graphql_namespace?(const_node)
167
+ namespace = const_node.namespace
168
+ return false if namespace.nil? || !namespace.const_type?
169
+
170
+ GRAPHQL_NAMESPACES.include?(namespace.short_name.to_s)
171
+ end
172
+
173
+ # A method call with no explicit receiver (e.g. `description "..."`,
174
+ # `include Foo`) - the shape of a GraphQL type DSL declaration.
175
+ def bare_call?(node)
176
+ node.send_type? && node.receiver.nil?
177
+ end
178
+
179
+ # Single pass over the module body: an interface module both `include`s an
180
+ # `*Interface` base and (when compliant) declares a `description`.
181
+ def undescribed_interface_module?(node)
182
+ interface = false
183
+ described = false
184
+
185
+ body_nodes(node).each do |child|
186
+ described ||= description_method_call?(child)
187
+ interface ||= interface_include?(child)
188
+ end
189
+
190
+ interface && !described
191
+ end
192
+
193
+ def interface_include?(node)
194
+ return false unless bare_call?(node) && node.method?(:include)
195
+
196
+ arg = node.first_argument
197
+ return false unless arg&.const_type?
198
+
199
+ name = arg.short_name.to_s
200
+ return true if additional_type_bases.include?(name)
201
+
202
+ name.end_with?("Interface", *additional_type_base_suffixes)
52
203
  end
53
204
 
54
- def child_nodes(node)
55
- if node.body.instance_of? RuboCop::AST::Node
56
- node.body.child_nodes
57
- else
58
- node.child_nodes
205
+ # Single pass over the class body: a `description` satisfies the cop, and a
206
+ # `graphql_name "Query"` marks a root operation type declared under some
207
+ # other class name, which is exempt like a class named `Query` itself.
208
+ def described_or_root_named?(node)
209
+ body_nodes(node).any? do |child|
210
+ description_method_call?(child) || root_graphql_name?(child)
59
211
  end
60
212
  end
213
+
214
+ def root_graphql_name?(node)
215
+ return false unless ignore_root_types?
216
+ return false unless bare_call?(node) && node.method?(:graphql_name)
217
+
218
+ arg = node.first_argument
219
+ return false unless arg&.str_type?
220
+
221
+ ROOT_TYPE_NAMES.include?(arg.value)
222
+ end
223
+
224
+ def body_nodes(node)
225
+ body = node.body
226
+ return [] if body.nil?
227
+
228
+ body.begin_type? ? body.child_nodes : [body]
229
+ end
230
+
231
+ def ignore_root_types?
232
+ cop_config.fetch("IgnoreRootTypes", true)
233
+ end
234
+
235
+ def additional_type_bases
236
+ @additional_type_bases ||= Array(cop_config["AdditionalTypeBases"])
237
+ end
238
+
239
+ def additional_type_base_suffixes
240
+ @additional_type_base_suffixes ||= Array(cop_config["AdditionalTypeBaseSuffixes"])
241
+ end
61
242
  end
62
243
  end
63
244
  end
@@ -40,8 +40,8 @@ module RuboCop
40
40
  "section. "\
41
41
  "Field `%<current>s` should appear before `%<previous>s`."
42
42
 
43
- # @!method field_declarations(node)
44
- def_node_search :field_declarations, <<~PATTERN
43
+ # @!method field_nodes(node)
44
+ def_node_search :field_nodes, <<~PATTERN
45
45
  {
46
46
  (send nil? :field (:sym _) ...)
47
47
  (block
@@ -77,6 +77,14 @@ module RuboCop
77
77
  end
78
78
  end
79
79
 
80
+ def field_declarations(node)
81
+ field_nodes(node).reject { |field| block_field_send?(field) }
82
+ end
83
+
84
+ def block_field_send?(node)
85
+ node.parent&.block_type? && node.parent.send_node == node
86
+ end
87
+
80
88
  def direct_field_declarations(node)
81
89
  field_declarations(node).select { |field| direct_child_field?(field, node) }
82
90
  end
@@ -22,6 +22,8 @@ module RuboCop
22
22
  #
23
23
  class UnnecessaryFieldAlias < Base
24
24
  extend AutoCorrector
25
+ include RangeHelp
26
+ include RuboCop::GraphQL::Heredoc
25
27
  include RuboCop::GraphQL::NodePattern
26
28
 
27
29
  MSG = "Unnecessary :%<kwarg>s configured"
@@ -35,25 +37,80 @@ module RuboCop
35
37
  if (unnecessary_kwarg = validate_kwargs(field))
36
38
  message = format(self.class::MSG, kwarg: unnecessary_kwarg)
37
39
  add_offense(node, message: message) do |corrector|
40
+ next if node.each_descendant(:str, :dstr).any? { |str| heredoc?(str) }
41
+
38
42
  kwarg_node = node.last_argument.pairs.find do |pair|
39
43
  pair.key.value == unnecessary_kwarg.to_sym
40
44
  end
41
- corrector.remove_preceding(kwarg_node, 2)
42
- corrector.remove(kwarg_node)
45
+ remove_pair(corrector, kwarg_node)
43
46
  end
44
47
  end
45
48
  end
46
49
 
47
50
  private
48
51
 
49
- def validate_kwargs(field) # rubocop:disable Metrics/CyclomaticComplexity
52
+ def remove_pair(corrector, pair)
53
+ comment = comment_before_last_pair(pair)
54
+ comma = comma_before(comment) if comment
55
+
56
+ if comma
57
+ replaced = range_between(comma.begin_pos, end_of_line(pair))
58
+ corrector.replace(replaced, "#{code_after(pair)} #{comment.source}")
59
+ else
60
+ corrector.remove(range_to_remove(pair))
61
+ end
62
+ end
63
+
64
+ def range_to_remove(pair)
65
+ if (next_pair = pair.right_sibling)
66
+ range_between(pair.source_range.begin_pos, next_pair.source_range.begin_pos)
67
+ else
68
+ previous = pair.left_sibling || pair.parent.left_sibling
69
+ range_between(previous.source_range.end_pos, pair.source_range.end_pos)
70
+ end
71
+ end
72
+
73
+ def comment_before_last_pair(pair)
74
+ return if pair.right_sibling
75
+
76
+ previous = pair.left_sibling || pair.parent.left_sibling
77
+ comment = processed_source.comment_at_line(previous.last_line)
78
+ comment if comment && comment.source_range.end_pos <= pair.source_range.begin_pos
79
+ end
80
+
81
+ def code_after(pair)
82
+ own_comment = processed_source.comment_at_line(pair.last_line)
83
+ stop = own_comment ? own_comment.source_range.begin_pos : end_of_line(pair)
84
+
85
+ range_between(pair.source_range.end_pos, stop).source.rstrip
86
+ end
87
+
88
+ def end_of_line(node)
89
+ processed_source.buffer.line_range(node.last_line).end_pos
90
+ end
91
+
92
+ def comma_before(comment)
93
+ comment_begin = comment.source_range.begin_pos
94
+ line_begin = comment_begin - comment.loc.column
95
+ offset = processed_source.buffer.source[line_begin...comment_begin].rindex(/,\s*\z/)
96
+ return unless offset
97
+
98
+ range_between(line_begin + offset, line_begin + offset + 1)
99
+ end
100
+
101
+ def validate_kwargs(field)
50
102
  case field.name
51
103
  when field.kwargs.alias then "alias"
52
- when field.kwargs.method&.value&.value then "method"
104
+ when symbol_value(field.kwargs.method) then "method"
53
105
  when field.kwargs.resolver_method_name then "resolver_method"
54
- when field.kwargs.hash_key&.value&.value then "hash_key"
106
+ when symbol_value(field.kwargs.hash_key) then "hash_key"
55
107
  end
56
108
  end
109
+
110
+ def symbol_value(kwarg)
111
+ value = kwarg&.value
112
+ value.value if value&.sym_type?
113
+ end
57
114
  end
58
115
  end
59
116
  end
@@ -109,7 +109,7 @@ module RuboCop
109
109
  node.node_parts[0]
110
110
  end.to_set
111
111
  declared_args = declared_arg_nodes.map { |node| RuboCop::GraphQL::Argument.new(node) }
112
- declared_args.map(&method(:arg_name)).uniq.reject do |declared_arg_name|
112
+ declared_args.map(&:keyword).uniq.reject do |declared_arg_name|
113
113
  resolve_method_kwargs_names.include?(declared_arg_name)
114
114
  end
115
115
  end
@@ -147,26 +147,6 @@ module RuboCop
147
147
  node.source_range.end
148
148
  end
149
149
 
150
- def inferred_arg_name(name_as_string)
151
- case name_as_string
152
- when /_id$/
153
- name_as_string.sub(/_id$/, "").to_sym
154
- when /_ids$/
155
- name_as_string.sub(/_ids$/, "")
156
- .sub(/([^s])$/, "\\1s")
157
- .to_sym
158
- else
159
- name_as_string.to_sym
160
- end
161
- end
162
-
163
- def arg_name(declared_arg)
164
- return declared_arg.as if declared_arg.kwargs.as
165
- return inferred_arg_name(declared_arg.name.to_s) if declared_arg.kwargs.loads
166
-
167
- declared_arg.name
168
- end
169
-
170
150
  def scoped_node?(node)
171
151
  scope_changing_syntax?(node) || block_or_lambda?(node)
172
152
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Cops for the `GraphQL` department. The department's cops are
6
+ # registered for lazy loading and their files are loaded on demand.
7
+ module GraphQL
8
+ extend LazyLoader
9
+
10
+ register_cop :ArgumentDescription, "#{__dir__}/graphql/argument_description"
11
+ register_cop :ArgumentName, "#{__dir__}/graphql/argument_name"
12
+ register_cop :ArgumentUniqueness, "#{__dir__}/graphql/argument_uniqueness"
13
+ register_cop :ContextWriteInType, "#{__dir__}/graphql/context_write_in_type"
14
+ register_cop :DefaultForOptionalArgument, "#{__dir__}/graphql/default_for_optional_argument"
15
+ register_cop :DisallowedTypes, "#{__dir__}/graphql/disallowed_types"
16
+ register_cop :ExtractInputType, "#{__dir__}/graphql/extract_input_type"
17
+ register_cop :ExtractType, "#{__dir__}/graphql/extract_type"
18
+ register_cop :FieldDefinitions, "#{__dir__}/graphql/field_definitions"
19
+ register_cop :FieldDescription, "#{__dir__}/graphql/field_description"
20
+ register_cop :FieldHashKey, "#{__dir__}/graphql/field_hash_key"
21
+ register_cop :FieldMethod, "#{__dir__}/graphql/field_method"
22
+ register_cop :FieldName, "#{__dir__}/graphql/field_name"
23
+ register_cop :FieldUniqueness, "#{__dir__}/graphql/field_uniqueness"
24
+ register_cop :GraphqlName, "#{__dir__}/graphql/graphql_name"
25
+ register_cop :LegacyDsl, "#{__dir__}/graphql/legacy_dsl"
26
+ register_cop :MaxComplexitySchema, "#{__dir__}/graphql/max_complexity_schema"
27
+ register_cop :MaxDepthSchema, "#{__dir__}/graphql/max_depth_schema"
28
+ register_cop :MethodShadowedByResolverMethod, "#{__dir__}/graphql/method_shadowed_by_resolver_method"
29
+ register_cop :MultipleFieldDefinitions, "#{__dir__}/graphql/multiple_field_definitions"
30
+ register_cop :NotAuthorizedNodeType, "#{__dir__}/graphql/not_authorized_node_type"
31
+ register_cop :NullabilityMismatch, "#{__dir__}/graphql/nullability_mismatch"
32
+ register_cop :ResolverMethodLength, "#{__dir__}/graphql/resolver_method_length"
33
+ register_cop :ObjectDescription, "#{__dir__}/graphql/object_description"
34
+ register_cop :OrderedArguments, "#{__dir__}/graphql/ordered_arguments"
35
+ register_cop :OrderedFields, "#{__dir__}/graphql/ordered_fields"
36
+ register_cop :PrepareMethod, "#{__dir__}/graphql/prepare_method"
37
+ register_cop :UnusedArgument, "#{__dir__}/graphql/unused_argument"
38
+ register_cop :UnnecessaryArgumentCamelize, "#{__dir__}/graphql/unnecessary_argument_camelize"
39
+ register_cop :UnnecessaryFieldAlias, "#{__dir__}/graphql/unnecessary_field_alias"
40
+ register_cop :UnnecessaryFieldCamelize, "#{__dir__}/graphql/unnecessary_field_camelize"
41
+ register_cop :UselessMethodOption, "#{__dir__}/graphql/useless_method_option"
42
+ end
43
+ end
44
+ end
@@ -1,31 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "graphql/argument_description"
4
- require_relative "graphql/argument_name"
5
- require_relative "graphql/argument_uniqueness"
6
- require_relative "graphql/context_write_in_type"
7
- require_relative "graphql/extract_input_type"
8
- require_relative "graphql/extract_type"
9
- require_relative "graphql/field_definitions"
10
- require_relative "graphql/field_description"
11
- require_relative "graphql/field_hash_key"
12
- require_relative "graphql/field_method"
13
- require_relative "graphql/field_name"
14
- require_relative "graphql/field_uniqueness"
15
- require_relative "graphql/graphql_name"
16
- require_relative "graphql/legacy_dsl"
17
- require_relative "graphql/max_complexity_schema"
18
- require_relative "graphql/max_depth_schema"
19
- require_relative "graphql/method_shadowed_by_resolver_method"
20
- require_relative "graphql/multiple_field_definitions"
21
- require_relative "graphql/not_authorized_node_type"
22
- require_relative "graphql/resolver_method_length"
23
- require_relative "graphql/object_description"
24
- require_relative "graphql/ordered_arguments"
25
- require_relative "graphql/ordered_fields"
26
- require_relative "graphql/prepare_method"
27
- require_relative "graphql/unused_argument"
28
- require_relative "graphql/unnecessary_argument_camelize"
29
- require_relative "graphql/unnecessary_field_alias"
30
- require_relative "graphql/unnecessary_field_camelize"
31
- require_relative "graphql/useless_method_option"
3
+ # @deprecated This file is deprecated. Cops are registered for lazy loading in
4
+ # `rubocop/cop/graphql`; this file is kept for compatibility with code
5
+ # that requires it directly.
6
+ require_relative "graphql"
@@ -36,6 +36,16 @@ module RuboCop
36
36
  (pair (sym :camelize) ...)
37
37
  PATTERN
38
38
 
39
+ # @!method required_kwarg?(node)
40
+ def_node_matcher :required_kwarg?, <<~PATTERN
41
+ (pair (sym :required) ...)
42
+ PATTERN
43
+
44
+ # @!method default_value_kwarg?(node)
45
+ def_node_matcher :default_value_kwarg?, <<~PATTERN
46
+ (pair (sym :default_value) ...)
47
+ PATTERN
48
+
39
49
  def initialize(argument_node)
40
50
  @nodes = argument_kwargs(argument_node) || []
41
51
  end
@@ -55,6 +65,14 @@ module RuboCop
55
65
  def as
56
66
  @nodes.find { |kwarg| as_kwarg?(kwarg) }
57
67
  end
68
+
69
+ def required
70
+ @nodes.find { |kwarg| required_kwarg?(kwarg) }
71
+ end
72
+
73
+ def default_value
74
+ @nodes.find { |kwarg| default_value_kwarg?(kwarg) }
75
+ end
58
76
  end
59
77
  end
60
78
  end
@@ -7,7 +7,7 @@ module RuboCop
7
7
 
8
8
  # @!method argument_description(node)
9
9
  def_node_matcher :argument_description, <<~PATTERN
10
- (send nil? :argument _ _ (:str $_) ...)
10
+ (send nil? :argument _ _ {(:str $_)|(:dstr $...)} ...)
11
11
  PATTERN
12
12
 
13
13
  # @!method argument_name(node)
@@ -20,6 +20,11 @@ module RuboCop
20
20
  (pair (sym :as) (sym $_))
21
21
  PATTERN
22
22
 
23
+ # @!method argument_required(node)
24
+ def_node_matcher :argument_required, <<~PATTERN
25
+ (pair (sym :required) $_)
26
+ PATTERN
27
+
23
28
  attr_reader :node
24
29
 
25
30
  def initialize(node)
@@ -34,6 +39,29 @@ module RuboCop
34
39
  @as ||= argument_as(kwargs.as)
35
40
  end
36
41
 
42
+ # The keyword this argument is passed as to #resolve and friends. `as:` wins when both
43
+ # are given, matching graphql-ruby's `kwargs[:as] ||= inferred_arg_name`.
44
+ def keyword
45
+ return as if kwargs.as
46
+ return inferred_keyword if kwargs.loads
47
+
48
+ name
49
+ end
50
+
51
+ # `required: false` is the only value that lets graphql-ruby omit the keyword:
52
+ # `required: :nullable` still demands the argument be present, though it may be null.
53
+ def optional?
54
+ required_node = kwargs.required
55
+ return false unless required_node
56
+
57
+ argument_required(required_node)&.false_type? || false
58
+ end
59
+
60
+ # A configured default is always passed, even when the client omits the argument.
61
+ def default_value?
62
+ !kwargs.default_value.nil?
63
+ end
64
+
37
65
  def description
38
66
  @description ||= argument_description(@node) || kwargs.description || block.description
39
67
  end
@@ -45,6 +73,23 @@ module RuboCop
45
73
  def block
46
74
  @block ||= Argument::Block.new(@node.parent)
47
75
  end
76
+
77
+ private
78
+
79
+ # Mirrors graphql-ruby: a `loads:` argument named `foo_id` arrives as `foo:`, and one
80
+ # named `foo_ids` arrives as `foos:`.
81
+ def inferred_keyword
82
+ name_as_string = name.to_s
83
+
84
+ case name_as_string
85
+ when /_id$/
86
+ name_as_string.sub(/_id$/, "").to_sym
87
+ when /_ids$/
88
+ name_as_string.sub(/_ids$/, "").sub(/([^s])$/, "\\1s").to_sym
89
+ else
90
+ name
91
+ end
92
+ end
48
93
  end
49
94
  end
50
95
  end
@@ -21,6 +21,16 @@ module RuboCop
21
21
  (pair (sym :resolver) ...)
22
22
  PATTERN
23
23
 
24
+ # @!method mutation_kwarg?(node)
25
+ def_node_matcher :mutation_kwarg?, <<~PATTERN
26
+ (pair (sym :mutation) ...)
27
+ PATTERN
28
+
29
+ # @!method subscription_kwarg?(node)
30
+ def_node_matcher :subscription_kwarg?, <<~PATTERN
31
+ (pair (sym :subscription) ...)
32
+ PATTERN
33
+
24
34
  # @!method method_kwarg?(node)
25
35
  def_node_matcher :method_kwarg?, <<~PATTERN
26
36
  (pair (sym :method) ...)
@@ -59,6 +69,20 @@ module RuboCop
59
69
  @nodes.find { |kwarg| resolver_kwarg?(kwarg) }
60
70
  end
61
71
 
72
+ def mutation
73
+ @nodes.find { |kwarg| mutation_kwarg?(kwarg) }
74
+ end
75
+
76
+ def subscription
77
+ @nodes.find { |kwarg| subscription_kwarg?(kwarg) }
78
+ end
79
+
80
+ # graphql-ruby builds the field's resolver class out of any one of these
81
+ # options, and the field then inherits configuration from that class.
82
+ def resolver_class
83
+ resolver || mutation || subscription
84
+ end
85
+
62
86
  def method
63
87
  @nodes.find { |kwarg| method_kwarg?(kwarg) }
64
88
  end
@@ -73,6 +73,10 @@ module RuboCop
73
73
  @block ||= Field::Block.new(@node.parent)
74
74
  end
75
75
 
76
+ def last_non_block_argument
77
+ @node.arguments.reject(&:block_pass_type?).last
78
+ end
79
+
76
80
  def schema_member
77
81
  @schema_member ||= SchemaMember.new(root_node)
78
82
  end
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "1.7.0".freeze
3
+ VERSION = "1.9.0".freeze
4
4
  end
5
5
  end
@@ -24,4 +24,4 @@ require_relative "rubocop/graphql/field/block"
24
24
  require_relative "rubocop/graphql/field/kwargs"
25
25
  require_relative "rubocop/graphql/schema_member"
26
26
 
27
- require_relative "rubocop/cop/graphql_cops"
27
+ require_relative "rubocop/cop/graphql"