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

resolves_snippets.rb « concerns « resolvers « graphql « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0bc38188b9a837c71d76ed8c23dffde090b8b36b (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
50
51
52
53
54
55
56
# frozen_string_literal: true

module ResolvesSnippets
  extend ActiveSupport::Concern

  included do
    type Types::SnippetType.connection_type, null: false

    argument :ids, [::Types::GlobalIDType[::Snippet]],
             required: false,
             description: 'Array of global snippet ids, e.g., "gid://gitlab/ProjectSnippet/1".'

    argument :visibility, Types::Snippets::VisibilityScopesEnum,
             required: false,
             description: 'The visibility of the snippet.'
  end

  def resolve(**args)
    resolve_snippets(args)
  end

  private

  def resolve_snippets(args)
    SnippetsFinder.new(context[:current_user], snippet_finder_params(args)).execute
  end

  def snippet_finder_params(args)
    {
      ids: resolve_ids(args[:ids]),
      scope: args[:visibility]
    }.merge(options_by_type(args[:type]))
  end

  def resolve_ids(ids, type = ::Types::GlobalIDType[::Snippet])
    Array.wrap(ids).map do |id|
      next unless id.present?

      # TODO: remove this line when the compatibility layer is removed
      # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
      id = type.coerce_isolated_input(id)
      id.model_id
    end.compact
  end

  def options_by_type(type)
    case type
    when 'personal'
      { only_personal: true }
    when 'project'
      { only_project: true }
    else
      {}
    end
  end
end