Class: RuboCop::Cop::GraphQL::UnusedArgument

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/graphql/unused_argument.rb

Overview

Arguments should either be listed explicitly or **rest should be in the resolve signature (and similar methods, such as #authorized?).

Examples:

# good

class SomeResolver < Resolvers::Base
  argument :arg1, String, required: true
  argument :user_id, String, required: true, loads: Types::UserType
  argument :post_id, String, loads: Types::PostType, as: :article
  argument :comment_ids, String, loads: Types::CommentType

  def authorized?(arg1:, user:, article:, comments:); end
  def resolve(arg1:, user:, article:, comments:); end
end

# good

class SomeResolver < Resolvers::Base
  argument :arg1, String, required: true
  argument :user_id, String, required: true, loads: Types::UserType
  argument :comment_ids, String, loads: Types::CommentType

  def resolve(arg1:, **rest); end
end

# good

class SomeResolver < Resolvers::Base
  type SomeType, null: false

  argument :arg1, String, required: true
  argument :arg2, String, required: true

  def resolve(args); end
end

# bad

class SomeResolver < Resolvers::Base
  type SomeType, null: false

  argument :arg1, String, required: true
  argument :arg2, String, required: true

  def authorized?; end
  def resolve(arg1:); end
end

# bad

class SomeResolver < Resolvers::Base
  type SomeType, null: false

  argument :arg1, String, required: true
  argument :arg2, String, required: true

  def resolve; end
end

Constant Summary collapse

MSG =
"Argument%<ending>s `%<unresolved_args>s` should be listed in the %<name>s signature."

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/graphql/unused_argument.rb', line 71

def on_class(node)
  resolve_method_node = find_resolve_method_node(node)
  return if resolve_method_node.nil? || ignore_arguments_type?(resolve_method_node)

  declared_arg_nodes = find_declared_arg_nodes(node)
  return unless declared_arg_nodes.any?

  unresolved_args = find_unresolved_args(resolve_method_node, declared_arg_nodes)
  register_offense(resolve_method_node, unresolved_args) if unresolved_args.any?
end