Last active
December 12, 2017 09:54
-
-
Save Jannis/6ce396f59b261f74d11f78bebca74c82 to your computer and use it in GitHub Desktop.
Create a GraphQL over WebSocket endpoint with graphqlws
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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