astnormalization

package
v2.14.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 18 Imported by: 19

Documentation

Overview

Package astnormalization helps to transform parsed GraphQL AST's into a easier to use structure.

Example

This example shows how the normalization package helps "simplify" a GraphQL AST.

Input:

subscription sub {
	... multipleSubscriptions
	... on Subscription {
		newMessage {
			body
			sender
		}
	}
}
fragment newMessageFields on Message {
	body: body
	sender
	... on Body {
		body
	}
}
fragment multipleSubscriptions on Subscription {
	newMessage {
		body
		sender
	}
	newMessage {
		... newMessageFields
	}
	newMessage {
		body
		body
		sender
	}
	... on Subscription {
		newMessage {
			body
			sender
		}
	}
	disallowedSecondRootField
}

Output:

subscription sub {
	newMessage {
		body
		sender
	}
	disallowedSecondRootField
}
fragment newMessageFields on Message {
	body
	sender
}
fragment multipleSubscriptions on Subscription {
	newMessage {
		body
		sender
	}
	disallowedSecondRootField
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InlineFragmentAddOnType

func InlineFragmentAddOnType(walker *astvisitor.Walker)

InlineFragmentAddOnType adds on type condition to inline fragments of the same type this is needed for the planner to work correctly Such typename will not be printed by astprinter

func NormalizeDefinition

func NormalizeDefinition(definition *ast.Document, report *operationreport.Report)

NormalizeDefinition creates a default DefinitionNormalizer and applies all rules to a given AST In case you're using DefinitionNormalizer in a hot path you shouldn't be using this function. Create a new DefinitionNormalizer using NewDefinitionNormalizer() instead and re-use it.

func NormalizeNamedOperation

func NormalizeNamedOperation(operation, definition *ast.Document, operationName []byte, report *operationreport.Report)

func NormalizeOperation

func NormalizeOperation(operation, definition *ast.Document, report *operationreport.Report)

NormalizeOperation creates a default Normalizer and applies all rules to a given AST In case you're using OperationNormalizer in a hot path, you shouldn't be using this function. Create a new OperationNormalizer using NewNormalizer() instead and re-use it.

func NormalizeSubgraphSDL

func NormalizeSubgraphSDL(definition *ast.Document, report *operationreport.Report)

Types

type AbstractFieldNormalizer

type AbstractFieldNormalizer struct {
	*astvisitor.Walker
	// contains filtered or unexported fields
}

func NewAbstractFieldNormalizer

func NewAbstractFieldNormalizer(operation *ast.Document, definition *ast.Document, fieldRef int) *AbstractFieldNormalizer

func (*AbstractFieldNormalizer) AllowVisitor

func (v *AbstractFieldNormalizer) AllowVisitor(kind astvisitor.VisitorKind, ref int, visitor any, skipFor astvisitor.SkipVisitors) bool

func (*AbstractFieldNormalizer) EnterField

func (v *AbstractFieldNormalizer) EnterField(ref int)

func (*AbstractFieldNormalizer) LeaveField

func (v *AbstractFieldNormalizer) LeaveField(ref int)

func (*AbstractFieldNormalizer) Normalize

func (v *AbstractFieldNormalizer) Normalize() error

type DefinitionNormalizer

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

DefinitionNormalizer walks a given AST and applies all registered rules

func NewDefinitionNormalizer

func NewDefinitionNormalizer() *DefinitionNormalizer

NewDefinitionNormalizer creates a new DefinitionNormalizer and sets up all default rules

func NewSubgraphDefinitionNormalizer

func NewSubgraphDefinitionNormalizer() *DefinitionNormalizer

func (*DefinitionNormalizer) NormalizeDefinition

func (o *DefinitionNormalizer) NormalizeDefinition(definition *ast.Document, report *operationreport.Report)

NormalizeDefinition applies all registered rules to the AST

type Depth

type Depth struct {
	SpreadRef  int
	Depth      int
	SpreadName ast.ByteSlice
	// contains filtered or unexported fields
}

Depth holds all necessary information to understand the Depth of a Fragment Node

type Depths

type Depths []Depth

func (Depths) ByRef

func (d Depths) ByRef(ref int) (int, bool)

type FragmentDefinitionRemoval

type FragmentDefinitionRemoval struct {
}

type FragmentSpreadDepth

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

FragmentSpreadDepth is a helper for nested Fragments to calculate the actual depth of a Fragment Node

func (*FragmentSpreadDepth) Get

func (r *FragmentSpreadDepth) Get(operation, definition *ast.Document, report *operationreport.Report, depths *Depths)

Get returns all FragmentSpread Depths for a given AST

type InlineArgument added in v2.12.0

type InlineArgument struct {
	ArgumentName string
	AncestorName string
	AncestorKind ast.NodeKind
	Path         string
	ValueKind    ast.ValueKind
	Position     position.Position
}

InlineArgument describes a single argument in an operation whose value was supplied inline (a literal) instead of as a variable.

func (InlineArgument) QualifiedName added in v2.12.0

func (a InlineArgument) QualifiedName() string

type InlineArgumentsValidationOptions added in v2.12.0

type InlineArgumentsValidationOptions struct {
	Enforce      bool
	ErrorMessage string
	ErrorCode    string
	StatusCode   int
}

type ListInputCoercion

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

func NewListInputCoercion

func NewListInputCoercion() *ListInputCoercion

func (*ListInputCoercion) CoerceInput

func (i *ListInputCoercion) CoerceInput(operation, definition *ast.Document, variables []byte) (coerced []byte, err error)

type NormalizationResult added in v2.12.0

type NormalizationResult struct {
	InlineArguments []InlineArgument
}

NormalizationResult carries per-run outputs of normalization beyond report errors.

type OperationNormalizer

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

OperationNormalizer walks a given AST and applies all registered rules

func NewNormalizer

func NewNormalizer(removeFragmentDefinitions, extractVariables bool) *OperationNormalizer

NewNormalizer creates a new OperationNormalizer and sets up all default rules

func NewWithOpts

func NewWithOpts(opts ...Option) *OperationNormalizer

NewWithOpts creates a new OperationNormalizer with Options

func (*OperationNormalizer) NormalizeNamedOperation

func (o *OperationNormalizer) NormalizeNamedOperation(operation, definition *ast.Document, operationName []byte, report *operationreport.Report)

func (*OperationNormalizer) NormalizeNamedOperationWithResult added in v2.12.0

func (o *OperationNormalizer) NormalizeNamedOperationWithResult(
	operation, definition *ast.Document,
	operationName []byte,
	report *operationreport.Report,
	runOpts RunOptions,
) *NormalizationResult

func (*OperationNormalizer) NormalizeOperation

func (o *OperationNormalizer) NormalizeOperation(operation, definition *ast.Document, report *operationreport.Report)

NormalizeOperation applies all registered rules to the AST

type Option

type Option func(options *options)

func WithEnableDefer added in v2.8.0

func WithEnableDefer() Option

func WithExtractVariables

func WithExtractVariables() Option

func WithIgnoreSkipInclude

func WithIgnoreSkipInclude() Option

func WithInlineArgumentsValidation added in v2.12.0

func WithInlineArgumentsValidation(opts InlineArgumentsValidationOptions) Option

WithInlineArgumentsValidation enables detection of arguments whose values are supplied inline (as literals) instead of as variables. Findings are returned from NormalizeNamedOperationWithResult as a NormalizationResult. When opts.Enforce is set, normalization aborts on the first inline argument and surfaces the error via the report instead of collecting findings.

func WithInlineFragmentSpreads

func WithInlineFragmentSpreads() Option

func WithNormalizeDefinition

func WithNormalizeDefinition() Option

func WithPrevalidationRules added in v2.8.0

func WithPrevalidationRules(rules ...func(walker *astvisitor.Walker)) Option

func WithRemoveFragmentDefinitions

func WithRemoveFragmentDefinitions() Option

func WithRemoveNotMatchingOperationDefinitions

func WithRemoveNotMatchingOperationDefinitions() Option

func WithRemoveUnusedVariables

func WithRemoveUnusedVariables() Option

type RunOptions added in v2.12.0

type RunOptions struct {
	SkipInlineArguments bool
}

RunOptions are per-call inputs to a normalization run.

type SubgraphSDLNormalizer

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

func NewSubgraphSDLNormalizer

func NewSubgraphSDLNormalizer() *SubgraphSDLNormalizer

func (*SubgraphSDLNormalizer) NormalizeSubgraphSDL

func (s *SubgraphSDLNormalizer) NormalizeSubgraphSDL(definition *ast.Document, report *operationreport.Report)

type VariablesMapper

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

func NewVariablesMapper

func NewVariablesMapper() *VariablesMapper

func (*VariablesMapper) NormalizeOperation

func (v *VariablesMapper) NormalizeOperation(operation, definition *ast.Document, report *operationreport.Report) map[string]string

type VariablesNormalizer

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

func NewVariablesNormalizer

func NewVariablesNormalizer() *VariablesNormalizer

func (*VariablesNormalizer) NormalizeOperation

func (v *VariablesNormalizer) NormalizeOperation(operation, definition *ast.Document, report *operationreport.Report) []uploads.UploadPathMapping

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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