gqlcost

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 10 Imported by: 2

README

koron-go/gqlcost

PkgGoDev Actions/Go Go Report Card

gqlcost provides cost analysis validation rule for graphql-go/graphql. This is a port of pa-bru/graphql-cost-analysis.

gqlcost supports only cost map, and not support @cost directive for now. Because graphql-go/graphql's graphql.Schema, Object and Field don't have spaces to store values for directives.

Getting started

Put this codes to your project.

It adds a cost analysis validation rule to graphql.SpecifiedRules Currently, graphql-go/graphql doesn't have any methods to customize validation rules exclude modifing graphql.SpecifiedRules.

import "github.com/koron-go/gqlcost"

func init() {
    gqlcost.AddCostAnalysisRule(gqlcost.AnalysisOptions{
        // FIXME: modify maximum cost as you need.
        MaximumCost: 1000,
        // CostMap defines cost for types and fields.
        CostMap: gqlcost.CostMap{
            // FIXME: modify CostMap as you need.
            "Query": gqlcost.TypeCost{
                // cost for "Query" type.
                Cost: &gqlcost.Cost{Complexity: 1},
                // cost for fields of "Query" type.
                Fields: gqlcost.FieldsCost{
                    "todoList": gqlcost.Cost{
                        UseMultipliers: true,
                        Complexity:     2,
                        Multipliers:    []string{"limit"},
                    },
                    // TODO: add other fields.
                },
            },
            // TODO: add other costs for types by using `TypeCost`
        },
    })
}

Documentation

Overview

Package gqlcost provides cost analysis validation rule for github.com/graphql-go/graph

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCostAnalysisRule

func AddCostAnalysisRule(opts AnalysisOptions)

AddCostAnalysisRule adds a rule of cost analysis to graphql.SpecifiedRules.

func AnalysisRule

func AnalysisRule(opts AnalysisOptions) graphql.ValidationRuleFn

AnalysisRule provides cost analysis rule (function)

Types

type AnalysisOptions

type AnalysisOptions struct {
	MaximumCost int
	DefaultCost int
	Valiables   map[string]interface{}

	CostMap         CostMap
	ComplexityRange ComplexityRange
}

AnalysisOptions provides options for cost analysis.

type ComplexityRange

type ComplexityRange struct {
	Min int
	Max int
}

ComplexityRange provides valid complexity min and max values.

type Cost

type Cost struct {
	// UseMultipliers is flag to use multiplier.
	// Multipliers and MultiplierFunc are refered only when this is true.
	UseMultipliers bool `json:"useMultipliers,omitempty"`

	// Complexity define default complexity of field or type.
	Complexity int `json:"complexity,omitempty"`

	// Multipliers enumerates name of arguments to be used to calculate
	// multiplier.
	Multipliers []string `json:"multipliers,omitempty"`

	// MultiplierFunc is for customizing multiplier calculation.
	// When it available Multipliers is ignored.
	MultiplierFunc func(map[string]interface{}) int
}

Cost provides each cost value for type.field

type CostMap

type CostMap map[string]TypeCost

CostMap provides costs for type and fields.

type FieldsCost

type FieldsCost map[string]Cost

FieldsCost provides costs for each fields.

type TypeCost

type TypeCost struct {
	// Cost is cost of type itself
	Cost *Cost
	// Fields is costs for each fields.
	Fields FieldsCost
}

TypeCost provides costs for a type and its fields.

Jump to

Keyboard shortcuts

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