generated from datumforge/go-template
/
resolver.go
186 lines (153 loc) · 4.31 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package graphapi
import (
"context"
"fmt"
"net/http"
"time"
"entgo.io/contrib/entgql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
echo "github.com/datumforge/echox"
"github.com/gorilla/websocket"
"github.com/ravilushqa/otelgqlgen"
"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
}
// NewResolver returns a resolver configured with the given ent client
func NewResolver(client *ent.Client) *Resolver {
return &Resolver{
client: client,
}
}
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) *Handler {
srv := handler.NewDefaultServer(
NewExecutableSchema(
Config{
Resolvers: r,
},
),
)
srv.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second, // nolint: gomnd
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
})
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{})
srv.SetQueryCache(lru.New(1000)) // nolint:gomnd
srv.Use(extension.Introspection{})
srv.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100), // nolint:gomnd
})
// add transactional db client
WithTransactions(srv, r.client)
// add analytics
WithEvents(r.client)
srv.Use(otelgqlgen.Middleware())
h := &Handler{
r: r,
graphqlHandler: srv,
}
if withPlayground {
h.playground = playground.New(playground.Config{
PathPrefix: "/",
PlaygroundPath: playgroundPath,
GraphqlEndpointPath: graphFullPath,
})
}
return h
}
func WithEvents(c *ent.Client) {
// Add a global hook that runs on all types and all operations.
c.Use(func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
retVal, err := next.Mutate(ctx, m)
if err != nil {
return retVal, err
}
if TrackedEvent(m) {
CreateEvent(c, m, retVal)
}
return retVal, nil
})
})
}
// 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...)
// Create the default POST graph endpoint
e.POST("/"+graphPath, func(c echo.Context) error {
h.graphqlHandler.ServeHTTP(c.Response(), c.Request())
return nil
})
// Create a GET query endpoint in order to create short queries with a query string
e.GET("/"+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
})
}
}
}