Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resolver_type.rb « graphql « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9fa768fd3e79a43a6f9fb5c760bae86ebf3e5e3 (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::Types::String
#     end
#   end
#
#   # good
#   module Resolvers
#     class WithTypeResolver < BaseResolver
#       type MyType, null: true
#
#       field :some_field, GraphQL::Types::String
#     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