From fe6321b3b33f62c68616416f227a0859197d0585 Mon Sep 17 00:00:00 2001 From: Matt Fenelon Date: Mon, 20 May 2024 09:47:06 +0100 Subject: [PATCH] Add promise around resolved @resource.resolve will return either a promise or a real value --- lib/graphiti/scope.rb | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/graphiti/scope.rb b/lib/graphiti/scope.rb index 2ecee7db..61766734 100644 --- a/lib/graphiti/scope.rb +++ b/lib/graphiti/scope.rb @@ -34,15 +34,25 @@ def resolve payload[:results] = @resource.resolve(@object) payload[:results] end - resolved.compact! - assign_serializer(resolved) - yield resolved if block_given? - @opts[:after_resolve]&.call(resolved) - p = resolve_sideloads(resolved) unless @query.sideloads.empty? - if p.is_a?(Concurrent::Promises::Future) - p + resolved = Concurrent::Promises.fulfilled_future(resolved) unless resolved.is_a?(Concurrent::Promises::Future) + + p = resolved.then_on(self.class.global_thread_pool_executor) do |resolved| + resolved.compact! + assign_serializer(resolved) + yield resolved if block_given? + @opts[:after_resolve]&.call(resolved) + p = resolve_sideloads(resolved) unless @query.sideloads.empty? + if p.is_a?(Concurrent::Promises::Future) + p + else + resolved + end + end + + if @query.parents.empty? + p.value! else - resolved + p.flat end end end