From 7e9c479f7de77702622631cff2628a9c8dcbc627 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 19 Nov 2020 08:27:35 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-6-stable-ee --- rubocop/cop/graphql/resolver_type.rb | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 rubocop/cop/graphql/resolver_type.rb (limited to 'rubocop/cop/graphql') diff --git a/rubocop/cop/graphql/resolver_type.rb b/rubocop/cop/graphql/resolver_type.rb new file mode 100644 index 00000000000..1209c5dbc6b --- /dev/null +++ b/rubocop/cop/graphql/resolver_type.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +# This cop checks for missing GraphQL type annotations on resolvers +# +# @example +# +# # bad +# module Resolvers +# class NoTypeResolver < BaseResolver +# field :some_field, GraphQL::STRING_TYPE +# end +# end +# +# # good +# module Resolvers +# class WithTypeResolver < BaseResolver +# type MyType, null: true +# +# field :some_field, GraphQL::STRING_TYPE +# end +# end + +module RuboCop + module Cop + module Graphql + class ResolverType < RuboCop::Cop::Cop + MSG = 'Missing type annotation: Please add `type` DSL method call. ' \ + 'e.g: type UserType.connection_type, null: true' + + def_node_matcher :typed?, <<~PATTERN + (... (begin <(send nil? :type ...) ...>)) + PATTERN + + def on_class(node) + add_offense(node, location: :expression) if resolver?(node) && !typed?(node) + end + + private + + def resolver?(node) + node.loc.name.source.end_with?('Resolver') + end + end + end + end +end -- cgit v1.2.3