Last active
December 12, 2017 09:53
-
-
Save Jannis/e6f905f9517e126292f3c8473994f2ae to your computer and use it in GitHub Desktop.
Combining GraphQL over Websocket with graphql-go/handler
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" | |
"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