✨ Golang server implementing the GraphQL over WebSocket protocol
- Supports queries, mutations, and subscriptions
- Tested with Apollo WebSocket client
- Built-in authentication mechanism
- Example projects for reference
The graphql-go/graphql package is the required package for defining a GraphQL schema. For example:
userResolver := func(p graphql.ResolveParams) (interface{}, error) {
return testUser{1, "test user", "test@user.com"}, nil
}
userType := graphql.NewObject(
graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int},
"name": &graphql.Field{Type: graphql.String},
"email": &graphql.Field{Type: graphql.String},
},
},
)
queryType := graphql.NewObject(
graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"getUser": &graphql.Field{
Type: userType,
Resolve: userResolver,
Description: "Get test user",
},
},
},
)
if schema, err := graphql.NewSchema(
graphql.SchemaConfig{
Query: queryType,
},
); err != nil {
return nil, err
} else {
return &schema, nil
}
The protocol is defined in enisdenjo/graphql-ws and consists of the following eight message types:
- ConnectionInit (client -> server)
- ConnectionAck (client <- server)
- Ping (client <-> server)
- Pong (client <-> server)
- Subscribe (client -> server)
- Next (client <- server)
- Error (client <- server)
- Complete (client <-> server)
…
…