Throwing from onConnect - what's the proper pattern? #317
-
If I throw from the
I get the sense that I shouldn't be throwing an error, but instead closing the socket with some kind of message. What's a good example of that? Does this also apply to the other event listeners as well? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The docs does say that I should be able to throw from it, and the socket will close appropriately, but should I be getting that specific error on the server-side? I'm throwing an |
Beta Was this translation helpful? Give feedback.
-
Throwing from any of the server hooks close the WebSocket connection with a:
With this liberty, you can catch an error in a hook, close with whatever code is agreed with a client for that specific error, have the client do something fancy; or just ignore the error all-together and proceed with execution, it's up to you. Note that |
Beta Was this translation helpful? Give feedback.
Throwing from any of the server hooks close the WebSocket connection with a:
4500: Internal server error
. The close reason indicates that the server occurred an unexpected error, is terminal by nature and should not happen. Important thing to note is that the client will report the4500
close code immediately and not retry.graphql-ws
doesn't have opinions on how closing connections on errors should look like, so it just offers a basic solution in case of thrown errors in unexpected places. There are numerous close codes you can use on a WebSocket, they're all in your hands.With this liberty, you can catch an error in a hook, close with whatever code is agreed with a client for that spec…