forked from i-love-flamingo/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphqlhandler.go
46 lines (38 loc) · 885 Bytes
/
graphqlhandler.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
package graphql
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"flamingo.me/flamingo/v3/framework/web"
)
type (
gqlHandler struct {
recorder *httptest.ResponseRecorder
request *web.Request
}
)
// wrapGqlHandler wraps an http.Handler to be used in the flamingo http package
func wrapGqlHandler(handler http.Handler) web.Action {
return func(ctx context.Context, req *web.Request) web.Result {
rw := httptest.NewRecorder()
handler.ServeHTTP(rw, req.Request().WithContext(ctx))
return &gqlHandler{
request: req,
recorder: rw,
}
}
}
func (h *gqlHandler) Apply(_ context.Context, rw http.ResponseWriter) error {
for k, vs := range h.recorder.Header() {
for _, v := range vs {
rw.Header().Add(k, v)
}
}
_, err := io.Copy(rw, h.recorder.Body)
if err != nil {
return fmt.Errorf("failed to write body: %w", err)
}
return nil
}