Class: Resolvers::BaseResolver

Inherits:
GraphQL::Schema::Resolver
  • Object
show all
Extended by:
Gitlab::Utils::Override
Includes:
Gitlab::Graphql::GlobalIDCompatibility, Gitlab::Utils::StrongMemoize
Defined in:
app/graphql/resolvers/base_resolver.rb

Direct Known Subclasses

Admin::Analytics::UsageTrends::MeasurementsResolver, AlertManagement::AlertResolver, AlertManagement::AlertStatusCountsResolver, AlertManagement::HttpIntegrationsResolver, AlertManagement::IntegrationsResolver, BaseIssuesResolver, BlobsResolver, BoardListIssuesResolver, BoardListsResolver, BoardsResolver, BranchCommitResolver, Ci::ConfigResolver, Ci::JobTokenScopeResolver, Ci::JobsResolver, Ci::PipelineJobArtifactsResolver, Ci::PipelineStagesResolver, Ci::ProjectPipelineCountsResolver, Ci::RunnerJobsResolver, Ci::RunnerPlatformsResolver, Ci::RunnerResolver, Ci::RunnerSetupResolver, Ci::RunnerStatusResolver, Ci::RunnersResolver, Ci::TemplateResolver, Ci::TestReportSummaryResolver, Ci::TestSuiteResolver, Clusters::AgentActivityEventsResolver, Clusters::AgentTokensResolver, Clusters::AgentsResolver, CommitPipelinesResolver, ContainerRepositoriesResolver, ContainerRepositoryTagsResolver, DesignManagement::DesignAtVersionResolver, DesignManagement::DesignResolver, DesignManagement::DesignsResolver, DesignManagement::Version::DesignAtVersionResolver, DesignManagement::Version::DesignsAtVersionResolver, DesignManagement::VersionInCollectionResolver, DesignManagement::VersionResolver, DesignManagement::VersionsResolver, EchoResolver, EnvironmentsResolver, ErrorTracking::SentryDetailedErrorResolver, ErrorTracking::SentryErrorCollectionResolver, ErrorTracking::SentryErrorStackTraceResolver, ErrorTracking::SentryErrorsResolver, GroupMembers::NotificationEmailResolver, GroupResolver, GroupsResolver, IssueStatusCountsResolver, Kas::AgentConfigurationsResolver, Kas::AgentConnectionsResolver, LabelsResolver, LastCommitResolver, MembersResolver, MergeRequestPipelinesResolver, MergeRequestsCountResolver, MergeRequestsResolver, MetadataResolver, Metrics::DashboardResolver, Metrics::Dashboards::AnnotationResolver, MilestonesResolver, NamespaceProjectsResolver, NamespaceResolver, PackageDetailsResolver, PackagePipelinesResolver, PackagesBaseResolver, PaginatedTreeResolver, ProjectJobsResolver, ProjectPipelineResolver, ProjectPipelineStatisticsResolver, ProjectPipelinesResolver, ProjectResolver, Projects::GrafanaIntegrationResolver, Projects::JiraProjectsResolver, Projects::ServicesResolver, Projects::SnippetsResolver, ProjectsResolver, RecentBoardsResolver, ReleaseMilestonesResolver, ReleaseResolver, ReleasesResolver, RepositoryBranchNamesResolver, Snippets::BlobsResolver, SnippetsResolver, Terraform::StatesResolver, TimelogResolver, TodoResolver, TopicsResolver, TreeResolver, UserDiscussionsCountResolver, UserNotesCountResolver, UserResolver, UserStarredProjectsResolver, Users::GroupCountResolver, Users::GroupsResolver, Users::ParticipantsResolver, Users::SnippetsResolver, UsersResolver, WorkItemResolver, WorkItems::TypesResolver

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from Gitlab::Graphql::GlobalIDCompatibility

#coerce_global_id_arguments!

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Class Method Details

.as_singleObject


45
46
47
# File 'app/graphql/resolvers/base_resolver.rb', line 45

def self.as_single
  @as_single ||= []
end

.authorizationObject


162
163
164
# File 'app/graphql/resolvers/base_resolver.rb', line 162

def self.authorization
  @authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(try(:required_permissions))
end

.authorized?(object, context) ⇒ Boolean

Returns:

  • (Boolean)

166
167
168
# File 'app/graphql/resolvers/base_resolver.rb', line 166

def self.authorized?(object, context)
  authorization.ok?(object, context[:current_user])
end

.before_connection_authorization(&block) ⇒ Object


127
128
129
# File 'app/graphql/resolvers/base_resolver.rb', line 127

def self.before_connection_authorization(&block)
  @before_connection_authorization_block = block
end

.before_connection_authorization_blockObject

rubocop: disable Style/TrivialAccessors


132
133
134
# File 'app/graphql/resolvers/base_resolver.rb', line 132

