github.com/ultraware/gqlgen@v0.7.2/integration/server/server.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/99designs/gqlgen/graphql"
    10  	"github.com/99designs/gqlgen/handler"
    11  	"github.com/99designs/gqlgen/integration"
    12  	"github.com/pkg/errors"
    13  	"github.com/vektah/gqlparser/gqlerror"
    14  )
    15  
    16  const defaultPort = "8080"
    17  
    18  func main() {
    19  	port := os.Getenv("PORT")
    20  	if port == "" {
    21  		port = defaultPort
    22  	}
    23  
    24  	http.Handle("/", handler.Playground("GraphQL playground", "/query"))
    25  	http.Handle("/query", handler.GraphQL(
    26  		integration.NewExecutableSchema(integration.Config{Resolvers: &integration.Resolver{}}),
    27  		handler.ErrorPresenter(func(ctx context.Context, e error) *gqlerror.Error {
    28  			if e, ok := errors.Cause(e).(*integration.CustomError); ok {
    29  				return &gqlerror.Error{
    30  					Message: e.UserMessage,
    31  					Path:    graphql.GetResolverContext(ctx).Path(),
    32  				}
    33  			}
    34  			return graphql.DefaultErrorPresenter(ctx, e)
    35  		}),
    36  	))
    37  
    38  	log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
    39  	log.Fatal(http.ListenAndServe(":"+port, nil))
    40  }