-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spec: Formal Subscriptions Definition #305
Changes from 16 commits
4bba743
7b4fb56
328644b
c4aeaf9
f38092b
ca8af4d
3933a5a
d3220d6
e61963c
6d9dc8d
1321024
73b364f
d3011a5
23bd790
f7581ef
f18c569
a0aac7f
35c6059
ca2a02e
36359fd
47404cb
6425fed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,6 +198,67 @@ query getName { | |
} | ||
``` | ||
|
||
### Subscription Operation Definitions | ||
|
||
#### Single root field | ||
|
||
**Formal Specification** | ||
|
||
* For each subscription operation definition {subscription} in the document | ||
* Let {rootFields} be the top level selection set on {subscription}. | ||
* {rootFields} must be a set of one. | ||
|
||
**Explanatory Text** | ||
|
||
Subscription operations must have exactly one root field. | ||
|
||
Valid examples: | ||
|
||
```graphql | ||
subscription sub { | ||
newMessage { | ||
body | ||
sender | ||
} | ||
} | ||
``` | ||
|
||
```graphql | ||
fragment newMessageFields on Message { | ||
body | ||
sender | ||
} | ||
|
||
subscription sub { | ||
newMessage { | ||
... newMessageFields | ||
} | ||
} | ||
``` | ||
|
||
Invalid: | ||
|
||
```!graphql | ||
subscription sub { | ||
newMessage { | ||
body | ||
sender | ||
} | ||
disallowedSecondRootField | ||
} | ||
``` | ||
|
||
Introspection fields are counted. The following example is also invalid: | ||
|
||
```!graphql | ||
subscription sub { | ||
newMessage { | ||
body | ||
sender | ||
} | ||
__typename | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not clear what we would name this one if we wanted to get rid of the comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a description above. |
||
} | ||
``` | ||
|
||
## Fields | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,8 +103,10 @@ Note: This algorithm is very similar to {CoerceArgumentValues()}. | |
## Executing Operations | ||
|
||
The type system, as described in the “Type System” section of the spec, must | ||
provide a query root object type. If mutations are supported, it must also | ||
provide a mutation root object type. | ||
provide a query root object type. If mutations or subscriptions are supported, | ||
it must also provide a mutation and subscription root object type, respectively. | ||
|
||
### Query | ||
|
||
If the operation is a query, the result of the operation is the result of | ||
executing the query’s top level selection set with the query root object type. | ||
|
@@ -123,6 +125,8 @@ ExecuteQuery(query, schema, variableValues, initialValue): | |
selection set. | ||
* Return an unordered map containing {data} and {errors}. | ||
|
||
### Mutation | ||
|
||
If the operation is a mutation, the result of the operation is the result of | ||
executing the mutation’s top level selection set on the mutation root | ||
object type. This selection set should be executed serially. | ||
|
@@ -143,6 +147,136 @@ ExecuteMutation(mutation, schema, variableValues, initialValue): | |
selection set. | ||
* Return an unordered map containing {data} and {errors}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting a comment here because silly GitHub won't let me comment on the lines above. Up on line 106-107 it says:
That should probably say:
Or words to that effect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also noticed that a step should be added to |
||
|
||
### Subscription | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we include some example responses here, and perhaps an example of a lifecycle for a simple subscription? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, I've added an example after the unsubscribe section. |
||
|
||
If the operation is a subscription, the result is an event stream called the | ||
"Response Stream" where each event in the event stream is the result of | ||
executing the operation for each new event on an underlying "Source Stream". | ||
|
||
An event stream represents a sequence of discrete events over time which can be | ||
observed. As an example, a "Pub-Sub" system may produce an event stream when | ||
"subscribing to a topic", with an event occurring on that event stream for each | ||
"publish" to that topic. Event streams may produce an infinite sequence of | ||
events or may complete at any point. Event streams may complete in response to | ||
an error or simply because no more events will occur. An observer may at any | ||
point decide to stop observing an event stream, after which it must receive no | ||
more events from that event stream. | ||
|
||
Note: If an event stream's observer has stopped observing, that may be a good | ||
opportunity to clean up any associated resources such as closing any connections | ||
which are no longer necessary. | ||
|
||
#### Subscribe | ||
|
||
Executing a subscription creates a persistent function on the server that | ||
maps an underlying Source stream to the Publish Stream. The logic to create the | ||
Source stream is application-specific and takes the root field and query | ||
variables as inputs. | ||
|
||
Subscribe(subscription, schema, variableValues, initialValue): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, this is a good place to talk about returning an initial response. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm concerned that "ExecuteSubscription" and "ExecuteSubscriptionEvent" will be confused with one another. The name seems to imply that the caller can expect a response containing data. |
||
|
||
* Let {sourceStream} be the result of running {CreateSourceEventStream(subscription, schema, variableValues, initialValue)}. | ||
* Let {responseStream} be the result of running {MapSourceToResponseEvent(sourceStream, subscription, schema, variableValues)} | ||
* Return {responseStream}. | ||
|
||
CreateSourceEventStream(subscription, schema, variableValues, initialValue): | ||
|
||
* Let {subscriptionType} be the root Subscription type in {schema}. | ||
* Assert: {subscriptionType} is an Object type. | ||
* Let {selectionSet} be the top level Selection Set in {subscription}. | ||
* Let {rootField} be the first top level field in {selectionSet}. | ||
* Let {argumentValues} be the result of {CoerceArgumentValues(subscriptionType, rootField, variableValues)}. | ||
* Let {fieldStream} be the result of running {ResolveFieldEventStream(subscriptionType, initialValue, rootField, argumentValues)}. | ||
* Return {fieldStream}. | ||
|
||
ResolveFieldEventStream(subscriptionType, rootValue, fieldName, argumentValues): | ||
* Let {resolver} be the internal function provided by {subscriptionType} for | ||
determining the resolved value of a field named {fieldName}. | ||
* Return the result of calling {resolver}, providing {rootValue} and {argumentValues}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a |
||
|
||
#### Publish Stream | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Response Stream"? |
||
|
||
Each event in the underlying event stream triggers execution of the subscription | ||
selection set. | ||
|
||
MapSourceToResponseEvent(sourceStream, subscription, schema, variableValues): | ||
|
||
* Return a new event stream {responseStream} which yields events as follows: | ||
* For each {event} on {sourceStream}: | ||
* Let {response} be the result of running | ||
{ExecuteSubscriptionEvent(subscription, schema, variableValues, event)}. | ||
* Yield an event containing {response}. | ||
|
||
ExecuteSubscriptionEvent(subscription, schema, variableValues, initialValue): | ||
|
||
* Let {subscriptionType} be the root Subscription type in {schema}. | ||
* Assert: {subscriptionType} is an Object type. | ||
* Let {selectionSet} be the top level Selection Set in {subscription}. | ||
* Let {data} be the result of running | ||
{ExecuteSelectionSet(selectionSet, subscriptionType, initialValue, variableValues)} | ||
*normally* (allowing parallelization). | ||
* Let {errors} be any *field errors* produced while executing the | ||
selection set. | ||
* Return an unordered map containing {data} and {errors}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, it would be nice to have a |
||
|
||
Note: in large scale subscription systems, the {ExecuteSubscriptionEvent} and | ||
{Subscribe} algorithms may be run on separate services to maintain predictable | ||
scaling properties. See the section below on Supporting Subscriptions at Scale. | ||
|
||
#### Unsubscribe | ||
|
||
Unsubscribe cancels the Response Stream. This is also a good opportunity for the | ||
server to clean up the underlying event stream and any other resources used by | ||
the subscription. Here are some example cases in which to Unsubscribe: client | ||
no longer wishes to receive payloads for a subscription; the source event stream | ||
produced an error or naturally ended; the server encountered an error during | ||
ExecuteSubscriptionEvent. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with you that this is redundant with the note above. I think you should merge the two together, either relying on the Note above and leaving this section with a much simpler explanation of the algorithm below, or removing the Note above and leaving those details here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the note in the "### Subscription" section and left this paragraph in place. I think this reads more clearly after moving the example. |
||
|
||
Unsubscribe() | ||
|
||
* Cancel {responseStream} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above we used the term "stop observing" - we should probably be consistent here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the server is the producer of the response stream, if we use "stop observing", we probably give the impression that we are describing client behavior, whereas the previous sections clearly describe server behavior. |
||
|
||
#### Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elsewhere we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great suggestion! |
||
|
||
As an example, consider a chat application. To subscribe to new messages posted | ||
to the chat room, the client sends a request like so: | ||
|
||
```graphql | ||
subscription NewMessages { | ||
newMessage(roomId: 123) { | ||
sender | ||
text | ||
} | ||
} | ||
``` | ||
|
||
While the client is subscribe, whenever new messages are posted to chat room | ||
with ID "123", the selection for "sender" and "text" will be evaluated and | ||
published to the client, for example: | ||
|
||
```js | ||
{ | ||
"data": { | ||
"newMessage": { | ||
"sender": "Hagrid", | ||
"text": "You're a wizard!" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
#### Supporting Subscriptions at Scale | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might consider also moving this section above the algorithms and making this a |
||
|
||
Supporting subscriptions is a significant change for any GraphQL server. Query | ||
and mutation operations are stateless, allowing scaling via cloning GraphQL | ||
server instances. Subscriptions, by contrast, are stateful and require | ||
maintaining the GraphQL document, variables, and other context over the lifetime | ||
of the subscription. | ||
|
||
Consider the behavior of your system when state is lost due to the failure of a | ||
single machine in a service. Durability and availability may be improved by | ||
having separate dedicated services for managing subscription state and client | ||
connectivity. | ||
|
||
## Executing Selection Sets | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robzhu what is the reason for that? why can't i subscribe to more then one subscription with one call?
so far i thought this is implementation limitation..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Rob's text that starting with just one root field is reasonable. A later version of the spec could support multiple fields, but that requires figuring out a lot of edge cases around errors, when subfields are re-executed, and more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also would like to know the reason for this rule. I find that it is quite hard limitation, and in a way it goes against GraphQL principles where clients have the power to decide what it wants to get.
I think it would be helpful to list and discuss these edge cases. From my experience, there are definitely things to consider when merging different event streams from different GraphQL subscription fields, but I find it quite manageable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I listed most of the points I found as I was implementing it in sangria here:
#282 (comment)
I think that it boils down to points
3.i
,5
and6
. My take on it is here: #282 (comment)IMHO, if we need to make a trade off, I would rather disallow not-null root subscription field types than allow only a single subscription field in a query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stubailo if one wants to experiment that's fine, but i don't think it should be in the official spec unless there is a reason (which is not implementation) behind it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stubailo thanks a lot for describing in on an example!
I don't see it as a problem but rather as a natural behavior. This is inherent property of event-based interaction. This is also the reason why I suggested to disallow not-null root subscription field types.
I also don't see it as a issue either. Can you describe in more detail why this behavior can be disadvantageous? (considering that you still can make 2 separate and isolated subscription queries if it suits better for the use-case at hand)
I feel that either behavior is fine as long as it is defined in the spec. Though in this case I would suggest draw inspiration from streaming libraries: if 2 event streams are joined/merged together in a single result stream, then an error in either of these will also case the result stream to fail. If one of event steams naturally completes (because of the exhaustion), then the result steam still continue to emit events until all of the source streams are exhausted. I think this behavior is quite intuitive and widespread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OlegIlyenko @DxCx To give a little bit of context on where this came from, we discovered early on that it was better to use subscriptions for modeling granular events. For example, consider the three main subscriptions that operate on a facebook post: live comments, live likes, and typing indicators. These are individual subscriptions as opposed to a single "postLiveUpdate" subscription. Keeping these subscriptions granular on the client made natural sense. @laneyk and @dschafer may be able to add more perspective here.
Thinking this through, if we include multiple root fields like so:
So far, everyone seems to assume this subscription should publish data when either "live like" or "live comment" publishes. Is that clearly the intent of this query? What if there were a desire to trigger the publish only when both root fields have a publish payload available? How would we describe that? By limiting the selection to a single root field, we sidestep all that.
I also don't think the single-root-field-rule introduces any practical limitations to the client. In fact, it results in simpler client-side code, like so:
For subscriptions containing more than one root field, if we assume the "or" behavior, as @stubailo points out, you'll never have more than one event trigger at a time, so the code would end up looking like:
Can someone help me understand a compelling use case that is served by multi-root subscription operations that would not be equally served by separate individual subscriptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to add a meta-point to this conversation:
In my view, there's nothing stopping us from working through these details and figuring out the edge and corner cases of allowing multiple subscription fields in a single request, however the choice we do have is to address those concerns now, or allow for more time to do so. In previous conversations @robzhu has had over the last few months about subscriptions, he has convinced many that this is far more complicated than we originally thought and may not have clear answers. This limitation is added mostly in a desire to expedite the addition of subscriptions to the spec, while reserving the ability to continue to work out how or if multi-field subscriptions should be allowed.
Had this limitations been omitted while also not making mention of how to address multi-field subscriptions in the spec, then we would see divergence of behavior and that could tie our hands in the future for deciding how to address these edge cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @robzhu and @leebyron for the insightful comments! I think now I got a better understanding of the issue. I was also in two minds on this. On one hand, I wanted to get a better understanding about motivation behind inclusion of this rule. But on the other hand, I don't want to delay the progress on subscriptions incision in the spec. I tend to agree that it is a good idea to disallow multiple fields for now and start a separate discussion. I think it is a discussion worth having. I am actually very glad that this point is considered in the spec since I was also quite concerned about the semantics of multiple subscriptions fields.
In general, I found it very valuable to have as much information from client as possible in single query. For example, the fact that a client can express its requirements for a view or particular part of the application in a single query allowed us to make very interesting optimizations which would be quite hard to do otherwise (it is quite hard to correlate seemingly independent requests/queries). So by allowing client to better express it's requirements with several subscription fields in a single query, we open a door for potential server-side optimizations.
Now that I'm equipped with new insights, I will give it another thought. This thread was definitely helpful in this respect.
Before we will introduce this rule though, I think it is important to consider the nullability of the subscription fields, as i mentioned above. It is possible to make a nullable field not-null later on in a backwards-compatible way. If we allow subscription fields to be not-null now, it might become a challenge in future to allow multiple subscription fields, if we decide to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nullability may mean two different things in this context - this is a great point we should address.
One thing it may mean is that a subscription may not exist given some inputs in a way that isn't considered an error. I think this interpretation is both not what you were referring to, and also probably confusing to think about. The schema talks about the type of the payload result - so we're talking about the types of responses. We should probably make this point in the spec to clarify.
Secondly the nullability of the responses. This is one of the concerns with multi-field subscriptions to address later. For example, should it be legal to have a subscription field
streamThings: String?
where it is legal for any payload in the event sequence to in fact benull
? I don't see a compelling reason to explicitly disallow this - though it is an edge case.I think handling the payloads of multi-field subscriptions will need to account for this