Alternative proposal for `@stream`/`@defer`
At a glance​
- Identifier: #1018
- Stage: RFCX: Closed 2023-04-27T10:37:44Z
- Champion: @benjie
- PR: Alternative proposal for `@stream`/`@defer`
Timeline​
- Added to 2023-04-06 WG agenda
- Spec PR created on 2023-03-06 by benjie
- 19 commits pushed on 2023-03-06:
- defers is a map, streams is a list, they are both recursively merged by @benjie
- Defers are progressing well by @benjie
- Clear unused code by @benjie
- Minor fixes by @benjie
- Stream by @benjie
- Scope issue by @benjie
- Remove duplicate ExecuteField by @benjie
- Consistency by @benjie
- Else -> otherwise by @benjie
- Wording tweaks by @benjie
- Copy/paste error by @benjie
- More consistent with subscriptions by @benjie
- Replace hideous words with an even more hideous algorithm by @benjie
- Add a helpful note by @benjie
- Consistently use 'be the result of running' by @benjie
- Typo by @benjie
- Simplify by @benjie
- Sets aren't typically ordered, use a list. by @benjie
- Hyphen by @benjie
- 3 commits pushed on 2023-03-04:
This RFC is an alternative solution to incremental delivery. This is a very early draft to enable sharing this solution with the incremental delivery working group, and, if ratified, may either be merged into @robrichard et al's excellent work in #742, or supersede it.
This PR currently only addresses the algorithms in the execution part of the specification (and is based off of a recent draft of the GraphQL specification, rather than any preceding work), so for accompanying specification text regarding the syntax and directives of incremental delivery, please see the excellent prior work in #742.
The goal of this proposal is to address the needs of incremental delivery whilst:
- Ensuring that the resolver at each path in the response is called at most once, to ensure that the individual payloads in an incremental delivery stream can always be reconciled into a final object that would be equivalent to the object that could be produced by removing all the
@stream
and@defer
directives from the request.- Ensuring that contents of a
@defer
'd or@stream
'd selection set are sent atomically in a single event, such thatMyFragment: __typename
and other fragment identification approaches can be relied upon to confirm that the entire fragment is present.- Avoiding duplicate delivery of leaves in order to reduce both network traffic, and memory load/redundant processing on client and server.
- Ensuring that you know if there is still pending data under a given path in the response.
- Allowing multiple streamed/deferred responses to be batched together into the same event to minimize client workload and reduce network traffic.
Non-goals of this proposal are:
- Ensuring that you can check on the status of a specific
@stream
or@defer
directives issued in the request.
- There is generally not a one-to-one relationship between
@stream
/@defer
directives in the request and any components in the response.- Allowing you to to set the priority of sibling
@defer
s
- There are no sibling defers, only nested defers
- Currently
... @defer { ... @defer { a } }
is equivalent to... @defer { a }
, but we might change this.The significant change in this RFC is that it is built around the field merging algorithm that we already have, and allows merging
@defer
s not just within a single selection set, but across the entire request. It works based on "defer layers" - thus for a query such as:{
a {
b
... @defer { c { c1 } }
d {
e
... @defer { f }
}
}
g
... @defer {
a {
c {
... @defer { c2 }
}
}
h
}
}The first query to be resolved is as before:
{
a {
b
d {
e
}
}
g
}Yielding something like:
{
data: {
a: {
b: "B",
d: {
e: "E"
}
},
g: "G"
},
pending: [
{ id: 0, path: [] }
],
hasNext: true
}Next the first layer of
@defer
'd leaves is evaluated, which results in the following selection sets being evaluated at the following paths:
- path:
[]
, selection:{ h }
- path:
['a']
, selection:{ c { c1 } }
- path:
['a', 'd']
, selection:{ f }
All three of these are evaluated (separately, in parallel) and then grouped together into the same event, something like:
{
incremental: [
{ path: [], data: { h: "H" } },
{ path: ['a'], data: { c: { c1: "C1" } } },
{ path: ['a', 'd'], data: { f: "F" } }
],
completed: [
{ id: 0 }
],
pending: [
{ id: 1, path: ['a', 'c'] }
],
hasNext: true
}Finally we look at the next layer of
@defer
, which would yield:
- path:
['a', 'c']
, selection:{ c2 }
Which would yield:
{
incremental: [
{ path: ['a', 'c'], data: { c2: "C2" } }
],
completed: [
{ id: 1 }
],
hasNext: true
}and finally:
{
hasNext: false
}(Note: we can probably optimize this to put the
hasNext: false
in the previous payload instead, but I've not yet tried to write that into the spec text.)