Repository navigation
Description
Describe the bug
With incremental support on, a @defer nested under a field of another @defer is delivered before the fragment that contains it. The child payload's path points at an object that the client has not received yet. The parent payload then delivers that object as {}.
The incremental delivery working group decided that a nested deferred fragment must not be delivered before its parent (graphql/defer-stream-wg#17). A client that applies payloads in order can lose the child's data: the parent's inner: {} arrives last and can replace what the child payload wrote.
Reproduced on 26.1 and 26.2, with enableEarlyIncrementalFieldExecution on and off, in every run.
To reproduce
Query, with slow resolving after 300 ms and every other field at once:
{
obj {
fast
... @defer(label: "parent") {
slow
inner { ... @defer(label: "child") { fast } }
}
}
}Output of the program below on 26.2:
graphql-java from graphql-java-26.2.jar
enableEarlyIncrementalFieldExecution=false
61 ms initial {obj={fast=obj-fast}}
73 ms child path=[obj, inner] {fast=inner-fast}
369 ms parent path=[obj] {slow=obj-slow, inner={}}
order [child, parent] child delivered before its parent
enableEarlyIncrementalFieldExecution=true
2 ms initial {obj={fast=obj-fast}}
2 ms child path=[obj, inner] {fast=inner-fast}
302 ms parent path=[obj] {slow=obj-slow, inner={}}
order [child, parent] child delivered before its parent
Expected: parent before child, or both in one payload with parent first.
Run it with JDK 17 or later, with graphql-java, java-dataloader 6.0.0, reactive-streams 1.0.3 and jspecify 1.0.0 on the class path:
java -cp graphql-java-26.2.jar:java-dataloader-6.0.0.jar:reactive-streams-1.0.3.jar:jspecify-1.0.0.jar NestedDeferOrderRepro.java
It exits with status 1 when the child is delivered first.
NestedDeferOrderRepro.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import graphql.ExecutionInput;
import graphql.GraphQL;
import graphql.incremental.DelayedIncrementalPartialResult;
import graphql.incremental.IncrementalExecutionResult;
import graphql.incremental.IncrementalPayload;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
/**
* A @defer nested under a field of another @defer is delivered before the fragment that contains it.
*
* The outer fragment ("parent") selects a field that resolves after 300 ms and a field "inner"
* whose only selection is the nested fragment ("child"). The child's field resolves at once.
* Expected: "parent" before "child", as graphql-js does and as the incremental delivery working
* group decided (https://github.com/graphql/defer-stream-wg/discussions/17).
*/
public class NestedDeferOrderRepro {
static final String SDL = String.join("\n",
"directive @defer(if: Boolean! = true, label: String) on FRAGMENT_SPREAD | INLINE_FRAGMENT",
"type Query { obj: Obj }",
"type Obj { fast: String slow: String inner: Inner }",
"type Inner { fast: String }");
static final String QUERY = String.join("\n",
"{",
" obj {",
" fast",
" ... @defer(label: \"parent\") {",
" slow",
" inner { ... @defer(label: \"child\") { fast } }",
" }",
" }",
"}");
static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(2);
static <T> CompletableFuture<T> after(long delayMs, T value) {
CompletableFuture<T> future = new CompletableFuture<>();
SCHEDULER.schedule(() -> future.complete(value), delayMs, TimeUnit.MILLISECONDS);
return future;
}
static GraphQL engine() {
Map<String, Object> empty = new HashMap<>();
DataFetcher<Object> emptyObject = env -> empty;
RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring()
.type("Query", t -> t.dataFetcher("obj", emptyObject))
.type("Obj", t -> t
.dataFetcher("fast", env -> "obj-fast")
.dataFetcher("slow", env -> after(300, "obj-slow"))
.dataFetcher("inner", emptyObject))
.type("Inner", t -> t.dataFetcher("fast", env -> "inner-fast"))
.build();
GraphQLSchema schema =
new SchemaGenerator().makeExecutableSchema(new SchemaParser().parse(SDL), wiring);
return GraphQL.newGraphQL(schema).build();
}
/** Runs the query and returns the deferred labels in the order they were delivered. */
static List<String> run(GraphQL graphQL, boolean eager) throws InterruptedException {
ExecutionInput input = ExecutionInput.newExecutionInput(QUERY)
.graphQLContext(builder -> GraphQL.unusualConfiguration(builder)
.incrementalSupport()
.enableIncrementalSupport(true)
.enableEarlyIncrementalFieldExecution(eager))
.build();
long start = System.nanoTime();
IncrementalExecutionResult result = (IncrementalExecutionResult) graphQL.execute(input);
System.out.printf(" %4d ms initial %s%n", (System.nanoTime() - start) / 1_000_000,
result.toSpecification().get("data"));
List<String> labels = new ArrayList<>();
CountDownLatch done = new CountDownLatch(1);
result.getIncrementalItemPublisher().subscribe(new Subscriber<DelayedIncrementalPartialResult>() {
Subscription subscription;
@Override public void onSubscribe(Subscription s) {
subscription = s;
s.request(1);
}
@Override public void onNext(DelayedIncrementalPartialResult part) {
long ms = (System.nanoTime() - start) / 1_000_000;
for (IncrementalPayload payload : part.getIncremental()) {
labels.add(payload.getLabel());
System.out.printf(" %4d ms %-7s path=%s %s%n", ms, payload.getLabel(),
payload.getPath(), payload.toSpecification().get("data"));
}
subscription.request(1);
}
@Override public void onError(Throwable t) {
t.printStackTrace();
done.countDown();
}
@Override public void onComplete() {
done.countDown();
}
});
if (!done.await(10, TimeUnit.SECONDS)) {
throw new IllegalStateException("the stream did not complete");
}
return labels;
}
public static void main(String[] args) throws Exception {
System.out.println("graphql-java from " + GraphQL.class.getProtectionDomain().getCodeSource().getLocation());
GraphQL graphQL = engine();
boolean childFirst = false;
for (boolean eager : new boolean[] {false, true}) {
System.out.println("enableEarlyIncrementalFieldExecution=" + eager);
List<String> labels = run(graphQL, eager);
boolean ok = labels.indexOf("parent") < labels.indexOf("child");
childFirst |= !ok;
System.out.println(" order " + labels + (ok ? " OK" : " child delivered before its parent"));
}
SCHEDULER.shutdown();
System.exit(childFirst ? 1 : 0);
}
}Context
#4497 proposes moving @defer to the newer incremental delivery draft. If that replaces the current implementation, it would be good for the new one to keep parents before nested fragments too. We currently work around this with an Instrumentation that holds a nested payload until the payloads around it are sent.
Activity