An implementation of websocket transport for graphql-go.
Currently following flavors are supported:
graphql-ws
subprotocol, older spec: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.mdgraphql-transport-ws
subprotocol, newer spec: https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
Inspired by graphqlws
Key features:
- Subscription support
- Interceptors at every stage of communication process for easy customization
- Supports both websockets and plain http queries, with http chunked response for plain http subscriptions
- Mutable context allowing to keep request-scoped connection/authentication data and operation-scoped state
Assuming gorilla websocket upgrader
import (
"net/http"
"github.com/eientei/wsgraphql/v1"
"github.com/eientei/wsgraphql/v1/compat/gorillaws"
"github.com/gorilla/websocket"
"github.com/graphql-go/graphql"
)
schema, err := graphql.NewSchema(...)
if err != nil {
panic(err)
}
srv, err := wsgraphql.NewServer(
schema,
wsgraphql.WithUpgrader(gorillaws.Wrap(&websocket.Upgrader{
Subprotocols: []string{
wsgraphql.WebsocketSubprotocolGraphqlWS.String(),
wsgraphql.WebsocketSubprotocolGraphqlTransportWS.String(),
},
})),
)
if err != nil {
panic(err)
}
http.Handle("/query", srv)
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
See /v1/examples
- minimal-graphql-ws
graphql-ws
/ older subscriptions-transport-ws server setup - minimal-graphql-transport-ws
graphql-transport-ws
/ newer graphql-ws server setup - simpleserver complete example with subscriptions, mutations and queries