complexityreporter

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 3 Imported by: 0

README

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))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExtension

func NewExtension(complexityReporter ComplexityReporter, opts ...Option) *extension

NewExtension sets a logger/tracer which reports a query complexity exceeds a threshold

func WithThreshold

func WithThreshold(threshold int) func(*extension)

Types

type ComplexityReporter

type ComplexityReporter interface {
	ReportComplexity(ctx context.Context, operationName string, complexity int)
}

ComplexityReporter is an interface for reporting a query complexity exceeds a threshold

type Option

type Option func(*extension)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL