Skip to content

Instantly share code, notes, and snippets.

@schaternik
Created November 19, 2019 10:42
Show Gist options
  • Save schaternik/9acacc9064155f40cd66b86c4fd18e9a to your computer and use it in GitHub Desktop.
Save schaternik/9acacc9064155f40cd66b86c4fd18e9a to your computer and use it in GitHub Desktop.
Graphql server in go with Graphiql handling one query
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