From adfac6aa2e952c90a9756200877a9ed0a3ebd330 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Tue, 27 Aug 2024 15:12:19 -0600 Subject: [PATCH] Making handler paths customizable --- graphql.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/graphql.go b/graphql.go index d8d09f7..d122538 100644 --- a/graphql.go +++ b/graphql.go @@ -30,6 +30,10 @@ type GraphQL struct { Scopes []string MaxComplexity int MaxUploadSize int + + QueryPath string + ScopesPath string + PlaygroundPath string } type GQLExtension struct { @@ -81,23 +85,25 @@ func (g *GQLExtension) Extend(gserver *server.Server) (*server.Server, error) { srv.SetRecoverFunc(g.gqlEmailRecover) srv.Use(extension.FixedComplexityLimit(g.GQL.MaxComplexity)) - qroute := g.eg.POST("/query", func(c echo.Context) error { + qroute := g.eg.POST(g.GQL.QueryPath, func(c echo.Context) error { // Hate this... ctx := server.EchoContext(c.Request().Context(), c) c.SetRequest(c.Request().WithContext(ctx)) srv.ServeHTTP(c.Response(), c.Request()) return nil }) - g.eg.GET("/query/api-scopes.json", func(c echo.Context) error { - info := struct { - Scopes []string `json:"scopes"` - }{g.GQL.Scopes} + if g.GQL.ScopesPath != "" { + g.eg.GET(g.GQL.ScopesPath, func(c echo.Context) error { + info := struct { + Scopes []string `json:"scopes"` + }{g.GQL.Scopes} - return c.JSON(http.StatusOK, &info) - }) - if g.s.Config.Debug { + return c.JSON(http.StatusOK, &info) + }) + } + if g.GQL.PlaygroundPath != "" { // If debug is set, add a handler for the default playground - g.eg.GET("/graphql-debug", func(c echo.Context) error { + g.eg.GET(g.GQL.PlaygroundPath, func(c echo.Context) error { // Hate this... ctx := server.EchoContext(c.Request().Context(), c) c.SetRequest(c.Request().WithContext(ctx)) @@ -210,6 +216,6 @@ The following stack trace was produced: // NewGQLExtension will return a GQLExtension that can be passed to a gobwebs server func NewGQLExtension(eg *echo.Group, schema graphql.ExecutableSchema, scopes []string) *GQLExtension { - gql := &GraphQL{Schema: schema, Scopes: scopes} + gql := &GraphQL{Schema: schema, Scopes: scopes, QueryPath: "/query"} return &GQLExtension{GQL: gql, eg: eg} } -- 2.45.2