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
use super::{
locate_fragments::LocateFragments,
rules::ValidationRule,
utils::{ValidationContext, ValidationError},
};
use crate::{
ast::TypeInfoRegistry,
static_graphql::{query, schema},
};
pub struct ValidationPlan {
pub rules: Vec<Box<dyn ValidationRule>>,
}
impl<'a> ValidationPlan {
pub fn add_rule(&mut self, rule: Box<dyn ValidationRule>) {
self.rules.push(rule);
}
}
pub fn validate<'a>(
schema: &'a schema::Document,
operation: &'a query::Document,
validation_plan: &'a ValidationPlan,
) -> Vec<ValidationError> {
let mut fragments_locator = LocateFragments::new();
let fragments = fragments_locator.locate_fragments(&operation);
let type_info_registry = TypeInfoRegistry::new(schema);
let validation_context = ValidationContext {
operation: operation.clone(),
schema: schema.clone(),
fragments,
type_info_registry: Some(type_info_registry),
};
let validation_errors = validation_plan
.rules
.iter()
.flat_map(|rule| rule.validate(&validation_context))
.collect::<Vec<_>>();
validation_errors
}
#[test]
fn test_validate_valid_query() {
use crate::validation::test_utils::*;
let mut default_plan = create_default_ruleset_plan();
let errors = test_operation_without_schema(
"
query test {
foo
}
",
&mut default_plan,
);
assert_eq!(errors.len(), 0);
}
#[test]
fn test_validate_valid_fragment() {
use crate::validation::test_utils::*;
let mut default_plan = create_default_ruleset_plan();
let errors = test_operation_without_schema(
"
fragment uniqueFields on Dog {
name
nickname
}
",
&mut default_plan,
);
assert_eq!(errors.len(), 0);
}