Skip to content

Commit c37a4a4

Browse files
authored
Editorial changes for Event Streams (#1099)
* Editorial changes for Event Streams This makes a few changes to the Subscriptions section where we're talking about event streams in an attempt to make it more clear about what's going on. - Revised variable names from "fieldStream" to "sourceStream" to make it easier to trace variables through algorithms. - Rewrote the "Event Streams" definition to be more clear about "emit" keyword and have clear paragraphs on completion and cancellation. - Rewrote the `MapSourceToResponseEvent` algorithm to be a correctly formatted algorithm with a return statement at the end. Introduced a new "When" keyword to describe event subscriptions. Added explicit sections on passing back cancellation (discussed in WG) as well as completion with error (not discussed, but I realized was also left ambiguous) * feedback and use definition syntax
1 parent 34730e8 commit c37a4a4

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

spec/Section 2 -- Language.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ There are three types of operations that GraphQL models:
288288

289289
- query - a read-only fetch.
290290
- mutation - a write followed by a fetch.
291-
- subscription - a long-lived request that fetches data in response to source
292-
events.
291+
- subscription - a long-lived request that fetches data in response to a
292+
sequence of events over time.
293293

294294
Each operation is represented by an optional operation name and a _selection
295295
set_.

spec/Section 6 -- Execution.md

+48-25
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ ExecuteMutation(mutation, schema, variableValues, initialValue):
164164

165165
### Subscription
166166

167-
If the operation is a subscription, the result is an event stream called the
167+
If the operation is a subscription, the result is an _event stream_ called the
168168
"Response Stream" where each event in the event stream is the result of
169169
executing the operation for each new event on an underlying "Source Stream".
170170

@@ -217,14 +217,21 @@ chat room ID is the "topic" and each "publish" contains the sender and text.
217217

218218
**Event Streams**
219219

220-
An event stream represents a sequence of discrete events over time which can be
221-
observed. As an example, a "Pub-Sub" system may produce an event stream when
222-
"subscribing to a topic", with an event occurring on that event stream for each
223-
"publish" to that topic. Event streams may produce an infinite sequence of
224-
events or may complete at any point. Event streams may complete in response to
225-
an error or simply because no more events will occur. An observer may at any
226-
point decide to stop observing an event stream by cancelling it, after which it
227-
must receive no more events from that event stream.
220+
:: An _event stream_ represents a sequence of events: discrete emitted values
221+
over time which can be observed. As an example, a "Pub-Sub" system may produce
222+
an _event stream_ when "subscribing to a topic", with an value emitted for each
223+
"publish" to that topic.
224+
225+
An _event stream_ may complete at any point, often because no further events
226+
will occur. An _event stream_ may emit an infinite sequence of values, in which
227+
it may never complete. If an _event stream_ encounters an error, it must
228+
complete with that error.
229+
230+
An observer may at any point decide to stop observing an _event stream_ by
231+
cancelling it. When an _event stream_ is cancelled, it must complete.
232+
233+
Internal user code also may cancel an _event stream_ for any reason, which would
234+
be observed as that _event stream_ completing.
228235

229236
**Supporting Subscriptions at Scale**
230237

@@ -250,8 +257,8 @@ service details should be chosen by the implementing service.
250257

251258
#### Source Stream
252259

253-
A Source Stream represents the sequence of events, each of which will trigger a
254-
GraphQL execution corresponding to that event. Like field value resolution, the
260+
A Source Stream is an _event stream_ representing a sequence of root values,
261+
each of which will trigger a GraphQL execution. Like field value resolution, the
255262
logic to create a Source Stream is application-specific.
256263

257264
CreateSourceEventStream(subscription, schema, variableValues, initialValue):
@@ -268,15 +275,15 @@ CreateSourceEventStream(subscription, schema, variableValues, initialValue):
268275
- Let {field} be the first entry in {fields}.
269276
- Let {argumentValues} be the result of {CoerceArgumentValues(subscriptionType,
270277
field, variableValues)}.
271-
- Let {fieldStream} be the result of running
278+
- Let {sourceStream} be the result of running
272279
{ResolveFieldEventStream(subscriptionType, initialValue, fieldName,
273280
argumentValues)}.
274-
- Return {fieldStream}.
281+
- Return {sourceStream}.
275282

276283
ResolveFieldEventStream(subscriptionType, rootValue, fieldName, argumentValues):
277284

278285
- Let {resolver} be the internal function provided by {subscriptionType} for
279-
determining the resolved event stream of a subscription field named
286+
determining the resolved _event stream_ of a subscription field named
280287
{fieldName}.
281288
- Return the result of calling {resolver}, providing {rootValue} and
282289
{argumentValues}.
@@ -287,17 +294,33 @@ operation type.
287294

288295
#### Response Stream
289296

290-
Each event in the underlying Source Stream triggers execution of the
291-
subscription _selection set_ using that event as a root value.
297+
Each event from the underlying Source Stream triggers execution of the
298+
subscription _selection set_ using that event's value as the {initialValue}.
292299

293300
MapSourceToResponseEvent(sourceStream, subscription, schema, variableValues):
294301

295-
- Return a new event stream {responseStream} which yields events as follows:
296-
- For each {event} on {sourceStream}:
297-
- Let {response} be the result of running
298-
{ExecuteSubscriptionEvent(subscription, schema, variableValues, event)}.
299-
- Yield an event containing {response}.
300-
- When {sourceStream} completes: complete {responseStream}.
302+
- Let {responseStream} be a new _event stream_.
303+
- When {sourceStream} emits {sourceValue}:
304+
- Let {response} be the result of running
305+
{ExecuteSubscriptionEvent(subscription, schema, variableValues,
306+
sourceValue)}.
307+
- If internal {error} was raised:
308+
- Cancel {sourceStream}.
309+
- Complete {responseStream} with {error}.
310+
- Otherwise emit {response} on {responseStream}.
311+
- When {sourceStream} completes normally:
312+
- Complete {responseStream} normally.
313+
- When {sourceStream} completes with {error}:
314+
- Complete {responseStream} with {error}.
315+
- When {responseStream} is cancelled:
316+
- Cancel {sourceStream}.
317+
- Complete {responseStream} normally.
318+
- Return {responseStream}.
319+
320+
Note: Since {ExecuteSubscriptionEvent()} handles all _field error_, and _request
321+
error_ only occur during {CreateSourceEventStream()}, the only remaining error
322+
condition handled from {ExecuteSubscriptionEvent()} are internal exceptional
323+
errors not described by this specification.
301324

302325
ExecuteSubscriptionEvent(subscription, schema, variableValues, initialValue):
303326

@@ -317,9 +340,9 @@ Note: The {ExecuteSubscriptionEvent()} algorithm is intentionally similar to
317340
#### Unsubscribe
318341

319342
Unsubscribe cancels the Response Stream when a client no longer wishes to
320-
receive payloads for a subscription. This may in turn also cancel the Source
321-
Stream. This is also a good opportunity to clean up any other resources used by
322-
the subscription.
343+
receive payloads for a subscription. This in turn also cancels the Source
344+
Stream, which is a good opportunity to clean up any other resources used by the
345+
subscription.
323346

324347
Unsubscribe(responseStream):
325348

0 commit comments

Comments
 (0)