debian-mirror-gitlab/app/graphql/resolvers/concerns/looks_ahead.rb

53 lines
969 B
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
module LooksAhead
extend ActiveSupport::Concern
included do
2021-01-29 00:20:46 +05:30
extras [:lookahead]
2020-06-23 00:09:42 +05:30
attr_accessor :lookahead
end
def resolve(**args)
self.lookahead = args.delete(:lookahead)
resolve_with_lookahead(**args)
end
def apply_lookahead(query)
2021-04-29 21:17:54 +05:30
all_preloads = (unconditional_includes + filtered_preloads).uniq
2020-06-23 00:09:42 +05:30
2021-03-08 18:12:59 +05:30
return query if all_preloads.empty?
2020-06-23 00:09:42 +05:30
2021-03-08 18:12:59 +05:30
query.preload(*all_preloads) # rubocop: disable CodeReuse/ActiveRecord
2020-06-23 00:09:42 +05:30
end
private
def unconditional_includes
[]
end
def preloads
{}
end
2021-04-29 21:17:54 +05:30
def filtered_preloads
selection = node_selection
preloads.each.flat_map do |name, requirements|
selection&.selects?(name) ? requirements : []
end
end
2020-06-23 00:09:42 +05:30
def node_selection
return unless lookahead
if lookahead.selects?(:nodes)
lookahead.selection(:nodes)
elsif lookahead.selects?(:edges)
2020-11-24 15:15:51 +05:30
lookahead.selection(:edges).selection(:node)
2020-06-23 00:09:42 +05:30
end
end
end