Created
December 10, 2021 04:46
-
-
Save manakuro/c8eeafa011350250657894c16402a4e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package router | |
import ( | |
"net/http" | |
"github.com/99designs/gqlgen/graphql/handler" | |
"github.com/99designs/gqlgen/graphql/playground" | |
"github.com/labstack/echo/v4" | |
"github.com/labstack/echo/v4/middleware" | |
) | |
// Path of route | |
const ( | |
QueryPath = "/query" | |
PlaygroundPath = "/playground" | |
) | |
// New creates route endpoint | |
func New(srv *handler.Server) *echo.Echo { | |
e := echo.New() | |
e.Use(middleware.Recover()) | |
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ | |
AllowOrigins: []string{"*"}, | |
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodOptions}, | |
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderXRequestedWith, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization}, | |
})) | |
{ | |
e.POST(QueryPath, echo.WrapHandler(srv)) | |
e.GET(PlaygroundPath, func(c echo.Context) error { | |
playground.Handler("GraphQL", QueryPath).ServeHTTP(c.Response(), c.Request()) | |
return nil | |
}) | |
} | |
return e | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment