Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resource_type.rb « catalog « ci « types « graphql « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b90ae111a3d999b1bb1cb4881223e9e20d0d8cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

module Types
  module Ci
    module Catalog
      # rubocop: disable Graphql/AuthorizeTypes
      class ResourceType < BaseObject
        graphql_name 'CiCatalogResource'

        connection_type_class(Types::CountableConnectionType)

        field :id, GraphQL::Types::ID, null: false, description: 'ID of the catalog resource.',
          alpha: { milestone: '15.11' }

        field :name, GraphQL::Types::String, null: true, description: 'Name of the catalog resource.',
          alpha: { milestone: '15.11' }

        field :description, GraphQL::Types::String, null: true, description: 'Description of the catalog resource.',
          alpha: { milestone: '15.11' }

        field :icon, GraphQL::Types::String, null: true, description: 'Icon for the catalog resource.',
          method: :avatar_path, alpha: { milestone: '15.11' }

        field :web_path, GraphQL::Types::String, null: true, description: 'Web path of the catalog resource.',
          alpha: { milestone: '16.1' }

        field :versions, Types::ReleaseType.connection_type, null: true,
          description: 'Versions of the catalog resource.',
          resolver: Resolvers::ReleasesResolver,
          alpha: { milestone: '16.1' }

        field :star_count, GraphQL::Types::Int, null: false,
          description: 'Number of times the catalog resource has been starred.',
          alpha: { milestone: '16.1' }

        field :forks_count, GraphQL::Types::Int, null: false, calls_gitaly: true,
          description: 'Number of times the catalog resource has been forked.',
          alpha: { milestone: '16.1' }

        field :root_namespace, Types::NamespaceType, null: true,
          description: 'Root namespace of the catalog resource.',
          alpha: { milestone: '16.1' }

        def web_path
          ::Gitlab::Routing.url_helpers.project_path(object.project)
        end

        def forks_count
          BatchLoader::GraphQL.wrap(object.forks_count)
        end

        def root_namespace
          BatchLoader::GraphQL.for(object.project_id).batch do |project_ids, loader|
            projects = Project.id_in(project_ids)

            # This preloader uses traversal_ids to obtain Group-type root namespaces.
            # It also preloads each project's immediate parent namespace, which effectively
            # preloads the User-type root namespaces since they cannot be nested (parent == root).
            Preloaders::ProjectRootAncestorPreloader.new(projects, :group).execute
            root_namespaces = projects.map(&:root_ancestor)

            # NamespaceType requires the `:read_namespace` ability. We must preload the policy for
            # Group-type namespaces to avoid N+1 queries caused by the authorization requests.
            group_root_namespaces = root_namespaces.select { |n| n.type == ::Group.sti_name }
            Preloaders::GroupPolicyPreloader.new(group_root_namespaces, current_user).execute

            # For User-type namespaces, the authorization request requires preloading the owner objects.
            user_root_namespaces = root_namespaces.select { |n| n.type == ::Namespaces::UserNamespace.sti_name }
            ActiveRecord::Associations::Preloader.new(records: user_root_namespaces, associations: :owner).call

            projects.each { |project| loader.call(project.id, project.root_ancestor) }
          end
        end
      end
      # rubocop: enable Graphql/AuthorizeTypes
    end
  end
end