2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
module Resolvers
|
|
|
|
class BaseResolver < GraphQL::Schema::Resolver
|
2019-12-13 04:08:01 -05:00
|
|
|
extend ::Gitlab::Utils::Override
|
2020-05-21 14:08:27 -04:00
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
2020-10-01 05:09:54 -04:00
|
|
|
include ::Gitlab::Graphql::GlobalIDCompatibility
|
2019-12-13 04:08:01 -05:00
|
|
|
|
2020-10-13 23:08:32 -04:00
|
|
|
argument_class ::Types::BaseArgument
|
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
def self.singular_type
|
|
|
|
return unless type
|
2020-05-21 14:08:27 -04:00
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
unwrapped = type.unwrap
|
|
|
|
|
|
|
|
%i[node_type relay_node_type of_type itself].reduce(nil) do |t, m|
|
|
|
|
t || unwrapped.try(m)
|
|
|
|
end
|
|
|
|
end
|
2019-12-23 07:08:18 -05:00
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
def self.when_single(&block)
|
|
|
|
as_single << block
|
|
|
|
|
|
|
|
# Have we been called after defining the single version of this resolver?
|
|
|
|
if @single.present?
|
|
|
|
@single.instance_exec(&block)
|
|
|
|
end
|
|
|
|
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
|
2019-12-23 07:08:18 -05:00
|
|
|
end
|
2020-05-21 14:08:27 -04:00
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
single_definition_blocks.each do |definition|
|
|
|
|
klass.instance_exec(&definition)
|
2020-05-21 14:08:27 -04:00
|
|
|
end
|
2020-11-09 19:08:52 -05:00
|
|
|
|
|
|
|
klass
|
2019-01-30 14:00:30 -05:00
|
|
|
end
|
|
|
|
end
|
2019-05-06 17:24:19 -04:00
|
|
|
|
2019-11-14 07:06:30 -05:00
|
|
|
def self.last
|
2020-11-09 19:08:52 -05:00
|
|
|
parent = self
|
2020-05-21 14:08:27 -04:00
|
|
|
@last ||= Class.new(self.single) do
|
2020-11-09 19:08:52 -05:00
|
|
|
type parent.singular_type, null: true
|
|
|
|
|
2020-05-21 14:08:27 -04:00
|
|
|
def select_result(results)
|
|
|
|
results&.last
|
2019-12-23 07:08:18 -05:00
|
|
|
end
|
2020-11-09 19:08:52 -05:00
|
|
|
|
|
|
|
define_singleton_method :to_s do
|
|
|
|
"#{parent}.last"
|
|
|
|
end
|
2019-11-14 07:06:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-26 04:08:47 -05:00
|
|
|
def self.complexity
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2019-05-31 12:46:16 -04:00
|
|
|
def self.resolver_complexity(args, child_complexity:)
|
2019-05-06 17:24:19 -04:00
|
|
|
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
|
|
|
|
# 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
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
override :object
|
|
|
|
def object
|
|
|
|
super.tap do |obj|
|
2019-12-23 07:08:18 -05:00
|
|
|
# If the field this resolver is used in is wrapped in a presenter, unwrap its subject
|
2019-12-13 04:08:01 -05:00
|
|
|
break obj.subject if obj.is_a?(Gitlab::View::Presenter::Base)
|
|
|
|
end
|
|
|
|
end
|
2019-12-23 07:08:18 -05:00
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
# TODO: remove! This should never be necessary
|
|
|
|
# Remove as part of https://gitlab.com/gitlab-org/gitlab/-/issues/13984,
|
|
|
|
# since once we use that authorization approach, the object is guaranteed to
|
|
|
|
# be synchronized before any field.
|
2020-05-21 14:08:27 -04:00
|
|
|
def synchronized_object
|
|
|
|
strong_memoize(:synchronized_object) do
|
2020-11-09 19:08:52 -05:00
|
|
|
::Gitlab::Graphql::Lazy.force(object)
|
2020-05-21 14:08:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-23 07:08:18 -05:00
|
|
|
def single?
|
|
|
|
false
|
|
|
|
end
|
2020-01-22 19:08:53 -05:00
|
|
|
|
|
|
|
def current_user
|
|
|
|
context[:current_user]
|
|
|
|
end
|
2020-06-29 08:09:20 -04:00
|
|
|
|
|
|
|
# Overridden in sub-classes (see .single, .last)
|
|
|
|
def select_result(results)
|
|
|
|
results
|
|
|
|
end
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|
|
|
|
end
|