/
httpserver.go
55 lines (48 loc) · 1.13 KB
/
httpserver.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
52
53
54
55
package httpserver
import (
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/httpserver"
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"go.uber.org/zap"
)
type Params struct {
fx.In
Schema lazy.Lazy[graphql.ExecutableSchema]
Logger *zap.SugaredLogger
}
type Result struct {
fx.Out
Option httpserver.Option `group:"http_server_options"`
}
func New(p Params) Result {
return Result{
Option: &builder{
schema: p.Schema,
},
}
}
type builder struct {
schema lazy.Lazy[graphql.ExecutableSchema]
}
func (builder) Key() string {
return "graphql"
}
func (b builder) Apply(e *gin.Engine) error {
schema, err := b.schema.Get()
if err != nil {
return err
}
gql := handler.NewDefaultServer(schema)
e.POST("/graphql", func(c *gin.Context) {
gql.ServeHTTP(c.Writer, c.Request)
})
pg := playground.Handler("GraphQL playground", "/graphql")
e.GET("/graphql", func(c *gin.Context) {
pg.ServeHTTP(c.Writer, c.Request)
})
return nil
}