Skip to content

Instantly share code, notes, and snippets.

@Jannis
Last active December 12, 2017 09:53
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/e6f905f9517e126292f3c8473994f2ae to your computer and use it in GitHub Desktop.
Save Jannis/e6f905f9517e126292f3c8473994f2ae to your computer and use it in GitHub Desktop.
Combining GraphQL over Websocket with graphql-go/handler
package main
import (
"net/http"
"github.com/functionalfoundry/graphqlws"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
)
func main() {
// Create a GraphQL schema with a subscription object
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Subscription: graphql.NewObject(...),
})
// Create a GraphQL over WebSocket handler
subscriptionManager := graphqlws.NewSubscriptionManager(&schema)
graphqlwsHandler := graphqlws.NewHandler(graphqlws.HandlerConfig{
SubscriptionManager: subscriptionManager,
})
// Create a GraphQL over HTTP handler
graphqlHandler := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
})
// Serve both handlers on different endpoints
http.Handle("/", graphqlHandler)
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