-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphql_server.go
45 lines (36 loc) · 1.06 KB
/
graphql_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
package server
import (
"context"
"fmt"
"github.com/go-chi/chi"
"github.com/jjauzion/ws-backend/db"
"github.com/jjauzion/ws-backend/graph/playground"
"net/http"
"github.com/99designs/gqlgen/graphql/handler"
"go.uber.org/zap"
"github.com/jjauzion/ws-backend/graph"
)
func RunGraphQL(bootstrap bool) {
ctx := context.Background()
app, resolver, err := buildApplication()
if err != nil {
return
}
if bootstrap {
err = db.Bootstrap(ctx, app.dbal)
if err != nil {
resolver.Log.Error("bootstrap failed", zap.Error(err))
return
}
}
router := chi.NewRouter()
router.Use(app.auth.Middleware())
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: resolver}))
router.Handle("/query", srv)
if app.conf.IS_DEV_ENV {
router.Handle("/playground", playground.Handler("GraphQL playground", "/query"))
}
hostIp := resolver.ApiHost + ":" + resolver.ApiPort
resolver.Log.Info(fmt.Sprintf("connect to http://%s/playground for GraphQL playground", hostIp))
resolver.Log.Error("", zap.Error(http.ListenAndServe(hostIp, router)))
}