Skip to content

Instantly share code, notes, and snippets.

@Jannis
Last active December 12, 2017 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jannis/6ce396f59b261f74d11f78bebca74c82 to your computer and use it in GitHub Desktop.
Save Jannis/6ce396f59b261f74d11f78bebca74c82 to your computer and use it in GitHub Desktop.
Create a GraphQL over WebSocket endpoint with graphqlws
package main
import (
"net/http"
"github.com/functionalfoundry/graphqlws"
"github.com/graphql-go/graphql"
)
func main() {
// Create a GraphQL schema with a subscription object
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Subscription: graphql.NewObject(...),
})
// Create a subscription manager
subscriptionManager := graphqlws.NewSubscriptionManager(&schema)
// Create a WebSocket/HTTP handler
graphqlwsHandler := graphqlws.NewHandler(graphqlws.HandlerConfig{
// Wire up the GraphqL WebSocket handler with the subscription manager
SubscriptionManager: subscriptionManager,
// Optional: Add a hook to resolve auth tokens into users that are
// then stored on the GraphQL WS connections
Authenticate: func(authToken string) (interface{}, error) {
// This is just a dumb example
return "Joe", nil
},
})
// The handler integrates seamlessly with existing HTTP servers
http.Handle("/subscriptions", graphqlwsHandler)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment