astvalidation

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: 14 Imported by: 17

Documentation

Overview

Package astvalidation implements the validation rules specified in the GraphQL specification.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefinitionValidator

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

func DefaultDefinitionValidator

func DefaultDefinitionValidator() *DefinitionValidator

func NewDefinitionValidator

func NewDefinitionValidator(rules ...Rule) *DefinitionValidator

func (*DefinitionValidator) RegisterRule

func (d *DefinitionValidator) RegisterRule(rule Rule)

func (*DefinitionValidator) Validate

func (d *DefinitionValidator) Validate(definition *ast.Document, report *operationreport.Report) ValidationState

type OperationValidator

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

OperationValidator orchestrates the validation process of Operations

func DefaultOperationValidator

func DefaultOperationValidator(options ...Option) *OperationValidator

DefaultOperationValidator returns a fully initialized OperationValidator with all default rules registered

func NewOperationValidator

func NewOperationValidator(rules []Rule) *OperationValidator

func (*OperationValidator) RegisterRule

func (o *OperationValidator) RegisterRule(rule Rule)

RegisterRule registers a rule to the OperationValidator

func (*OperationValidator) Validate

func (o *OperationValidator) Validate(operation, definition *ast.Document, report *operationreport.Report) ValidationState

Validate validates the operation against the definition using the registered ruleset.

type OperationValidatorOptions

type OperationValidatorOptions struct {
	ApolloCompatibilityFlags                   apollocompatibility.Flags
	RelaxFieldSelectionMergingNullabilityCheck bool
	AllowStringLiteralsForEnums                bool
}

type Option

type Option func(options *OperationValidatorOptions)

func WithAllowStringLiteralsForEnums added in v2.14.0

func WithAllowStringLiteralsForEnums() Option

WithAllowStringLiteralsForEnums enables a deliberate spec deviation that allows a string literal to be used where an enum value is expected, as long as the string content matches one of the enum's values (e.g. `f(arg: "VALUE1")` for `enum SomeEnum { VALUE1 }`). Without this option, the validator enforces the strict GraphQL spec behavior where only enum literals are valid inline values for enum types.

func WithApolloCompatibilityFlags

func WithApolloCompatibilityFlags(flags apollocompatibility.Flags) Option

func WithRelaxFieldSelectionMergingNullability

func WithRelaxFieldSelectionMergingNullability() Option

WithRelaxFieldSelectionMergingNullability enables a deliberate spec deviation that allows differing nullability (e.g. String! vs String) on fields in non-overlapping concrete object types within inline fragments. Without this option, the validator enforces the strict GraphQL spec behavior where nullability must always match.

type Rule

type Rule func(walker *astvisitor.Walker)

Rule is hook to register callback functions on the Walker

func AllVariableUsesDefined

func AllVariableUsesDefined() Rule

AllVariableUsesDefined validates if used variables are defined within the operation

func AllVariablesUsed

func AllVariablesUsed() Rule

AllVariablesUsed validates if all defined variables are used

func ArgumentUniqueness

func ArgumentUniqueness() Rule

ArgumentUniqueness validates if arguments are unique

func DeferStreamHaveUniqueLabels added in v2.8.0

func DeferStreamHaveUniqueLabels() Rule

DeferStreamHaveUniqueLabels validates that defer and stream directive labels are: 1. Unique across all defer and stream directives within an operation 2. Not using variables (must be static string values)

func DeferStreamOnValidOperations added in v2.8.0

func DeferStreamOnValidOperations() Rule

DeferStreamOnValidOperations validates that defer/stream directives are used on valid operations: - Query operations: @defer and @stream are allowed everywhere (root and nested fields) - Mutation operations: @defer and @stream are NOT allowed on root fields, but allowed on nested fields - Subscription operations: @defer and @stream are NOT allowed anywhere (root or nested fields) Directives with if: false are allowed (disabled directives). Directives with if: $variable are allowed (dynamic directives that can't be statically determined).

func DirectivesAreDefined

func DirectivesAreDefined() Rule

DirectivesAreDefined validates if used directives are defined

func DirectivesAreInValidLocations

func DirectivesAreInValidLocations() Rule

DirectivesAreInValidLocations validates if directives are used in the right place

func DirectivesAreUniquePerLocation

func DirectivesAreUniquePerLocation() Rule

DirectivesAreUniquePerLocation validates if directives are unique per location

func DocumentContainsExecutableOperation

func DocumentContainsExecutableOperation() Rule

DocumentContainsExecutableOperation validates if the document actually contains an executable Operation

func FieldSelectionMerging

func FieldSelectionMerging(relaxNullabilityCheck ...bool) Rule

FieldSelectionMerging returns a validation rule that ensures field selections can be merged.

This rule implements the validation described in the GraphQL specification section 5.3.2: "Field Selection Merging". It ensures that when multiple fields with the same response key (name or alias) are selected in overlapping selection sets, they can be unambiguously merged into a single field in the response.

The rule is applied to each operation and fragment definition in the document.

func FieldSelections

func FieldSelections() Rule

FieldSelections validates if all FieldSelections are possible and valid

func Fragments

func Fragments() Rule

Fragments validates if the use of fragments in a given document is correct

func ImplementTransitiveInterfaces

func ImplementTransitiveInterfaces() Rule

func ImplementingTypesAreSupersets

func ImplementingTypesAreSupersets() Rule

func KnownArguments

func KnownArguments() Rule

KnownArguments validates if all arguments are known

func KnownTypeNames

func KnownTypeNames() Rule

func LoneAnonymousOperation

func LoneAnonymousOperation() Rule

LoneAnonymousOperation validates if anonymous operations are alone in a given document.

func OperationNameUniqueness

func OperationNameUniqueness() Rule

OperationNameUniqueness validates if all operation names are unique

func PopulatedTypeBodies

func PopulatedTypeBodies() Rule

func RequireDefinedTypesForExtensions

func RequireDefinedTypesForExtensions() Rule

func RequiredArguments

func RequiredArguments() Rule

RequiredArguments validates if all required arguments are present

func StreamAppliedToListFieldsOnly added in v2.8.0

func StreamAppliedToListFieldsOnly() Rule

StreamAppliedToListFieldsOnly validates that the stream directive is used on list fields

func SubscriptionSingleRootField

func SubscriptionSingleRootField() Rule

SubscriptionSingleRootField validates if subscriptions have a single root field

func UniqueEnumValueNames

func UniqueEnumValueNames() Rule

func UniqueFieldDefinitionNames

func UniqueFieldDefinitionNames() Rule

func UniqueOperationTypes

func UniqueOperationTypes() Rule

func UniqueTypeNames

func UniqueTypeNames() Rule

func UniqueUnionMemberTypes

func UniqueUnionMemberTypes() Rule

func ValidateEmptySelectionSets

func ValidateEmptySelectionSets() Rule

ValidateEmptySelectionSets validates if selection sets are not empty should be used only when the operation is created on the fly

func Values

func Values(allowStringLiteralsForEnums ...bool) Rule

Values validates if values are used properly. The optional allowStringLiteralsForEnums argument enables a deliberate spec deviation that accepts a string literal where an enum value is expected, as long as the string content matches one of the enum's values.

func VariableUniqueness

func VariableUniqueness() Rule

VariableUniqueness validates if variables are unique in a given document

func VariablesAreInputTypes

func VariablesAreInputTypes() Rule

VariablesAreInputTypes validates if variables are correct input types

type ValidationState

type ValidationState int

ValidationState is the outcome of a validation

const (
	UnknownState ValidationState = iota
	Valid
	Invalid
)

func (ValidationState) String

func (i ValidationState) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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