summaryrefslogtreecommitdiff
path: root/app/graphql/resolvers/snippets_resolver.rb
blob: 7d18c9c6fea41ed02a430dc188c345e62098d139 (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
47
48
49
# frozen_string_literal: true
# rubocop:disable Graphql/ResolverType (inherited from ResolvesSnippets)

module Resolvers
  class SnippetsResolver < BaseResolver
    include ResolvesIds
    include ResolvesSnippets

    ERROR_MESSAGE = 'Filtering by both an author and a project is not supported'

    alias_method :user, :object

    argument :author_id, ::Types::GlobalIDType[::User],
              required: false,
              description: 'The ID of an author.'

    argument :project_id, ::Types::GlobalIDType[::Project],
              required: false,
              description: 'The ID of a project.'

    argument :type, Types::Snippets::TypeEnum,
              required: false,
              description: 'The type of snippet.'

    argument :explore,
              GraphQL::Types::Boolean,
              required: false,
              description: 'Explore personal snippets.'

    def resolve(**args)
      if args[:author_id].present? && args[:project_id].present?
        raise Gitlab::Graphql::Errors::ArgumentError, ERROR_MESSAGE
      end

      super
    end

    private

    def snippet_finder_params(args)
      # TODO: remove the type arguments when the compatibility layer is removed
      # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
      super
        .merge(author: resolve_ids(args[:author_id], ::Types::GlobalIDType[::User]),
               project: resolve_ids(args[:project_id], ::Types::GlobalIDType[::Project]),
               explore: args[:explore])
    end
  end
end