1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#[cfg(test)]
#[macro_use]
mod test_harness;
mod rules;
mod suggestion;
mod utils;
mod visitor;
mod visitors;
pub use visitor::VisitorContext;
use visitor::{visit, VisitorNil};
use crate::{
parser::types::ExecutableDocument, registry::Registry, CacheControl, ServerError, Variables,
};
#[derive(Debug, Copy, Clone)]
pub struct ValidationResult {
pub cache_control: CacheControl,
pub complexity: usize,
pub depth: usize,
}
#[derive(Copy, Clone, Debug)]
pub enum ValidationMode {
Strict,
Fast,
}
pub fn check_rules(
registry: &Registry,
doc: &ExecutableDocument,
variables: Option<&Variables>,
mode: ValidationMode,
) -> Result<ValidationResult, Vec<ServerError>> {
let mut ctx = VisitorContext::new(registry, doc, variables);
let mut cache_control = CacheControl::default();
let mut complexity = 0;
let mut depth = 0;
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::NoUndefinedVariables::default())
.with(rules::NoUnusedFragments::default())
.with(rules::NoUnusedVariables::default())
.with(rules::UniqueArgumentNames::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::DirectivesUnique::default())
.with(rules::OverlappingFieldsCanBeMerged)
.with(rules::UploadFile)
.with(visitors::CacheControlCalculate {
cache_control: &mut cache_control,
})
.with(visitors::ComplexityCalculate::new(&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::new(&mut complexity))
.with(visitors::DepthCalculate::new(&mut depth));
visit(&mut visitor, &mut ctx, doc);
}
}
if !ctx.errors.is_empty() {
return Err(ctx.errors.into_iter().map(Into::into).collect());
}
Ok(ValidationResult {
cache_control,
complexity,
depth,
})
}