Class: RuboCop::Cop::GraphQL::MethodShadowedByResolverMethod

Inherits:
Base
  • Object
show all
Includes:
GraphQL::NodePattern
Defined in:
lib/rubocop/cop/graphql/method_shadowed_by_resolver_method.rb

Overview

This cop detects method definitions that are never called because the field's effective resolver_method points somewhere else.

graphql-ruby only ever calls a single method on the type instance: the field's resolver_method (which is the resolver class's own method when resolver: is set, the explicit resolver_method: value when given, or the field name otherwise). Any other same-named method left on the type is unreachable:

  • When resolver: is set, the resolver class handles resolution entirely, so neither the field name, resolver_method:, nor method: (if also given) is ever dispatched to a method on the type.
  • When only resolver_method: is set, that name is the one actually called -- a leftover method matching the plain field name is never reached.

Examples:

# good

class Types::PostType < Types::BaseObject
  field :author, resolver: Resolvers::AuthorResolver
end

class Types::PostType < Types::BaseObject
  field :author, String, null: true, resolver_method: :fetch_author

  def fetch_author
    object.author
  end
end

# bad

class Types::PostType < Types::BaseObject
  field :author, resolver: Resolvers::AuthorResolver

  def author
    object.author
  end
end

class Types::PostType < Types::BaseObject
  field :author, String, null: true, resolver_method: :fetch_author

  def author
    object.author
  end

  def fetch_author
    object.author
  end
end

Constant Summary collapse

RESOLVER_MSG =
"Remove this method, it is never called: `resolver: %<resolver>s` " \
"resolves this field instead."
RESOLVER_METHOD_MSG =
"Remove this method, it is never called: " \
"`resolver_method: :%<resolver_method>s` is called instead."
RESTRICT_ON_SEND =
%i[field].freeze

Instance Method Summary collapse

Methods included from GraphQL::NodePattern

#argument?, #field?, #field_definition?, #field_definition_with_body?

Instance Method Details

#on_send(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/graphql/method_shadowed_by_resolver_method.rb', line 68

def on_send(node)
  return unless field_definition?(node)

  field = RuboCop::GraphQL::Field.new(node)

  if field.kwargs.resolver
    register_resolver_offenses(field)
  else
    register_resolver_method_offense(field)
  end
end