Skip to content

Commit

Permalink
feat(handler): do not apply cookies after flamingo ran
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianccm committed Apr 22, 2022
1 parent 7f139a5 commit fd28798
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions graphqlhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package graphql

import (
"context"
"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(ctx 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)
return err
}
2 changes: 1 addition & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (r *routes) Routes(registry *web.RouterRegistry) {

registry.MustRoute("/graphql", "graphql")
registry.HandleOptions("graphql", web.WrapHTTPHandler(corsHandler.preflightHandler()))
registry.HandleAny("graphql", web.WrapHTTPHandler(corsHandler.gqlMiddleware(gqlHandler)))
registry.HandleAny("graphql", wrapGqlHandler(corsHandler.gqlMiddleware(gqlHandler)))

registry.MustRoute("/graphql-console", "graphql.console")
u, _ := r.reverseRouter.Relative("graphql", nil)
Expand Down

0 comments on commit fd28798

Please sign in to comment.