operation_complexity

package
v2.21.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package operation_complexity implements two common algorithms used by GitHub to calculate GraphQL query complexity:

  1. Node count, the maximum number of Nodes a query may return
  2. Complexity, the maximum number of Node requests that might be needed to execute the query

OperationComplexityEstimator takes a schema definition and a query and then walks recursively through the query to calculate both variables.

The calculation can be influenced by integer arguments on fields that indicate the number of Nodes returned by a field.

To help the algorithm understand the schema make use of these two directives:

  • directive @nodeCountMultiply on ARGUMENT_DEFINITION
  • directive @nodeCountSkip on FIELD

"nodeCountMultiply" indicates that the Int value the directive is applied on should be used as a Node multiplier.

"nodeCountSkip" indicates that the algorithm should skip this Node. It can be used to allowlist certain query paths.

Note: Introspection fields (__schema and __type) are automatically skipped from complexity calculations by default.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateOperationComplexity deprecated

func CalculateOperationComplexity(operation, definition *ast.Document, report *operationreport.Report) (OperationStats, []RootFieldStats)

Deprecated: use NewOperationComplexityEstimator.

Types

type OperationComplexityEstimator

type OperationComplexityEstimator struct {
	// contains filtered or unexported fields
}

OperationComplexityEstimator estimates stats for normalized operations. It may be reused sequentially, but is not safe for concurrent use.

func NewOperationComplexityEstimator

func NewOperationComplexityEstimator(skipIntrospection bool) *OperationComplexityEstimator

NewOperationComplexityEstimator creates an estimator. If skipIntrospection is true, __schema and __type root fields are excluded.

func (*OperationComplexityEstimator) Do

func (n *OperationComplexityEstimator) Do(operation, definition *ast.Document, report *operationreport.Report) (OperationStats, []RootFieldStats)

Do returns global and per-root-field estimates for the operation.

type OperationStats

type OperationStats struct {
	// NodeCount is the maximum number of returned nodes.
	NodeCount int
	// Complexity is the maximum number of field requests.
	Complexity int
	// Depth is the maximum number of response field levels on a path.
	// Root-field depth is relative to the root.
	Depth int
}

OperationStats contains estimates for an operation or root field.

type RootFieldStats

type RootFieldStats struct {
	TypeName  string
	FieldName string
	Alias     string
	Stats     OperationStats
}

RootFieldStats contains the stats for one top-level field. Alias is empty when the response name is the same as FieldName.

Jump to

Keyboard shortcuts

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