2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module Loaders
|
|
|
|
# Suitable for use to find resources that expose `where_full_path_in`,
|
|
|
|
# such as Project, Group, Namespace
|
2021-11-11 11:23:49 +05:30
|
|
|
# full path is always converted to lowercase for case-insensitive results
|
2020-06-23 00:09:42 +05:30
|
|
|
class FullPathModelLoader
|
|
|
|
attr_reader :model_class, :full_path
|
|
|
|
|
|
|
|
def initialize(model_class, full_path)
|
2021-04-29 21:17:54 +05:30
|
|
|
@model_class = model_class
|
2021-11-11 11:23:49 +05:30
|
|
|
@full_path = full_path.downcase
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def find
|
|
|
|
BatchLoader::GraphQL.for(full_path).batch(key: model_class) do |full_paths, loader, args|
|
2021-12-11 22:18:48 +05:30
|
|
|
scope = args[:key]
|
|
|
|
# this logic cannot be placed in the NamespaceResolver due to N+1
|
|
|
|
scope = scope.without_project_namespaces if scope == Namespace
|
2020-06-23 00:09:42 +05:30
|
|
|
# `with_route` avoids an N+1 calculating full_path
|
2021-12-11 22:18:48 +05:30
|
|
|
scope.where_full_path_in(full_paths).with_route.each do |model_instance|
|
2021-11-11 11:23:49 +05:30
|
|
|
loader.call(model_instance.full_path.downcase, model_instance)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|