debian-mirror-gitlab/app/graphql/resolvers/base_resolver.rb

93 lines
2.1 KiB
Ruby
Raw Normal View History

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
2020-01-01 13:55:28 +05:30
2019-07-07 11:18:12 +05:30
def self.single
@single ||= Class.new(self) do
2020-06-23 00:09:42 +05:30
def ready?(**args)
ready, early_return = super
[ready, select_result(early_return)]
end
2019-07-07 11:18:12 +05:30
def resolve(**args)
2020-06-23 00:09:42 +05:30
select_result(super)
2019-07-07 11:18:12 +05:30
end
2020-03-13 15:44:24 +05:30
def single?
true
end
2020-06-23 00:09:42 +05:30
def select_result(results)
results&.first
end
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
2020-06-23 00:09:42 +05:30
@last ||= Class.new(self.single) do
def select_result(results)
results&.last
2020-03-13 15:44:24 +05:30
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
# 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
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
2020-01-01 13:55:28 +05:30
break obj.subject if obj.is_a?(Gitlab::View::Presenter::Base)
end
end
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
def synchronized_object
strong_memoize(:synchronized_object) do
case object
when BatchLoader::GraphQL
object.sync
else
object
end
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
2018-11-08 19:23:39 +05:30
end
end