Created
November 19, 2019 10:42
-
-
Save schaternik/9acacc9064155f40cd66b86c4fd18e9a to your computer and use it in GitHub Desktop.
Graphql server in go with Graphiql handling one query
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/friendsofgo/graphiql" | |
graphql "github.com/graph-gophers/graphql-go" | |
"github.com/graph-gophers/graphql-go/relay" | |
) | |
type query struct{} | |
func (*query) Hello() string { return "Hello, world!" } | |
func main() { | |
s := ` | |
type Query { | |
hello: String! | |
} | |
` | |
schema := graphql.MustParseSchema(s, &query{}) | |
graphiqlHandler, err := graphiql.NewGraphiqlHandler("/graphql") | |
if err != nil { | |
panic(err) | |
} | |
http.Handle("/graphql", &relay.Handler{Schema: schema}) | |
http.Handle("/graphiql", graphiqlHandler) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment