graphql-js: Errors thrown when iterating over subscription source event streams (AsyncIterables) should be caught
Context
Hi there. We’re using graphql-js and serving subscriptions over WebSocket via graphql-ws (as recommended by Apollo for both server and client).
In our subscriptions’ subscribe methods, we always return an AsyncIterable pretty much right away. We typically do this either by defining our methods via async generator functions (async function*), or by calling graphql-redis-subscriptions’s asyncIterator method. Our subscribe methods effectively never throw an error just providing an AsyncIterable.
However, we occasionally hit errors actually streaming subscription events, when graphql-js calls our AsyncIterable’s next() method. E.g. Redis could be momentarily down, or an upstream producer/generator could fail/throw. So we sometimes throw errors during iteration. And importantly, this can happen mid-stream.
Problem
graphql-js does not try/catch/handle errors when iterating over an AsyncIterable:
There’s even a test case today that explicitly expects these errors to be re-thrown:
graphql-ws doesn’t try/catch/handle errors thrown during iteration either:
As a result, when occasional errors happen like this, the entire underlying WebSocket connection is closed.
This is obviously not good! 😅 This interrupts every other subscription the client may be subscribed to at that moment, adds reconnection overhead, drops events, etc. And if we’re experiencing some downtime on a specific subscription/source stream, this’ll result in repeat disconnect-reconnect thrash, because the client also has no signal on which subscription has failed!!
Inconsistency
You could argue that graphql-ws should try/catch these errors and send back an error message itself. The author of graphql-ws believes this is the domain of graphql-js, though (https://github.com/enisdenjo/graphql-ws/discussions/333), and I agree.
That’s because graphql-js already try/catches and handles errors both earlier in the execution of a subscription and later:
-
Errors producing an
AsyncIterablein the first place (the synchronous result of calling the subscription’ssubscribemethod, AKA producing a source event stream in the spec) are caught, and returned as a{data: null, errors: ...}result: -
Errors mapping iteration results to response events (the result of calling the subscription’s
resolvemethod) are caught, and sent back to the client as a{value: {data: null, errors: ...}, done: false}event:
So it’s only iterating over the AsyncIterable — the “middle” step of execution — where graphql-js doesn’t catch errors and convert them to {data: null, errors: ...} objects.
This seems neither consistent nor desirable, right?
Alternatives
We can change our code to:
- Have our
AsyncIterablenever throw innext()(try/catch every iteration ourselves)- Have it instead always return a wrapper type, mimicking
{data, errors}
- Have it instead always return a wrapper type, mimicking
- Define a
resolvemethod just to unwrap this type (even if we have no need for custom resolving otherwise)- And have this
resolvemethodthrowanyerrorsorreturn dataif no errors
- And have this
Doing this would obviously be pretty manual, though, and we’d have to do it for every subscription we have.
Relation to spec
Given the explicit test case, I wasn’t sure at first if this was an intentional implementation/interpretation of the spec.
I’m not clear from reading the spec, and it looks like at least one other person wasn’t either: https://github.com/graphql/graphql-spec/issues/995.
But I think my own interpretation is that the spec doesn’t explicitly say to re-throw errors. It just doesn’t say what to do.
And I believe that graphql-js is inconsistent in its handling of errors, as shown above. The spec also doesn’t seem to clearly specify how to handle errors creating source event streams, yet graphql-js (nicely) handles them.
I hope you’ll consider handling errors iterating over source event streams too! Thank you.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 8
- Comments: 25 (20 by maintainers)
Commits related to this issue
- Update execute.ts issue-:#4001 — committed to xonx4l/graphql-js by xonx4l 3 years ago
@benjie that’s all fine, yielding
errors: []is ok . What I was discussing is the need of anerrormessage type - which would not be needed if theerrors: []are yielded throughnext.@martinbonnin I am on that team too! 😄
I love the idea that a GraphQL stream emits data until completed. And the data can contain an error.
@martinbonnin graphql-sse follows SSE semantics where all events are just data transmitted over the wire.
The only reason why
completeexists in the “distinct connections mode” is to indicate to the client that the following close of connection is expected. For example, EventSource will keep reconnecting forever until the client stops the stream.That being said,
nextjust means “event” - any event, i.e. if thecompletewerent necessary,nextwouldnt exist either.“Single connection mode”, on the other hand, would need the
completenevertheless because of its multiplex nature. But there we’re keeping the semantics in line by not introducing anerrormessage type.P.S. I spoke too fast in my previous comment and have updated it in place to not confuse future readers.
Just thinking of cases where internal errors span across the whole GraphQL instance - but couldnt think of any. That being said - I agree. graphql-ws should change to use the “error” message type for internal errors too.