def self.before_connection_authorization_block
  @before_connection_authorization_block
end

.calls_gitaly!Object


15
16
17
# File 'app/graphql/resolvers/base_resolver.rb', line 15

def self.calls_gitaly!
  @calls_gitaly = true
end

.complexityObject


104
105
106
# File 'app/graphql/resolvers/base_resolver.rb', line 104

def self.complexity
  0
end

.complexity_multiplier(args) ⇒ Object


116
117
118
119
120
121
122
123
124
125
# File 'app/graphql/resolvers/base_resolver.rb', line 116

def self.complexity_multiplier(args)
  # When fetching many items, additional complexity is added to the field
  # depending on how many items is fetched. For each item we add 1% of the
  # original complexity - this means that loading 100 items (our default
  # maxp_age_size limit) doubles the original complexity.
  #
  # Complexity is not increased when searching by specific ID(s), because
  # complexity difference is minimal in this case.
  [args[:iid], args[:iids]].any? ? 0 : 0.01
end

.field_optionsObject


19
20
21
22
23
24
25
26
# File 'app/graphql/resolvers/base_resolver.rb', line 19

def self.field_options
  extra_options = {
    requires_argument: @requires_argument,
    calls_gitaly: @calls_gitaly
  }.compact

  super.merge(extra_options)
end

.lastObject


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/graphql/resolvers/base_resolver.rb', line 89

def self.last
  parent = self
  @last ||= Class.new(single) do
    type parent.singular_type, null: true

    def select_result(results)
      results&.last
    end

    define_singleton_method :to_s do
      "#{parent}.last"
    end
  end
end

.requires_argument!Object


11
12
13
# File 'app/graphql/resolvers/base_resolver.rb', line 11

def self.requires_argument!
  @requires_argument = true
end

.resolver_complexity(args, child_complexity:) ⇒ Object


108
109
110
111
112
113
114
# File 'app/graphql/resolvers/base_resolver.rb', line 108

def self.resolver_complexity(args, child_complexity:)
  complexity = 1
  complexity += 1 if args[:sort]
  complexity += 5 if args[:search]

  complexity
end

.singleObject


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
79
80
81
82
83
84
85
86
87
# File 'app/graphql/resolvers/base_resolver.rb', line 53

def self.single
  @single ||= begin
    parent = self
    klass = Class.new(self) do
      type parent.singular_type, null: true

      def ready?(**args)
        ready, early_return = super
        [ready, select_result(early_return)]
      end

      def resolve(**args)
        select_result(super)
      end

      def single?
        true
      end

      def select_result(results)
        results&.first
      end

      define_singleton_method :to_s do
        "#{parent}.single"
      end
    end

    single_definition_blocks.each do |definition|
      klass.instance_exec(&definition)
    end

    klass
  end
end

.single_definition_blocksObject


49
50
51
# File 'app/graphql/resolvers/base_resolver.rb', line 49

def self.single_definition_blocks
  ancestors.flat_map { |klass| klass.try(:as_single) || [] }
end

.singular_typeObject


28
29
30
31
32
33
34
35
36
# File 'app/graphql/resolvers/base_resolver.rb', line 28

def self.singular_type
  return unless type

  unwrapped = type.unwrap

  %i[node_type relay_node_type of_type itself].reduce(nil) do |t, m|
    t || unwrapped.try(m)
  end
end

.when_single(&block) ⇒ Object


38
39
40
41
42
43
# File 'app/graphql/resolvers/base_resolver.rb', line 38

def self.when_single(&block)
  as_single << block

  # Have we been called after defining the single version of this resolver?
  @single.instance_exec(&block) if @single.present?
end

Instance Method Details

#current_userObject


153
154
155
# File 'app/graphql/resolvers/base_resolver.rb', line 153

def current_user
  context[:current_user]
end

#objectObject


142
143
144
145
146
147
# File 'app/graphql/resolvers/base_resolver.rb', line 142

def object
  super.tap do |obj|
    # If the field this resolver is used in is wrapped in a presenter, unwrap its subject
    break obj.__subject__ if obj.is_a?(Gitlab::View::Presenter::Base)
  end
end

#offset_pagination(relation) ⇒ Object

rubocop: enable Style/TrivialAccessors


137
138
139
# File 'app/graphql/resolvers/base_resolver.rb', line 137

def offset_pagination(relation)
  ::Gitlab::Graphql::Pagination::OffsetPaginatedRelation.new(relation)
end

#select_result(results) ⇒ Object

Overridden in sub-classes (see .single, .last)


158
159
160
# File 'app/graphql/resolvers/base_resolver.rb', line 158

def select_result(results)
  results
end

#single?Boolean

Returns:

  • (Boolean)

149
150
151
# File 'app/graphql/resolvers/base_resolver.rb', line 149

def single?
  false
end