2019-05-12 17:10:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Resolvers
|
|
|
|
class NamespaceProjectsResolver < BaseResolver
|
|
|
|
argument :include_subgroups, GraphQL::BOOLEAN_TYPE,
|
|
|
|
required: false,
|
|
|
|
default_value: false,
|
|
|
|
description: 'Include also subgroup projects'
|
|
|
|
|
2020-09-04 23:08:31 -04:00
|
|
|
argument :search, GraphQL::STRING_TYPE,
|
|
|
|
required: false,
|
|
|
|
default_value: nil,
|
|
|
|
description: 'Search project with most similar names or paths'
|
|
|
|
|
|
|
|
argument :sort, Types::Projects::NamespaceProjectSortEnum,
|
|
|
|
required: false,
|
|
|
|
default_value: nil,
|
|
|
|
description: 'Sort projects by this criteria'
|
|
|
|
|
2019-05-12 17:10:46 -04:00
|
|
|
type Types::ProjectType, null: true
|
|
|
|
|
2020-09-04 23:08:31 -04:00
|
|
|
def resolve(include_subgroups:, sort:, search:)
|
2019-05-12 17:10:46 -04:00
|
|
|
# The namespace could have been loaded in batch by `BatchLoader`.
|
|
|
|
# At this point we need the `id` or the `full_path` of the namespace
|
|
|
|
# to query for projects, so make sure it's loaded and not `nil` before continuing.
|
2019-09-04 13:42:48 -04:00
|
|
|
namespace = object.respond_to?(:sync) ? object.sync : object
|
2019-05-12 17:10:46 -04:00
|
|
|
return Project.none if namespace.nil?
|
|
|
|
|
2020-09-04 23:08:31 -04:00
|
|
|
query = include_subgroups ? namespace.all_projects.with_route : namespace.projects.with_route
|
|
|
|
|
|
|
|
return query unless search.present?
|
|
|
|
|
|
|
|
if sort == :similarity
|
|
|
|
query.sorted_by_similarity_desc(search, include_in_select: true).merge(Project.search(search))
|
2019-05-12 17:10:46 -04:00
|
|
|
else
|
2020-09-04 23:08:31 -04:00
|
|
|
query.merge(Project.search(search))
|
2019-05-12 17:10:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.resolver_complexity(args, child_complexity:)
|
|
|
|
complexity = super
|
|
|
|
complexity + 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-07 05:09:51 -04:00
|
|
|
|
|
|
|
Resolvers::NamespaceProjectsResolver.prepend_if_ee('::EE::Resolvers::NamespaceProjectsResolver')
|