-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
50 lines (45 loc) · 1.44 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/graph-gophers/graphql-go"
"github.com/huantt/go-graphql-sample/adapter"
"github.com/huantt/go-graphql-sample/config"
"github.com/huantt/go-graphql-sample/graphql/loader"
"github.com/huantt/go-graphql-sample/graphql/resolver"
"github.com/huantt/go-graphql-sample/graphql/schema"
"github.com/huantt/go-graphql-sample/graphql/tracer"
"github.com/huantt/go-graphql-sample/middleware"
"github.com/huantt/go-graphql-sample/pkg/log"
)
func main() {
cfg, err := config.Load()
if err != nil {
panic(err)
}
cfg.Log.Build()
schemaString, err := schema.String()
if err != nil {
log.Fatalf("reading embedded schema contents: %schemaString", err)
}
jsonPlaceholder := adapter.NewJsonPlaceHolder()
rootResolver, err := resolver.NewRootResolver(jsonPlaceholder)
if err != nil {
log.Fatalf("creating rootResolver resolver: %schemaString", err)
}
graphqlSchema := graphql.MustParseSchema(
schemaString,
rootResolver,
graphql.MaxParallelism(cfg.Graphql.MaxParallelism),
graphql.MaxDepth(cfg.Graphql.MaxDepth),
graphql.Tracer(tracer.NewCustomTracer()),
)
loaders := loader.Init(jsonPlaceholder)
service := gin.Default()
service.Use(middleware.AddRequestId)
service.Use(middleware.CORS(cfg.AllowedOrigins))
service.POST("/graphql", middleware.NewHandler(graphqlSchema, loaders))
if err := service.Run(fmt.Sprintf("0.0.0.0:%d", cfg.Port)); err != nil {
log.Fatal(err)
}
}