fix: options and observeHeadersSent flag #7669
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem:
Currently,
ExpressMiddleware
will attempt to end the Express request after executing a GraphQL request by callingres.end()
. If the request was already closed, or headers were sent earlier, an exception is thrown.Why is this a problem?
Sometimes a GraphQL request may trigger a long server-side process, and we want to respond in some other way than a simple HTTP response. In the project I'm working on, some requests are received as a GraphQL request, but the response is sent as a Server Sent Event, sometimes up to a minute later.
In such a case, the GraphQL request should be responded immediately with just a HTTP code 200, or a simple response. However, if we do that,
ExpressMiddleware
will attempt to send another response when the process ends. This is undesirable.What is the suggestion?
The simplest would be to simply skip sending a response if
res.headersSent
istrue
. However, this may also introduce unwanted side effects.My suggestion introduces an
options
object alongsidecontext
. It currently has only one value,observeHeadersSent
. If this flag istrue
, the middleware will not try to send a HTTP response if headers were already sent.The
options
object can be extended later with other options, as needed.