Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ Async.CombinedBuilder<FieldValueInfo> getAsyncFieldValueInfo(

MergedSelectionSet fields = parameters.getFields();

executionContext.getIncrementalCallState().enqueue(deferredExecutionSupport.createCalls());
executionContext.getIncrementalCallState().enqueue(
deferredExecutionSupport.createCalls(),
parameters.getAlternativeCallContext()
);

// Only non-deferred fields should be considered for calculating the expected size of futures.
Async.CombinedBuilder<FieldValueInfo> futures = Async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public ResultPath getPath() {
return path;
}

public AlternativeCallContext getAlternativeCallContext() {
return alternativeCallContext;
}

private final ResultPath path;
private final List<Supplier<CompletableFuture<FieldWithExecutionResult>>> calls;
private final AlternativeCallContext alternativeCallContext;
Expand Down
107 changes: 88 additions & 19 deletions src/main/java/graphql/execution/incremental/IncrementalCallState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
import graphql.incremental.IncrementalPayload;
import graphql.util.InterThreadMemoizedSupplier;
import graphql.util.LockKit;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -29,36 +37,28 @@ public class IncrementalCallState {
private final Supplier<SingleSubscriberPublisher<DelayedIncrementalPartialResult>> publisher = createPublisher();
private final AtomicInteger pendingCalls = new AtomicInteger();
private final LockKit.ReentrantLock publisherLock = new LockKit.ReentrantLock();
private final Set<AlternativeCallContext> activeParents = new LinkedHashSet<>();
private final Map<IncrementalCall<? extends IncrementalPayload>, AlternativeCallContext> callParents = new LinkedHashMap<>();
private final Map<AlternativeCallContext, List<IncrementalCall<? extends IncrementalPayload>>> readyChildrenByParent = new LinkedHashMap<>();
private final Map<IncrementalCall<? extends IncrementalPayload>, IncrementalPayload> readyPayloads = new LinkedHashMap<>();

@SuppressWarnings("FutureReturnValueIgnored")
private void drainIncrementalCalls() {
IncrementalCall<? extends IncrementalPayload> incrementalCall = incrementalCalls.poll();

while (incrementalCall != null) {
incrementalCall.invoke()
IncrementalCall<? extends IncrementalPayload> currentCall = incrementalCall;
currentCall.invoke()
.whenComplete((payload, exception) -> {
if (exception != null) {
publisher.get().offerError(exception);
return;
}

// The assigment of `remainingCalls` and `publisher.offer` need to be synchronized to ensure
// `hasNext` is `false` precisely on the last event offered to the publisher.
publisherLock.lock();
final int remainingCalls;

try {
remainingCalls = pendingCalls.decrementAndGet();

DelayedIncrementalPartialResult executionResult = newIncrementalExecutionResult()
.incrementalItems(Collections.singletonList(payload))
.hasNext(remainingCalls != 0)
.build();

publisher.get().offer(executionResult);
} finally {
publisherLock.unlock();
}
// The assignment of `remainingCalls` and `publisher.offer` need to be synchronized to ensure
// `hasNext` is `false` precisely on the last event offered to the publisher, and that parent
// deferred fragments are always offered before their nested children.
final int remainingCalls = publisherLock.callLocked(() -> onCallCompleted(currentCall, payload));

if (remainingCalls == 0) {
publisher.get().noMoreData();
Expand All @@ -71,16 +71,85 @@ private void drainIncrementalCalls() {
}
}

private int onCallCompleted(IncrementalCall<? extends IncrementalPayload> completedCall, IncrementalPayload payload) {
AlternativeCallContext parentContext = callParents.remove(completedCall);
if (parentContext != null && activeParents.contains(parentContext)) {
readyChildrenByParent.computeIfAbsent(parentContext, key -> new ArrayList<>()).add(completedCall);
readyPayloads.put(completedCall, payload);
return pendingCalls.get();
}

readyPayloads.put(completedCall, payload);
Deque<IncrementalCall<? extends IncrementalPayload>> toEmit = new ArrayDeque<>();
toEmit.add(completedCall);

int remainingCalls = pendingCalls.get();
while (!toEmit.isEmpty()) {
IncrementalCall<? extends IncrementalPayload> nextCall = toEmit.poll();
IncrementalPayload nextPayload = readyPayloads.remove(nextCall);
if (nextPayload != null) {
remainingCalls = publishPayload(nextPayload);
}
releaseChildren(nextCall, toEmit);
}
return remainingCalls;
}

private int publishPayload(IncrementalPayload payload) {
int remainingCalls = pendingCalls.decrementAndGet();
DelayedIncrementalPartialResult executionResult = newIncrementalExecutionResult()
.incrementalItems(Collections.singletonList(payload))
.hasNext(remainingCalls != 0)
.build();
publisher.get().offer(executionResult);
return remainingCalls;
}

private void releaseChildren(
IncrementalCall<? extends IncrementalPayload> emittedCall,
Deque<IncrementalCall<? extends IncrementalPayload>> toEmit
) {
if (!(emittedCall instanceof DeferredFragmentCall)) {
return;
}
AlternativeCallContext ownContext = ((DeferredFragmentCall) emittedCall).getAlternativeCallContext();
activeParents.remove(ownContext);
List<IncrementalCall<? extends IncrementalPayload>> readyChildren = readyChildrenByParent.remove(ownContext);
if (readyChildren != null) {
toEmit.addAll(readyChildren);
}
}

public void enqueue(IncrementalCall<? extends IncrementalPayload> incrementalCall) {
enqueue(incrementalCall, null);
}

public void enqueue(
IncrementalCall<? extends IncrementalPayload> incrementalCall,
@Nullable AlternativeCallContext parentContext
) {
publisherLock.runLocked(() -> {
if (incrementalCall instanceof DeferredFragmentCall) {
activeParents.add(((DeferredFragmentCall) incrementalCall).getAlternativeCallContext());
}
if (parentContext != null && activeParents.contains(parentContext)) {
callParents.put(incrementalCall, parentContext);
}
incrementalCallsDetected.set(true);
incrementalCalls.offer(incrementalCall);
pendingCalls.incrementAndGet();
});
}

public void enqueue(Collection<IncrementalCall<? extends IncrementalPayload>> calls) {
calls.forEach(this::enqueue);
enqueue(calls, null);
}

public void enqueue(
Collection<IncrementalCall<? extends IncrementalPayload>> calls,
@Nullable AlternativeCallContext parentContext
) {
calls.forEach(call -> enqueue(call, parentContext));
}

public boolean getIncrementalCallsDetected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,190 @@ class DeferExecutionSupportIntegrationTest extends Specification {
]
}

def "nested defer is delivered after its parent defer even when nested resolves faster"() {
given:
def sdl = '''
type Query { obj: Obj }
type Obj { fast: String slow: String inner: Inner }
type Inner { fast: String }
'''
def runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("obj", { [:] } as DataFetcher)
)
.type(newTypeWiring("Obj")
.dataFetcher("fast", { "obj-fast" } as DataFetcher)
.dataFetcher("slow", resolve("obj-slow", 200))
.dataFetcher("inner", { [:] } as DataFetcher)
)
.type(newTypeWiring("Inner")
.dataFetcher("fast", { "inner-fast" } as DataFetcher)
)
.build()

def schema = TestUtil.schema(sdl, runtimeWiring)
.transform({ b -> b.additionalDirective(Directives.DeferDirective) })
def testGraphQL = GraphQL.newGraphQL(schema).build()

def query = '''
query {
obj {
fast
... @defer(label: "parent") {
slow
inner {
... @defer(label: "child") {
fast
}
}
}
}
}
'''

when:
def executionInput = ExecutionInput.newExecutionInput()
.graphQLContext([
(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT) : true,
(IncrementalExecutionContextKeys.ENABLE_EAGER_DEFER_START): eagerStart
])
.query(query)
.build()
IncrementalExecutionResult initialResult = testGraphQL.execute(executionInput) as IncrementalExecutionResult
def incrementalResults = getIncrementalResults(initialResult)

then:
initialResult.toSpecification() == [
data : [obj: [fast: "obj-fast"]],
hasNext: true
]
incrementalResults == [
[
hasNext : true,
incremental: [
[
label: "parent",
path : ["obj"],
data : [slow: "obj-slow", inner: [:]]
]
]
],
[
hasNext : false,
incremental: [
[
label: "child",
path : ["obj", "inner"],
data : [fast: "inner-fast"]
]
]
]
]

where:
eagerStart << [false, true]
}

def "deeply nested defers are delivered in parent-before-child order when inner resolves faster"() {
given:
def sdl = '''
type Query { obj: Obj }
type Obj { fast: String slowOuter: String mid: Mid }
type Mid { slowMid: String inner: Inner }
type Inner { fast: String }
'''
def runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("obj", { [:] } as DataFetcher)
)
.type(newTypeWiring("Obj")
.dataFetcher("fast", { "obj-fast" } as DataFetcher)
.dataFetcher("slowOuter", resolve("outer-val", 250))
.dataFetcher("mid", { [:] } as DataFetcher)
)
.type(newTypeWiring("Mid")
.dataFetcher("slowMid", resolve("mid-val", 100))
.dataFetcher("inner", { [:] } as DataFetcher)
)
.type(newTypeWiring("Inner")
.dataFetcher("fast", { "inner-val" } as DataFetcher)
)
.build()

def schema = TestUtil.schema(sdl, runtimeWiring)
.transform({ b -> b.additionalDirective(Directives.DeferDirective) })
def testGraphQL = GraphQL.newGraphQL(schema).build()

def query = '''
query {
obj {
fast
... @defer(label: "grandparent") {
slowOuter
mid {
... @defer(label: "parent") {
slowMid
inner {
... @defer(label: "child") {
fast
}
}
}
}
}
}
}
'''

when:
def executionInput = ExecutionInput.newExecutionInput()
.graphQLContext([
(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT) : true,
(IncrementalExecutionContextKeys.ENABLE_EAGER_DEFER_START): eagerStart
])
.query(query)
.build()
IncrementalExecutionResult initialResult = testGraphQL.execute(executionInput) as IncrementalExecutionResult
def incrementalResults = getIncrementalResults(initialResult)

then:
incrementalResults == [
[
hasNext : true,
incremental: [
[
label: "grandparent",
path : ["obj"],
data : [slowOuter: "outer-val", mid: [:]]
]
]
],
[
hasNext : true,
incremental: [
[
label: "parent",
path : ["obj", "mid"],
data : [slowMid: "mid-val", inner: [:]]
]
]
],
[
hasNext : false,
incremental: [
[
label: "child",
path : ["obj", "mid", "inner"],
data : [fast: "inner-val"]
]
]
]
]

where:
eagerStart << [false, true]
}


private ExecutionResult executeQuery(String query) {
return this.executeQuery(query, true, [:])
Expand Down
Loading