Module validate

Source
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:

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§

rules

Structs§

ValidationContext
The ValidationContext carrying a reference to the AST Context’s arena and a list of errors.

Traits§

ValidateNode
Trait to run a ValidationRule on a given GraphQL Document node.
ValidationRule
Trait for a ValidationRule that checks a given GraphQl document against its rules using a visitor.