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