Title: Nested @defer fragment is delivered before the fragment that contains it · Issue #4567 · graphql-java/graphql-java · GitHub
Open Graph Title: Nested @defer fragment is delivered before the fragment that contains it · Issue #4567 · graphql-java/graphql-java
X Title: Nested @defer fragment is delivered before the fragment that contains it · Issue #4567 · graphql-java/graphql-java
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 paren...
Open Graph 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 th...
X 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 tha...
Opengraph URL: https://github.com/graphql-java/graphql-java/issues/4567
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Nested @defer fragment is delivered before the fragment that contains it","articleBody":"### Describe the bug\n\nWith 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 `{}`.\n\nThe incremental delivery working group decided that a nested deferred fragment must not be delivered before its parent ([graphql/defer-stream-wg#17](https://github.com/graphql/defer-stream-wg/discussions/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.\n\nReproduced on 26.1 and 26.2, with `enableEarlyIncrementalFieldExecution` on and off, in every run.\n\n### To reproduce\n\nQuery, with `slow` resolving after 300 ms and every other field at once:\n\n```graphql\n{\n obj {\n fast\n ... @defer(label: \"parent\") {\n slow\n inner { ... @defer(label: \"child\") { fast } }\n }\n }\n}\n```\n\nOutput of the program below on 26.2:\n\n```\ngraphql-java from graphql-java-26.2.jar\nenableEarlyIncrementalFieldExecution=false\n 61 ms initial {obj={fast=obj-fast}}\n 73 ms child path=[obj, inner] {fast=inner-fast}\n 369 ms parent path=[obj] {slow=obj-slow, inner={}}\n order [child, parent] child delivered before its parent\nenableEarlyIncrementalFieldExecution=true\n 2 ms initial {obj={fast=obj-fast}}\n 2 ms child path=[obj, inner] {fast=inner-fast}\n 302 ms parent path=[obj] {slow=obj-slow, inner={}}\n order [child, parent] child delivered before its parent\n```\n\nExpected: `parent` before `child`, or both in one payload with `parent` first.\n\nRun 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:\n\n```\njava -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\n```\n\nIt exits with status 1 when the child is delivered first.\n\n\u003cdetails>\u003csummary>NestedDeferOrderRepro.java\u003c/summary>\n\n```java\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\nimport java.util.concurrent.CompletableFuture;\nimport java.util.concurrent.CountDownLatch;\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.ScheduledExecutorService;\nimport java.util.concurrent.TimeUnit;\n\nimport graphql.ExecutionInput;\nimport graphql.GraphQL;\nimport graphql.incremental.DelayedIncrementalPartialResult;\nimport graphql.incremental.IncrementalExecutionResult;\nimport graphql.incremental.IncrementalPayload;\nimport graphql.schema.DataFetcher;\nimport graphql.schema.GraphQLSchema;\nimport graphql.schema.idl.RuntimeWiring;\nimport graphql.schema.idl.SchemaGenerator;\nimport graphql.schema.idl.SchemaParser;\nimport org.reactivestreams.Subscriber;\nimport org.reactivestreams.Subscription;\n\n/**\n * A @defer nested under a field of another @defer is delivered before the fragment that contains it.\n *\n * The outer fragment (\"parent\") selects a field that resolves after 300 ms and a field \"inner\"\n * whose only selection is the nested fragment (\"child\"). The child's field resolves at once.\n * Expected: \"parent\" before \"child\", as graphql-js does and as the incremental delivery working\n * group decided (https://github.com/graphql/defer-stream-wg/discussions/17).\n */\npublic class NestedDeferOrderRepro {\n static final String SDL = String.join(\"\\n\",\n \"directive @defer(if: Boolean! = true, label: String) on FRAGMENT_SPREAD | INLINE_FRAGMENT\",\n \"type Query { obj: Obj }\",\n \"type Obj { fast: String slow: String inner: Inner }\",\n \"type Inner { fast: String }\");\n\n static final String QUERY = String.join(\"\\n\",\n \"{\",\n \" obj {\",\n \" fast\",\n \" ... @defer(label: \\\"parent\\\") {\",\n \" slow\",\n \" inner { ... @defer(label: \\\"child\\\") { fast } }\",\n \" }\",\n \" }\",\n \"}\");\n\n static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(2);\n\n static \u003cT> CompletableFuture\u003cT> after(long delayMs, T value) {\n CompletableFuture\u003cT> future = new CompletableFuture\u003c>();\n SCHEDULER.schedule(() -> future.complete(value), delayMs, TimeUnit.MILLISECONDS);\n return future;\n }\n\n static GraphQL engine() {\n Map\u003cString, Object> empty = new HashMap\u003c>();\n DataFetcher\u003cObject> emptyObject = env -> empty;\n RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring()\n .type(\"Query\", t -> t.dataFetcher(\"obj\", emptyObject))\n .type(\"Obj\", t -> t\n .dataFetcher(\"fast\", env -> \"obj-fast\")\n .dataFetcher(\"slow\", env -> after(300, \"obj-slow\"))\n .dataFetcher(\"inner\", emptyObject))\n .type(\"Inner\", t -> t.dataFetcher(\"fast\", env -> \"inner-fast\"))\n .build();\n GraphQLSchema schema =\n new SchemaGenerator().makeExecutableSchema(new SchemaParser().parse(SDL), wiring);\n return GraphQL.newGraphQL(schema).build();\n }\n\n /** Runs the query and returns the deferred labels in the order they were delivered. */\n static List\u003cString> run(GraphQL graphQL, boolean eager) throws InterruptedException {\n ExecutionInput input = ExecutionInput.newExecutionInput(QUERY)\n .graphQLContext(builder -> GraphQL.unusualConfiguration(builder)\n .incrementalSupport()\n .enableIncrementalSupport(true)\n .enableEarlyIncrementalFieldExecution(eager))\n .build();\n long start = System.nanoTime();\n IncrementalExecutionResult result = (IncrementalExecutionResult) graphQL.execute(input);\n System.out.printf(\" %4d ms initial %s%n\", (System.nanoTime() - start) / 1_000_000,\n result.toSpecification().get(\"data\"));\n\n List\u003cString> labels = new ArrayList\u003c>();\n CountDownLatch done = new CountDownLatch(1);\n result.getIncrementalItemPublisher().subscribe(new Subscriber\u003cDelayedIncrementalPartialResult>() {\n Subscription subscription;\n\n @Override public void onSubscribe(Subscription s) {\n subscription = s;\n s.request(1);\n }\n\n @Override public void onNext(DelayedIncrementalPartialResult part) {\n long ms = (System.nanoTime() - start) / 1_000_000;\n for (IncrementalPayload payload : part.getIncremental()) {\n labels.add(payload.getLabel());\n System.out.printf(\" %4d ms %-7s path=%s %s%n\", ms, payload.getLabel(),\n payload.getPath(), payload.toSpecification().get(\"data\"));\n }\n subscription.request(1);\n }\n\n @Override public void onError(Throwable t) {\n t.printStackTrace();\n done.countDown();\n }\n\n @Override public void onComplete() {\n done.countDown();\n }\n });\n if (!done.await(10, TimeUnit.SECONDS)) {\n throw new IllegalStateException(\"the stream did not complete\");\n }\n return labels;\n }\n\n public static void main(String[] args) throws Exception {\n System.out.println(\"graphql-java from \" + GraphQL.class.getProtectionDomain().getCodeSource().getLocation());\n GraphQL graphQL = engine();\n boolean childFirst = false;\n for (boolean eager : new boolean[] {false, true}) {\n System.out.println(\"enableEarlyIncrementalFieldExecution=\" + eager);\n List\u003cString> labels = run(graphQL, eager);\n boolean ok = labels.indexOf(\"parent\") \u003c labels.indexOf(\"child\");\n childFirst |= !ok;\n System.out.println(\" order \" + labels + (ok ? \" OK\" : \" child delivered before its parent\"));\n }\n SCHEDULER.shutdown();\n System.exit(childFirst ? 1 : 0);\n }\n}\n```\n\n\u003c/details>\n\n### Context\n\n#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.\n","author":{"url":"https://github.com/superliaye","@type":"Person","name":"superliaye"},"datePublished":"2026-11-21T01:21:23Z","url":"https://github.com/graphql-java/graphql-java/issues/4567"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:42391df4-5070-a2ec-56bb-86b4b5468272 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE3E:4279A:1C9F7E4:268137D:6B01764C |
| html-safe-nonce | a3556270d76b246bba78679c9e48c60a10dccdd2eb92a65ab3733b10245533eb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTNFOjQyNzlBOjFDOUY3RTQ6MjY4MTM3RDo2QjAxNzY0QyIsInZpc2l0b3JfaWQiOiIxOTk0OTc0MjI3MzEyMjQ0MzAwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6afa6705b8eafe95609c3dac26d24ed1cd99d9e680ea1339decb70b8937b32ba |
| hovercard-subject-tag | issue:6522722181 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/graphql-java/graphql-java/4567/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6b68a7b8339fb95888d8eee9dca5b9c71bbd0894fe9bba27cafb0dcb5d2355e2/graphql-java/graphql-java/issues/4567 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6b68a7b8339fb95888d8eee9dca5b9c71bbd0894fe9bba27cafb0dcb5d2355e2/graphql-java/graphql-java/issues/4567 |
| og:image:alt | 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 th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | superliaye |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/graphql-java/graphql-java git https://github.com/graphql-java/graphql-java.git |
| octolytics-dimension-user_id | 14289921 |
| octolytics-dimension-user_login | graphql-java |
| octolytics-dimension-repository_id | 38602457 |
| octolytics-dimension-repository_nwo | graphql-java/graphql-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38602457 |
| octolytics-dimension-repository_network_root_nwo | graphql-java/graphql-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width