debian-mirror-gitlab/app/graphql/resolvers/concerns/resolves_snippets.rb

57 lines
1.3 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
module ResolvesSnippets
extend ActiveSupport::Concern
included do
2021-01-29 00:20:46 +05:30
type Types::SnippetType.connection_type, null: false
2020-01-01 13:55:28 +05:30
2021-01-29 00:20:46 +05:30
argument :ids, [::Types::GlobalIDType[::Snippet]],
2020-01-01 13:55:28 +05:30
required: false,
2021-04-17 20:07:23 +05:30
description: 'Array of global snippet IDs. For example, `gid://gitlab/ProjectSnippet/1`.'
2020-01-01 13:55:28 +05:30
argument :visibility, Types::Snippets::VisibilityScopesEnum,
required: false,
2021-03-08 18:12:59 +05:30
description: 'The visibility of the snippet.'
2020-01-01 13:55:28 +05:30
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
2021-01-29 00:20:46 +05:30
def resolve_ids(ids, type = ::Types::GlobalIDType[::Snippet])
Array.wrap(ids).map do |id|
next unless id.present?
2020-01-01 13:55:28 +05:30
2021-01-29 00:20:46 +05:30
# 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
2020-01-01 13:55:28 +05:30
end
def options_by_type(type)
case type
when 'personal'
{ only_personal: true }
when 'project'
{ only_project: true }
else
{}
end
end
end