summaryrefslogtreecommitdiff
path: root/rubocop/cop/graphql/resolver_type.rb
blob: 1209c5dbc6b84d39b45d18aa6e97bcdc15bcabf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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