generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resolver.go
136 lines (111 loc) · 3.08 KB
/
resolver.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package graphapi
import (
"fmt"
"net/http"
"entgo.io/contrib/entgql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
echo "github.com/datumforge/echox"
"github.com/wundergraph/graphql-go-tools/pkg/playground"
"go.uber.org/zap"
ent "github.com/datumforge/datum/internal/ent/generated"
)
// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.
const (
ActionGet = "get"
ActionUpdate = "update"
ActionDelete = "delete"
ActionCreate = "create"
)
var (
graphPath = "query"
playgroundPath = "playground"
graphFullPath = fmt.Sprintf("/%s", graphPath)
)
// Resolver provides a graph response resolver
type Resolver struct {
client *ent.Client
logger *zap.SugaredLogger
authDisabled bool
}
// NewResolver returns a resolver configured with the given ent client
func NewResolver(client *ent.Client, authEnabled bool) *Resolver {
return &Resolver{
client: client,
// do not disable auth by default
authDisabled: !authEnabled,
}
}
func (r Resolver) WithLogger(l *zap.SugaredLogger) *Resolver {
r.logger = l
return &r
}
// Handler is an http handler wrapping a Resolver
type Handler struct {
r *Resolver
graphqlHandler *handler.Server
playground *playground.Playground
middleware []echo.MiddlewareFunc
}
// Handler returns an http handler for a graph resolver
func (r *Resolver) Handler(withPlayground bool, middleware ...echo.MiddlewareFunc) *Handler {
srv := handler.NewDefaultServer(
NewExecutableSchema(
Config{
Resolvers: r,
},
),
)
// add transactional db client
WithTransactions(srv, r.client)
srv.Use(extension.Introspection{})
h := &Handler{
r: r,
middleware: middleware,
graphqlHandler: srv,
}
if withPlayground {
h.playground = playground.New(playground.Config{
PathPrefix: "/",
PlaygroundPath: playgroundPath,
GraphqlEndpointPath: graphFullPath,
})
}
return h
}
// WithTransactions adds the transactioner to the ent db client
func WithTransactions(h *handler.Server, c *ent.Client) {
// setup transactional db client
h.AroundOperations(injectClient(c))
h.Use(entgql.Transactioner{TxOpener: c})
}
// Handler returns the http.HandlerFunc for the GraphAPI
func (h *Handler) Handler() http.HandlerFunc {
return h.graphqlHandler.ServeHTTP
}
// Routes for the the server
func (h *Handler) Routes(e *echo.Group) {
e.Use(h.middleware...)
e.POST("/"+graphPath, func(c echo.Context) error {
h.graphqlHandler.ServeHTTP(c.Response(), c.Request())
return nil
})
if h.playground != nil {
handlers, err := h.playground.Handlers()
if err != nil {
h.r.logger.Fatal("error configuring playground handlers", "error", err)
return
}
for i := range handlers {
// with the function we need to dereference the handler so that it remains
// the same in the function below
hCopy := handlers[i].Handler
e.GET(handlers[i].Path, func(c echo.Context) error {
hCopy.ServeHTTP(c.Response(), c.Request())
return nil
})
}
}
}