decrease query complexity

This commit is contained in:
Dawid Wysokiński 2021-04-22 19:34:47 +02:00
parent f4b667d1bb
commit c51abee565
2 changed files with 9 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/pkg/errors"
"github.com/tribalwarshelp/api/graphql/querycomplexity"
"github.com/tribalwarshelp/api/middleware"
"time"
@ -19,7 +20,7 @@ import (
)
const (
endpointMain = "/graphql"
endpointGraphQL = "/graphql"
endpointPlayground = "/"
playgroundTTL = time.Hour / time.Second
)
@ -31,11 +32,11 @@ type Config struct {
func Attach(cfg Config) error {
if cfg.Resolver == nil {
return fmt.Errorf("Graphql resolver cannot be nil")
return errors.New("Graphql resolver cannot be nil")
}
gqlHandler := graphqlHandler(prepareConfig(cfg.Resolver))
cfg.RouterGroup.GET(endpointMain, gqlHandler)
cfg.RouterGroup.POST(endpointMain, gqlHandler)
cfg.RouterGroup.GET(endpointGraphQL, gqlHandler)
cfg.RouterGroup.POST(endpointGraphQL, gqlHandler)
cfg.RouterGroup.GET(endpointPlayground, playgroundHandler())
return nil
}
@ -57,7 +58,7 @@ func graphqlHandler(cfg generated.Config) gin.HandlerFunc {
if middleware.CanExceedLimit(ctx) {
return 500000000
}
return 18000
return 12000
},
})
@ -69,7 +70,7 @@ func graphqlHandler(cfg generated.Config) gin.HandlerFunc {
// Defining the Playground handler
func playgroundHandler() gin.HandlerFunc {
h := playground.Handler("Playground", endpointMain)
h := playground.Handler("Playground", endpointGraphQL)
return func(c *gin.Context) {
c.Header("Cache-Control", fmt.Sprintf(`public, max-age=%d`, playgroundTTL))

View File

@ -61,7 +61,6 @@ func init() {
if mode.Get() == mode.DevelopmentMode {
godotenv.Load(".env.development")
}
log.Printf(os.Getenv("GIN_MODE"))
}
func main() {
@ -74,7 +73,7 @@ func main() {
})
defer func() {
if err := db.Close(); err != nil {
log.Fatal("While disconnecting from the database:", err)
log.Fatalln("Couldn't close the db connections:", err)
}
}()
if strings.ToUpper(os.Getenv("LOG_DB_QUERIES")) == "TRUE" {
@ -177,7 +176,7 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
log.Fatalln("Couldn't shutdown the server", err)
}
log.Println("Server exiting")
}