debian-mirror-gitlab/app/graphql/resolvers/labels_resolver.rb

56 lines
1.5 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
module Resolvers
class LabelsResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
authorize :read_label
type Types::LabelType.connection_type, null: true
2021-10-27 15:23:28 +05:30
argument :search_term, GraphQL::Types::String,
2021-03-11 19:13:27 +05:30
required: false,
2021-11-11 11:23:49 +05:30
description: 'Search term to find labels with.'
2021-03-11 19:13:27 +05:30
2021-10-27 15:23:28 +05:30
argument :include_ancestor_groups, GraphQL::Types::Boolean,
2021-03-11 19:13:27 +05:30
required: false,
description: 'Include labels from ancestor groups.',
default_value: false
2023-06-20 00:43:36 +05:30
before_connection_authorization do |nodes, current_user|
2023-07-09 08:55:56 +05:30
Preloaders::LabelsPreloader.new(nodes, current_user).preload_all
2023-06-20 00:43:36 +05:30
end
2021-03-11 19:13:27 +05:30
def resolve(**args)
return Label.none if parent.nil?
authorize!(parent)
# LabelsFinder uses `search` param, so we transform `search_term` into `search`
args[:search] = args.delete(:search_term)
2023-06-20 00:43:36 +05:30
# Optimization:
# Rely on the LabelsPreloader rather than the default parent record preloading in the
# finder because LabelsPreloader preloads more associations which are required for the
# permission check.
2021-03-11 19:13:27 +05:30
LabelsFinder.new(current_user, parent_param.merge(args)).execute
end
private
def parent
object.respond_to?(:sync) ? object.sync : object
end
def parent_param
key = case parent
when Group then :group
when Project then :project
else raise "Unexpected parent type: #{parent.class}"
end
{ "#{key}": parent }
end
end
end