github.com/senomas/gqlgen@v0.17.11-0.20220626120754-9aee61b0716a/integration/server/server.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  
    10  	"github.com/99designs/gqlgen/graphql"
    11  	"github.com/99designs/gqlgen/graphql/handler"
    12  	"github.com/99designs/gqlgen/graphql/handler/extension"
    13  	"github.com/99designs/gqlgen/graphql/playground"
    14  	"github.com/99designs/gqlgen/integration"
    15  	"github.com/vektah/gqlparser/v2/gqlerror"
    16  )
    17  
    18  const defaultPort = "8080"
    19  
    20  func main() {
    21  	port := os.Getenv("PORT")
    22  	if port == "" {
    23  		port = defaultPort
    24  	}
    25  
    26  	cfg := integration.Config{Resolvers: &integration.Resolver{}}
    27  	cfg.Complexity.Query.Complexity = func(childComplexity, value int) int {
    28  		// Allow the integration client to dictate the complexity, to verify this
    29  		// function is executed.
    30  		return value
    31  	}
    32  
    33  	srv := handler.NewDefaultServer(integration.NewExecutableSchema(cfg))
    34  	srv.SetErrorPresenter(func(ctx context.Context, e error) *gqlerror.Error {
    35  		var ie *integration.CustomError
    36  		if errors.As(e, &ie) {
    37  			return &gqlerror.Error{
    38  				Message: ie.UserMessage,
    39  				Path:    graphql.GetPath(ctx),
    40  			}
    41  		}
    42  		return graphql.DefaultErrorPresenter(ctx, e)
    43  	})
    44  	srv.Use(extension.FixedComplexityLimit(1000))
    45  
    46  	http.Handle("/", playground.Handler("GraphQL playground", "/query"))
    47  	http.Handle("/query", srv)
    48  
    49  	log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
    50  	log.Fatal(http.ListenAndServe(":"+port, nil))
    51  }