Note: We're looking for a new maintainer for graphqlws. Please reach out via jannis@thegraph.com if you're interested.
Implementation of the GraphQL over WebSocket protocol in Go. Brought to you by Functional Foundry.
- Install dependencies:
go get github.com/sirupsen/logrus go get github.com/x-cray/logrus-prefixed-formatter go get github.com/google/uuid go get github.com/gorilla/websocket go get github.com/graphql-go/graphql
- Clone the repository:
mkdir -p "$GOPATH/src/github.com/functionalfoundry"cd"$GOPATH/src/github.com/functionalfoundry" git clone https://github.com/functionalfoundry/graphqlws
- Run the tests:
cd graphqlws go test
- Run the example server:
go run graphqlws/examples/server
package main
import (
"net/http""github.com/functionalfoundry/graphqlws""github.com/graphql-go/graphql"
)
funcmain() {
// Create a GraphQL schemaschema, err:= graphql.NewSchema(...)
// Create a subscription manager
subscriptionManager :=graphqlws.NewSubscriptionManager(&schema)
// Create a WebSocket/HTTP handlergraphqlwsHandler:= graphqlws.NewHandler(graphqlws.HandlerConfig{
// Wire up the GraphqL WebSocket handler with the subscription managerSubscriptionManager: subscriptionManager,
// Optional: Add a hook to resolve auth tokens into users that are// then stored on the GraphQL WS connectionsAuthenticate: func(authTokenstring) (interface{}, error) {
// This is just a dumb examplereturn"Joe", nil
},
})
// The handler integrates seamlessly with existing HTTP servershttp.Handle("/subscriptions", graphqlwsHandler)
http.ListenAndServe(":8080", nil)
}// This assumes you have access to the above subscription managersubscriptions:=subscriptionManager.Subscriptions()
forconn, _:=rangesubscriptions {
// Things you have access to here:conn.ID() // The connection IDconn.User() // The user returned from the Authenticate functionfor_, subscription:=rangesubscriptions[conn] {
// Things you have access to here:subscription.ID// The subscription ID (unique per conn)subscription.OperationName// The name of the operationsubscription.Query// The subscription query/queries stringsubscription.Variables// The subscription variablessubscription.Document// The GraphQL AST for the subscriptionsubscription.Fields// The names of top-level queriessubscription.Connection// The GraphQL WS connection// Prepare an execution context for running the queryctx:=context.Context()
// Re-execute the subscription queryparams:= graphql.Params{
Schema: schema, // The GraphQL schemaRequestString: subscription.Query,
VariableValues: subscription.Variables,
OperationName: subscription.OperationName,
Context: ctx,
}
result:=graphql.Do(params)
// Send query results back to the subscriber at any pointdata:= graphqlws.DataMessagePayload{
// Data can be anything (interface{})Data: result.Data,
// Errors is optional ([]error)Errors: graphqlws.ErrorsFromGraphQLErrors(result.Errors),
}
subscription.SendData(&data)
}
}graphqlws uses logrus for logging.
In the future we might remove those logs entirely to leave logging entirely to developers
using graphqlws. Given the current solution, you can control the logging level of
graphqlws by setting it through logrus:
import (
log "github.com/sirupsen/logrus"
)
...log.SetLevel(log.WarnLevel)Copyright © 2017-2019 Functional Foundry, LLC.
Licensed under the MIT License.