Comments (7)
Ah, ok, I misread - the error is thrown properly, but you have problems writing the headers.
Writing a 200: OK
is necessary in order to establish an SSE connection; meaning, headers are already written and flushed at the point where onNext
is reached because there's an active and accepted SSE connection.
from graphql-sse.
Since re-writing the header is not possible, you can catch the error in onNext
and populate the errors
GraphQL result field to transmit to the client through the next
message.
Something around the edges:
import http from 'http';
import { createHandler } from 'graphql-sse';
const handler = createHandler({
// ... your other options ...
onNext: () => {
try {
mightThrowAnError();
} catch (err) {
return {
errors: [new GraphQLError(err)],
};
}
},
});
http.createServer(async (req, res) => {
try {
await handler(req, res);
} catch (err) {
if (!res.headersSent) {
res.writeHead(500, 'Internal Server Error').end();
} else {
console.error('Internal Server Error', err);
}
}
});
But beware, I do NOT recommend this. You should absolutely make sure that nothing is thrown from onNext
. Execution errors, errors that are a part of the next
message, are not terminal - the client will not end and the operation will proceed blindly.
from graphql-sse.
Thank you @enisdenjo! The example I have used is directly copied from the JSDoc for the Handler
. I have submitted a PR for this.
from graphql-sse.
Hmm, I couldn't manage to replicate your issue, I've added tests describing your case, and they pass.
Can you elaborate further? Which version are you using? How are you connecting to the server? Which client are you using?
Ideally a full repro would be perfect and much appreciated!
from graphql-sse.
If I want to write a different head, what are the options? For instance, I would like to send a 500 when something goes wrong in a resolver.
from graphql-sse.
You cannot write a different header. As mentioned before, it is already written and flushed at that point.
You can only leave a log on your server and fix the onNext
handler.
from graphql-sse.
🎉 This issue has been resolved in version 1.1.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
from graphql-sse.
Related Issues (20)
- Reconnection not working on Chrome HOT 4
- Network errors during event emission not retried in Safari HOT 1
- Pass all supported options to the fetch function HOT 4
- Can anyone help me use this with RedwoodJS? I want to run graphql subscriptions in redwoodjs.
- Http2 Example req res params not accepted HOT 2
- Example issues
- Special characters in payload are not parsed properly
- Persisted queries not compatible with Relay
- Empty string in message.event field causes a runtime error.
- Doesn't appear to be working properly with Bun HOT 1
- Subscription not getting canceled
- Infinite retries HOT 24
- Broken persisted queries support HOT 2
- Subscription errors get silently discarded HOT 1
- Handler does not accept AST as a query
- GraphQL SSE Subscriptions in a React-Native App. HOT 1
- Cannot call write after a stream was destroyed HOT 17
- How to add parameters to With EventSource HOT 1
- Support TypedDocumentNode HOT 1
- [HELP] integrating SSELink and httpLink
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-sse.