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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use super::ValidationRule;
use crate::{
ast::{ext::*, get_named_type, TypeInfo, TypeInfoElementRef, TypeInfoQueryVisitor},
validation::utils::{ValidationContext, ValidationError, ValidationErrorContext},
};
pub struct LeafFieldSelections;
impl TypeInfoQueryVisitor<ValidationErrorContext<'_>> for LeafFieldSelections {
fn enter_field(
&self,
_node: &crate::static_graphql::query::Field,
_visitor_context: &mut ValidationErrorContext<'_>,
_type_info: &TypeInfo,
) {
if let Some(TypeInfoElementRef::Ref(field_type)) = _type_info.get_type() {
let named_type = get_named_type(&field_type);
let schema_type = _visitor_context
.ctx
.type_info_registry
.as_ref()
.unwrap()
.type_by_name
.get(&named_type)
.unwrap();
let selection_set = &_node.selection_set;
if schema_type.is_leaf_type() {
if selection_set.items.len() > 0 {
_visitor_context.report_error(ValidationError {
locations: vec![_node.position],
message: format!(
"Field \"{}\" must not have a selection since type \"{}\" has no subfields.",
_node.name,
schema_type.name()
),
});
}
} else {
if selection_set.items.len() == 0 {
_visitor_context.report_error(ValidationError {
locations: vec![_node.position],
message: format!(
"Field \"{}\" of type \"{}\" must have a selection of subfields. Did you mean \"{} {{ ... }}\"?",
_node.name,
field_type,
_node.name
),
});
}
}
}
}
}
impl ValidationRule for LeafFieldSelections {
fn validate<'a>(&self, ctx: &ValidationContext) -> Vec<ValidationError> {
let mut error_context = ValidationErrorContext::new(ctx);
if let Some(type_info_registry) = &ctx.type_info_registry {
self.visit_document(
&ctx.operation.clone(),
&mut error_context,
&type_info_registry,
);
}
error_context.errors
}
}
#[test]
fn valid_scalar_selection() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"fragment scalarSelection on Dog {
barks
}",
TEST_SCHEMA,
&mut plan,
);
assert_eq!(get_messages(&errors).len(), 0);
}
#[test]
fn object_type_missing_selection() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"query directQueryOnObjectWithoutSubFields {
human
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"human\" of type \"Human\" must have a selection of subfields. Did you mean \"human { ... }\"?"]
);
}
#[test]
fn interface_type_missing_selection() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"{
human { pets }
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"pets\" of type \"[Pet]\" must have a selection of subfields. Did you mean \"pets { ... }\"?"]
);
}
#[test]
fn selection_not_allowed_on_scalar() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"fragment scalarSelectionsNotAllowedOnBoolean on Dog {
barks { sinceWhen }
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"barks\" must not have a selection since type \"Boolean\" has no subfields."]
);
}
#[test]
fn selection_not_allowed_on_enum() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"fragment scalarSelectionsNotAllowedOnEnum on Cat {
furColor { inHexDec }
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"furColor\" must not have a selection since type \"FurColor\" has no subfields."]
);
}
#[test]
fn scalar_selection_not_allowed_with_args() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"fragment scalarSelectionsNotAllowedWithArgs on Dog {
doesKnowCommand(dogCommand: SIT) { sinceWhen }
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"doesKnowCommand\" must not have a selection since type \"Boolean\" has no subfields."]
);
}
#[test]
fn scalar_selection_not_allowed_with_directives() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"fragment scalarSelectionsNotAllowedWithDirectives on Dog {
name @include(if: true) { isAlsoHumanName }
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"name\" must not have a selection since type \"String\" has no subfields."]
);
}
#[test]
fn scalar_selection_not_allowed_with_directives_and_args() {
use crate::validation::test_utils::*;
let mut plan = create_plan_from_rule(Box::new(LeafFieldSelections {}));
let errors = test_operation_with_schema(
"fragment scalarSelectionsNotAllowedWithDirectivesAndArgs on Dog {
doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen }
}",
TEST_SCHEMA,
&mut plan,
);
let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Field \"doesKnowCommand\" must not have a selection since type \"Boolean\" has no subfields."]
);
}