full_path_resolver.rb 704 字节
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module Resolvers
  module FullPathResolver
    extend ActiveSupport::Concern
6

7 8 9
    prepended do
      argument :full_path, GraphQL::ID_TYPE,
               required: true,
B
Brett Walker 已提交
10
               description: 'The full path of the project, group or namespace, e.g., "gitlab-org/gitlab-ce"'
N
Nick Thomas 已提交
11 12
    end

13
    def model_by_full_path(model, full_path)
B
Brett Walker 已提交
14
      BatchLoader::GraphQL.for(full_path).batch(key: model) do |full_paths, loader, args|
15
        # `with_route` avoids an N+1 calculating full_path
B
Brett Walker 已提交
16 17
        args[:key].where_full_path_in(full_paths).with_route.each do |model_instance|
          loader.call(model_instance.full_path, model_instance)
18
        end
19 20
      end
    end
N
Nick Thomas 已提交
21 22
  end
end