forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
40 lines (34 loc) · 1009 Bytes
/
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
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/integration"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/gqlerror"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
http.Handle("/", handler.Playground("GraphQL playground", "/query"))
http.Handle("/query", handler.GraphQL(
integration.NewExecutableSchema(integration.Config{Resolvers: &integration.Resolver{}}),
handler.ErrorPresenter(func(ctx context.Context, e error) *gqlerror.Error {
if e, ok := errors.Cause(e).(*integration.CustomError); ok {
return &gqlerror.Error{
Message: e.UserMessage,
Path: graphql.GetResolverContext(ctx).Path(),
}
}
return graphql.DefaultErrorPresenter(ctx, e)
}),
))
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}