2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module Resolvers
|
|
|
|
class BaseResolver < GraphQL::Schema::Resolver
|
2020-01-01 13:55:28 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
2020-06-23 00:09:42 +05:30
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
argument_class ::Types::BaseArgument
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def self.requires_argument!
|
|
|
|
@requires_argument = true
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def self.calls_gitaly!
|
|
|
|
@calls_gitaly = true
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
# This is a flag to allow us to use `complexity_multiplier` to compute complexity for connection
|
|
|
|
# fields(see BaseField#connection_complexity_multiplier) in resolvers that do external connection pagination,
|
|
|
|
# thus disabling the default `connection` option(see self.field_options method above).
|
|
|
|
def self.calculate_ext_conn_complexity
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def self.field_options
|
2021-04-17 20:07:23 +05:30
|
|
|
extra_options = {
|
|
|
|
requires_argument: @requires_argument,
|
|
|
|
calls_gitaly: @calls_gitaly
|
|
|
|
}.compact
|
|
|
|
|
|
|
|
super.merge(extra_options)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def self.singular_type
|
|
|
|
return unless type
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
unwrapped = type.unwrap
|
|
|
|
|
|
|
|
%i[node_type relay_node_type of_type itself].reduce(nil) do |t, m|
|
|
|
|
t || unwrapped.try(m)
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def self.when_single(&block)
|
|
|
|
as_single << block
|
|
|
|
|
|
|
|
# Have we been called after defining the single version of this resolver?
|
2021-04-29 21:17:54 +05:30
|
|
|
@single.instance_exec(&block) if @single.present?
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.as_single
|
|
|
|
@as_single ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.single_definition_blocks
|
|
|
|
ancestors.flat_map { |klass| klass.try(:as_single) || [] }
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
single_definition_blocks.each do |definition|
|
|
|
|
klass.instance_exec(&definition)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
klass
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def self.last
|
2021-01-29 00:20:46 +05:30
|
|
|
parent = self
|
2021-04-29 21:17:54 +05:30
|
|
|
@last ||= Class.new(single) do
|
2021-01-29 00:20:46 +05:30
|
|
|
type parent.singular_type, null: true
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def select_result(results)
|
|
|
|
results&.last
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
define_singleton_method :to_s do
|
|
|
|
"#{parent}.last"
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def self.complexity
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def self.resolver_complexity(args, child_complexity:)
|
2019-07-31 22:56:46 +05:30
|
|
|
complexity = 1
|
|
|
|
complexity += 1 if args[:sort]
|
|
|
|
complexity += 5 if args[:search]
|
|
|
|
|
|
|
|
complexity
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2023-03-04 22:38:38 +05:30
|
|
|
# max_page_size limit) doubles the original complexity.
|
2019-07-31 22:56:46 +05:30
|
|
|
#
|
|
|
|
# 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
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def self.before_connection_authorization(&block)
|
|
|
|
@before_connection_authorization_block = block
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable Style/TrivialAccessors
|
|
|
|
def self.before_connection_authorization_block
|
|
|
|
@before_connection_authorization_block
|
|
|
|
end
|
|
|
|
# rubocop: enable Style/TrivialAccessors
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def offset_pagination(relation)
|
2021-03-11 19:13:27 +05:30
|
|
|
::Gitlab::Graphql::Pagination::OffsetPaginatedRelation.new(relation)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
override :object
|
|
|
|
def object
|
|
|
|
super.tap do |obj|
|
2020-03-13 15:44:24 +05:30
|
|
|
# If the field this resolver is used in is wrapped in a presenter, unwrap its subject
|
2022-06-21 17:19:12 +05:30
|
|
|
break obj.__subject__ if obj.is_a?(Gitlab::View::Presenter::Base)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def single?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
context[:current_user]
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
# Overridden in sub-classes (see .single, .last)
|
|
|
|
def select_result(results)
|
|
|
|
results
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
def self.authorization
|
|
|
|
@authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(try(:required_permissions))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.authorized?(object, context)
|
|
|
|
authorization.ok?(object, context[:current_user])
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|