diff --git a/example/chat/readme.md b/example/chat/readme.md index 07b65c77738..416a9fb083a 100644 --- a/example/chat/readme.md +++ b/example/chat/readme.md @@ -22,7 +22,7 @@ Then to run the app with the `apollo-link-ws` implementation do npm run start ``` -or to run the app with the `graphql-ws` implementation do +or to run the app with the `graphql-ws` implementation (and the newer `graphql-transport-ws` protocol) do ```bash npm run start:graphql-transport-ws ``` diff --git a/graphql/handler/transport/websocket.go b/graphql/handler/transport/websocket.go index 37d62a997ca..bb148aec2c4 100644 --- a/graphql/handler/transport/websocket.go +++ b/graphql/handler/transport/websocket.go @@ -240,12 +240,19 @@ func (c *wsConnection) subscribe(start time.Time, msg *message) { go func() { defer func() { if r := recover(); r != nil { - userErr := rc.Recover(ctx, r) - c.sendError(msg.id, &gqlerror.Error{Message: userErr.Error()}) + err := rc.Recover(ctx, r) + var gqlerr *gqlerror.Error + if !errors.As(err, &gqlerr) { + gqlerr = &gqlerror.Error{} + if err != nil { + gqlerr.Message = err.Error() + } + } + c.sendError(msg.id, gqlerr) } - c.complete(message.ID) + c.complete(msg.id) c.mu.Lock() - delete(c.active, message.ID) + delete(c.active, msg.id) c.mu.Unlock() cancel() }() diff --git a/graphql/handler/transport/websocket_test.go b/graphql/handler/transport/websocket_test.go index 207493bd9be..4280b1205f9 100644 --- a/graphql/handler/transport/websocket_test.go +++ b/graphql/handler/transport/websocket_test.go @@ -318,6 +318,7 @@ func TestWebsocketGraphqltransportwsSubprotocol(t *testing.T) { } func wsConnect(url string) *websocket.Conn { + return wsConnectWithSubprocotol(url, "") } @@ -327,7 +328,7 @@ func wsConnectWithSubprocotol(url, subprocotol string) *websocket.Conn { h.Add("Sec-WebSocket-Protocol", subprocotol) } - c, resp, err := websocket.DefaultDialer.Dial(strings.Replace(url, "http://", "ws://", -1), h) + c, resp, err := websocket.DefaultDialer.Dial(strings.ReplaceAll(url, "http://", "ws://"), h) if err != nil { panic(err) }