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

49 lines
904 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
attr_accessor :lookahead
end
def resolve(**args)
self.lookahead = args.delete(:lookahead)
resolve_with_lookahead(**args)
end
def apply_lookahead(query)
selection = node_selection
includes = preloads.each.flat_map do |name, requirements|
selection&.selects?(name) ? requirements : []
end
preloads = (unconditional_includes + includes).uniq
return query if preloads.empty?
query.preload(*preloads) # rubocop: disable CodeReuse/ActiveRecord
end
private
def unconditional_includes
[]
end
def preloads
{}
end
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