|
| 1 | +package graphql.execution; |
| 2 | + |
| 3 | +import graphql.Directives; |
| 4 | +import graphql.ExecutionResult; |
| 5 | +import graphql.Internal; |
| 6 | +import graphql.language.Field; |
| 7 | +import org.reactivestreams.Publisher; |
| 8 | +import org.reactivestreams.Subscriber; |
| 9 | +import org.reactivestreams.Subscription; |
| 10 | + |
| 11 | +import java.util.Deque; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.concurrent.ConcurrentLinkedDeque; |
| 15 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 16 | +import java.util.function.Supplier; |
| 17 | + |
| 18 | +/** |
| 19 | + * This provides support for @defer directives on fields that mean that results will be sent AFTER |
| 20 | + * the main result is sent via a Publisher stream. |
| 21 | + */ |
| 22 | +@Internal |
| 23 | +public class DeferSupport implements Publisher<ExecutionResult> { |
| 24 | + |
| 25 | + private final Deque<Supplier<CompletableFuture<ExecutionResult>>> deferredCalls = new ConcurrentLinkedDeque<>(); |
| 26 | + private final AtomicBoolean deferDetected = new AtomicBoolean(false); |
| 27 | + |
| 28 | + public boolean checkForDeferDirective(List<Field> currentField) { |
| 29 | + for (Field field : currentField) { |
| 30 | + if (field.getDirective(Directives.DeferDirective.getName()) != null) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + } |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void subscribe(Subscriber<? super ExecutionResult> subscriber) { |
| 39 | + subscriber.onSubscribe(emptySubscription()); |
| 40 | + boolean ok = drainDeferredCalls(subscriber); |
| 41 | + if (ok) { |
| 42 | + subscriber.onComplete(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private boolean drainDeferredCalls(Subscriber<? super ExecutionResult> subscriber) { |
| 47 | + while (!deferredCalls.isEmpty()) { |
| 48 | + Supplier<CompletableFuture<ExecutionResult>> deferredExecution = deferredCalls.pop(); |
| 49 | + try { |
| 50 | + CompletableFuture<ExecutionResult> future = deferredExecution.get(); |
| 51 | + // |
| 52 | + // this becomes a blocking call on the deferred resolveField value. This and the use of the queue |
| 53 | + // means we will resolve them in "encountered" order. We could do it async and hence in any order |
| 54 | + // |
| 55 | + ExecutionResult executionResult = future.get(); |
| 56 | + subscriber.onNext(executionResult); |
| 57 | + } catch (Exception e) { |
| 58 | + subscriber.onError(e); |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + public void enqueue(Supplier<CompletableFuture<ExecutionResult>> deferredCall) { |
| 67 | + deferDetected.set(true); |
| 68 | + deferredCalls.offer(deferredCall); |
| 69 | + } |
| 70 | + |
| 71 | + public boolean isDeferDetected() { |
| 72 | + return deferDetected.get(); |
| 73 | + } |
| 74 | + |
| 75 | + public Publisher<ExecutionResult> getPublisher() { |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + private Subscription emptySubscription() { |
| 80 | + return new Subscription() { |
| 81 | + @Override |
| 82 | + public void request(long n) { |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void cancel() { |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | +} |
0 commit comments