Note: We're looking for a new maintainer for graphqlws
. Please reach out via jannis@thegraph.com if you're interested.
graphqlws
Implementation of the GraphQL over WebSocket protocol in Go.
Brought to you by Functional Foundry.
API Documentation
![Go Report](https://goreportcard.com/badge/github.com/functionalfoundry/graphqlws)
Getting started
- 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
Usage
Setup
package main
import (
"net/http"
"github.com/functionalfoundry/graphqlws"
"github.com/graphql-go/graphql"
)
func main() {
schema, err := graphql.NewSchema(...)
subscriptionManager := graphqlws.NewSubscriptionManager(&schema)
graphqlwsHandler := graphqlws.NewHandler(graphqlws.HandlerConfig{
SubscriptionManager: subscriptionManager,
Authenticate: func(authToken string) (interface{}, error) {
return "Joe", nil
},
})
http.Handle("/subscriptions", graphqlwsHandler)
http.ListenAndServe(":8080", nil)
}
Working with subscriptions
subscriptions := subscriptionManager.Subscriptions()
for conn, _ := range subscriptions {
conn.ID()
conn.User()
for _, subscription := range subscriptions[conn] {
subscription.ID
subscription.OperationName
subscription.Query
subscription.Variables
subscription.Document
subscription.Fields
subscription.Connection
ctx := context.Context()
params := graphql.Params{
Schema: schema,
RequestString: subscription.Query,
VariableValues: subscription.Variables,
OperationName: subscription.OperationName,
Context: ctx,
}
result := graphql.Do(params)
data := graphqlws.DataMessagePayload{
Data: result.Data,
Errors: graphqlws.ErrorsFromGraphQLErrors(result.Errors),
}
subscription.SendData(&data)
}
}
Logging
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)
License
Copyright © 2017-2019 Functional Foundry, LLC.
Licensed under the MIT License.