astnormalization

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

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

Example

This examples shows how the normalization package helps "simplifying" 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 interface{}, 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 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)

NormalizeNamedOperation applies all registered rules to one specific named operation in the AST

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 WithExtractVariables

func WithExtractVariables() Option

func WithInlineFragmentSpreads

func WithInlineFragmentSpreads() Option

func WithNormalizeDefinition

func WithNormalizeDefinition() Option

func WithRemoveFragmentDefinitions

func WithRemoveFragmentDefinitions() Option

func WithRemoveNotMatchingOperationDefinitions

func WithRemoveNotMatchingOperationDefinitions() Option

func WithRemoveUnusedVariables

func WithRemoveUnusedVariables() Option

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)

Jump to

Keyboard shortcuts

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