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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use super::ValidationRule;
use crate::ast::{visit_document, OperationVisitor, OperationVisitorContext};
use crate::static_graphql::query::*;
use crate::validation::utils::{ValidationError, ValidationErrorContext};
pub struct KnownFragmentNames;
impl KnownFragmentNames {
pub fn new() -> Self {
KnownFragmentNames
}
}
impl<'a> OperationVisitor<'a, ValidationErrorContext> for KnownFragmentNames {
fn enter_fragment_spread(
&mut self,
visitor_context: &mut OperationVisitorContext,
user_context: &mut ValidationErrorContext,
fragment_spread: &FragmentSpread,
) {
match visitor_context
.known_fragments
.get(&fragment_spread.fragment_name)
{
None => user_context.report_error(ValidationError {
locations: vec![fragment_spread.position],
message: format!("Unknown fragment \"{}\".", fragment_spread.fragment_name),
}),
_ => {}
}
}
}
impl ValidationRule for KnownFragmentNames {
fn validate<'a>(
&self,
ctx: &'a mut OperationVisitorContext,
error_collector: &mut ValidationErrorContext,
) {
visit_document(
&mut KnownFragmentNames::new(),
&ctx.operation,
ctx,
error_collector,
);
}
}
#[test]
fn valid_fragment() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(KnownFragmentNames {}));
let errors = test_operation_with_schema(
"{
human(id: 4) {
...HumanFields1
... on Human {
...HumanFields2
}
... {
name
}
}
}
fragment HumanFields1 on Human {
name
...HumanFields3
}
fragment HumanFields2 on Human {
name
}
fragment HumanFields3 on Human {
name
}",
TEST_SCHEMA,
&mut plan,
);
assert_eq!(get_messages(&errors).len(), 0);
}
#[test]
fn invalid_fragment() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(KnownFragmentNames {}));
let errors = test_operation_with_schema(
"{
human(id: 4) {
...UnknownFragment1
... on Human {
...UnknownFragment2
}
}
}
fragment HumanFields on Human {
name
...UnknownFragment3
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 3);
assert_eq!(
messages,
vec![
"Unknown fragment \"UnknownFragment1\".",
"Unknown fragment \"UnknownFragment2\".",
"Unknown fragment \"UnknownFragment3\".",
]
);
}