Expand description
§Validation Rules for GraphQL ASTs
This module contains logic to run validation rules on GraphQL Query Language documents.
It provides rules that have already been implemented to validate a document as much as it can
without using any schema information, which are rules grouped into this module’s DefaultRules
and utilities to create your own ValidationRules
.
The rules this module already comes with are:
rules::KnownFragmentNames
: validates that all spread fragments are definedrules::LoneAnonymousOperation
: validates that a document only contains a single anonymous operationrules::NoFragmentCycles
: validates that no fragment is spread in on itself to avoid loopingrules::NoUndefinedVariables
: checks that all used variables are defined per operationrules::NoUnusedFragments
: validates that all fragments in a document are used at least oncerules::UniqueArgumentNames
: checks for arguments that are used to not contain duplicatesrules::UniqueFragmentNames
: checks that no fragments share the same namerules::UniqueOperationNames
: checks that no operations share the same namerules::UniqueVariableNames
: checks that no variables share the same name
The visit module is used to actually execute validation rules.
The ValidationRule
trait is simply defined to implement the Visitor trait
and to accept the ValidationContext
, which is used to keep track of validation errors.
As such, the DefaultRules
rule is a ValidationRule
itself that’s
composed using the ComposedVisitor
utility.
All rules must implement the Default
trait, which makes it easier to quickly run a validation
rule and isolates them from external state, since no validation requires any external state.
For example, this is one way to run a validation rule, in this case DefaultRules
:
use graphql_query::{ast::*, validate::*};
let ctx = ASTContext::new();
let document = Document::parse(&ctx, "{ field }").unwrap();
DefaultRules::validate(&ctx, &document).unwrap()
Another way is to utilize the ValidateNode
trait instead to run validation starting from an
AST Node rather from the rule itself:
use graphql_query::{ast::*, validate::*};
let ctx = ASTContext::new();
let document = Document::parse(&ctx, "{ field }").unwrap();
document.validate::<DefaultRules>(&ctx).unwrap()
Re-exports§
pub use rules::DefaultRules;
Modules§
Structs§
- Validation
Context - The
ValidationContext
carrying a reference to the AST Context’s arena and a list of errors.
Traits§
- Validate
Node - Trait to run a
ValidationRule
on a given GraphQL Document node. - Validation
Rule - Trait for a
ValidationRule
that checks a givenGraphQl
document against its rules using a visitor.