2020-03-25 03:39:28 +00:00
|
|
|
mod rules;
|
2020-04-03 05:57:24 +00:00
|
|
|
mod suggestion;
|
2020-03-25 03:39:28 +00:00
|
|
|
mod utils;
|
|
|
|
mod visitor;
|
2020-03-25 07:07:16 +00:00
|
|
|
mod visitors;
|
2020-03-25 03:39:28 +00:00
|
|
|
|
2020-04-05 08:00:26 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_harness;
|
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
use crate::registry::Registry;
|
2020-04-02 02:21:04 +00:00
|
|
|
use crate::{CacheControl, Error, Result};
|
2020-03-08 12:35:36 +00:00
|
|
|
use graphql_parser::query::Document;
|
2020-03-25 03:39:28 +00:00
|
|
|
use visitor::{visit, VisitorContext, VisitorNil};
|
2020-03-08 12:35:36 +00:00
|
|
|
|
2020-03-25 07:07:16 +00:00
|
|
|
pub struct CheckResult {
|
|
|
|
pub cache_control: CacheControl,
|
|
|
|
pub complexity: usize,
|
|
|
|
pub depth: usize,
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:57:21 +00:00
|
|
|
/// Validation mode
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum ValidationMode {
|
|
|
|
/// Execute all validation rules.
|
|
|
|
Strict,
|
|
|
|
|
|
|
|
/// The executor itself also has error handling, so it can improve performance, but it can lose some error messages.
|
|
|
|
Fast,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_rules(
|
|
|
|
registry: &Registry,
|
|
|
|
doc: &Document,
|
|
|
|
mode: ValidationMode,
|
|
|
|
) -> Result<CheckResult> {
|
2020-03-22 08:45:59 +00:00
|
|
|
let mut ctx = VisitorContext::new(registry, doc);
|
2020-03-24 10:54:22 +00:00
|
|
|
let mut cache_control = CacheControl::default();
|
2020-03-25 07:07:16 +00:00
|
|
|
let mut complexity = 0;
|
|
|
|
let mut depth = 0;
|
|
|
|
|
2020-04-06 11:57:21 +00:00
|
|
|
match mode {
|
|
|
|
ValidationMode::Strict => {
|
|
|
|
let mut visitor = VisitorNil
|
|
|
|
.with(rules::ArgumentsOfCorrectType::default())
|
|
|
|
.with(rules::DefaultValuesOfCorrectType)
|
|
|
|
.with(rules::FieldsOnCorrectType)
|
|
|
|
.with(rules::FragmentsOnCompositeTypes)
|
|
|
|
.with(rules::KnownArgumentNames::default())
|
|
|
|
.with(rules::NoFragmentCycles::default())
|
|
|
|
.with(rules::KnownFragmentNames)
|
|
|
|
.with(rules::KnownTypeNames)
|
|
|
|
.with(rules::LoneAnonymousOperation::default())
|
|
|
|
.with(rules::NoUndefinedVariables::default())
|
|
|
|
.with(rules::NoUnusedFragments::default())
|
|
|
|
.with(rules::NoUnusedVariables::default())
|
|
|
|
.with(rules::UniqueArgumentNames::default())
|
|
|
|
.with(rules::UniqueFragmentNames::default())
|
|
|
|
.with(rules::UniqueOperationNames::default())
|
|
|
|
.with(rules::UniqueVariableNames::default())
|
|
|
|
.with(rules::VariablesAreInputTypes)
|
|
|
|
.with(rules::VariableInAllowedPosition::default())
|
|
|
|
.with(rules::ScalarLeafs)
|
|
|
|
.with(rules::PossibleFragmentSpreads::default())
|
|
|
|
.with(rules::ProvidedNonNullArguments)
|
|
|
|
.with(rules::KnownDirectives::default())
|
|
|
|
.with(rules::OverlappingFieldsCanBeMerged)
|
|
|
|
.with(rules::UploadFile)
|
|
|
|
.with(visitors::CacheControlCalculate {
|
|
|
|
cache_control: &mut cache_control,
|
|
|
|
})
|
|
|
|
.with(visitors::ComplexityCalculate {
|
|
|
|
complexity: &mut complexity,
|
|
|
|
})
|
|
|
|
.with(visitors::DepthCalculate::new(&mut depth));
|
|
|
|
visit(&mut visitor, &mut ctx, doc);
|
|
|
|
}
|
|
|
|
ValidationMode::Fast => {
|
|
|
|
let mut visitor = VisitorNil
|
|
|
|
.with(rules::NoFragmentCycles::default())
|
|
|
|
.with(rules::UploadFile)
|
|
|
|
.with(visitors::CacheControlCalculate {
|
|
|
|
cache_control: &mut cache_control,
|
|
|
|
})
|
|
|
|
.with(visitors::ComplexityCalculate {
|
|
|
|
complexity: &mut complexity,
|
|
|
|
})
|
|
|
|
.with(visitors::DepthCalculate::new(&mut depth));
|
|
|
|
visit(&mut visitor, &mut ctx, doc);
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
|
|
|
|
if !ctx.errors.is_empty() {
|
2020-04-02 02:21:04 +00:00
|
|
|
return Err(Error::Rule { errors: ctx.errors });
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
2020-03-25 07:07:16 +00:00
|
|
|
Ok(CheckResult {
|
|
|
|
cache_control,
|
|
|
|
complexity,
|
|
|
|
depth: depth as usize,
|
|
|
|
})
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|