Skip to content

This is a simple middleware of gqlgen to measure the complexity of a GraphQL query.

License

Notifications You must be signed in to change notification settings

basemachina/gqlgen-complexity-reporter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gqlgen-complexity-reporter

This is a simple middleware of gqlgen to measure the complexity of a GraphQL query.

Installation

go get github.com/basemachina/gqlgen-complexity-reporter

Usage

package main

import (
    ...
    complexityreporter "github.com/basemachina/gqlgen-complexity-reporter"
)

type reporter struct {
    logger *zap.Logger
}

func (r *reporter) ReportComplexity(ctx context.Context, operationName string, complexity int) {
    r.logger.Info("[graphql query complexity]", zap.Int("complexity", complexity))
}

func main() {
    srv := handler.NewDefaultServer(internal.NewExecutableSchema(internal.Config{
        ...
    }))
    logger, _ := zap.NewProduction()
    r := &reporter{logger: logger}
    srv.Use(complexityreporter.NewExtension(r, complexityreporter.WithThreshold(100))) // 100 is the maximum complexity allowed

    http.Handle("/", playground.Handler("GraphQL playground", "/query"))
    http.Handle("/query", auth.AuthMiddleware(srv))

    log.Fatal(http.ListenAndServe(":"+port, nil))
}

About

This is a simple middleware of gqlgen to measure the complexity of a GraphQL query.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages