git.sr.ht/~sircmpwn/gqlgen@v0.0.0-20200522192042-c84d29a1c940/integration/server/server.go (about) 1 package main 2 3 import ( 4 "context" 5 "log" 6 "net/http" 7 "os" 8 9 "git.sr.ht/~sircmpwn/gqlgen/graphql/handler/extension" 10 11 "git.sr.ht/~sircmpwn/gqlgen/graphql" 12 "git.sr.ht/~sircmpwn/gqlgen/graphql/handler" 13 "git.sr.ht/~sircmpwn/gqlgen/graphql/playground" 14 "git.sr.ht/~sircmpwn/gqlgen/integration" 15 "github.com/pkg/errors" 16 "github.com/vektah/gqlparser/v2/gqlerror" 17 ) 18 19 const defaultPort = "8080" 20 21 func main() { 22 port := os.Getenv("PORT") 23 if port == "" { 24 port = defaultPort 25 } 26 27 cfg := integration.Config{Resolvers: &integration.Resolver{}} 28 cfg.Complexity.Query.Complexity = func(childComplexity, value int) int { 29 // Allow the integration client to dictate the complexity, to verify this 30 // function is executed. 31 return value 32 } 33 34 srv := handler.NewDefaultServer(integration.NewExecutableSchema(cfg)) 35 srv.SetErrorPresenter(func(ctx context.Context, e error) *gqlerror.Error { 36 if e, ok := errors.Cause(e).(*integration.CustomError); ok { 37 return &gqlerror.Error{ 38 Message: e.UserMessage, 39 Path: graphql.GetFieldContext(ctx).Path(), 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 }