Build a GraphQL API in Go by defining your schema, generating code, and wiring handlers to your data source.
package main
import (
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"net/http"
)
func main() {
h := handler.New(&executableSchema{})
h.AddTransport(playground.Handler("GraphQL Playground", "/graphql"))
h.AddTransport(http.NewTransport())
http.Handle("/graphql", h)
http.ListenAndServe(":8080", nil)
}
- Define your schema in
schema.graphqlto specify types and queries. - Run
go run github.com/99designs/gqlgen generateto create resolvers and executable schema. - Implement resolver logic in
resolver.goto fetch data for your queries. - Start the server with
http.ListenAndServeto expose the/graphqlendpoint.