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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use std::collections::HashMap;
use crate::parser::types::{ExecutableDocument, FragmentSpread, InlineFragment, TypeCondition};
use crate::validation::visitor::{Visitor, VisitorContext};
use crate::Positioned;
#[derive(Default)]
pub struct PossibleFragmentSpreads<'a> {
fragment_types: HashMap<&'a str, &'a str>,
}
impl<'a> Visitor<'a> for PossibleFragmentSpreads<'a> {
fn enter_document(&mut self, _ctx: &mut VisitorContext<'a>, doc: &'a ExecutableDocument) {
for (name, fragment) in doc.fragments.iter() {
self.fragment_types
.insert(name.as_str(), &fragment.node.type_condition.node.on.node);
}
}
fn enter_fragment_spread(
&mut self,
ctx: &mut VisitorContext<'a>,
fragment_spread: &'a Positioned<FragmentSpread>,
) {
if let Some(fragment_type) = self
.fragment_types
.get(&*fragment_spread.node.fragment_name.node)
{
if let Some(current_type) = ctx.current_type() {
if let Some(on_type) = ctx.registry.types.get(*fragment_type) {
if !current_type.type_overlap(on_type) {
ctx.report_error(
vec![fragment_spread.pos],
format!(
"Fragment \"{}\" cannot be spread here as objects of type \"{}\" can never be of type \"{}\"",
fragment_spread.node.fragment_name.node, current_type.name(), fragment_type
),
);
}
}
}
}
}
fn enter_inline_fragment(
&mut self,
ctx: &mut VisitorContext<'a>,
inline_fragment: &'a Positioned<InlineFragment>,
) {
if let Some(parent_type) = ctx.parent_type() {
if let Some(TypeCondition { on: fragment_type }) = &inline_fragment
.node
.type_condition
.as_ref()
.map(|c| &c.node)
{
if let Some(on_type) = ctx.registry.types.get(fragment_type.node.as_str()) {
if !parent_type.type_overlap(&on_type) {
ctx.report_error(
vec![inline_fragment.pos],
format!(
"Fragment cannot be spread here as objects of type \"{}\" \
can never be of type \"{}\"",
parent_type.name(),
fragment_type
),
)
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
pub fn factory<'a>() -> PossibleFragmentSpreads<'a> {
PossibleFragmentSpreads::default()
}
#[test]
fn of_the_same_object() {
expect_passes_rule!(
factory,
r#"
fragment objectWithinObject on Dog { ...dogFragment }
fragment dogFragment on Dog { barkVolume }
{ __typename }
"#,
);
}
#[test]
fn of_the_same_object_with_inline_fragment() {
expect_passes_rule!(
factory,
r#"
fragment objectWithinObjectAnon on Dog { ... on Dog { barkVolume } }
{ __typename }
"#,
);
}
#[test]
fn object_into_an_implemented_interface() {
expect_passes_rule!(
factory,
r#"
fragment objectWithinInterface on Pet { ...dogFragment }
fragment dogFragment on Dog { barkVolume }
{ __typename }
"#,
);
}
#[test]
fn object_into_containing_union() {
expect_passes_rule!(
factory,
r#"
fragment objectWithinUnion on CatOrDog { ...dogFragment }
fragment dogFragment on Dog { barkVolume }
{ __typename }
"#,
);
}
#[test]
fn union_into_contained_object() {
expect_passes_rule!(
factory,
r#"
fragment unionWithinObject on Dog { ...catOrDogFragment }
fragment catOrDogFragment on CatOrDog { __typename }
{ __typename }
"#,
);
}
#[test]
fn union_into_overlapping_interface() {
expect_passes_rule!(
factory,
r#"
fragment unionWithinInterface on Pet { ...catOrDogFragment }
fragment catOrDogFragment on CatOrDog { __typename }
{ __typename }
"#,
);
}
#[test]
fn union_into_overlapping_union() {
expect_passes_rule!(
factory,
r#"
fragment unionWithinUnion on DogOrHuman { ...catOrDogFragment }
fragment catOrDogFragment on CatOrDog { __typename }
{ __typename }
"#,
);
}
#[test]
fn interface_into_implemented_object() {
expect_passes_rule!(
factory,
r#"
fragment interfaceWithinObject on Dog { ...petFragment }
fragment petFragment on Pet { name }
{ __typename }
"#,
);
}
#[test]
fn interface_into_overlapping_interface() {
expect_passes_rule!(
factory,
r#"
fragment interfaceWithinInterface on Pet { ...beingFragment }
fragment beingFragment on Being { name }
{ __typename }
"#,
);
}
#[test]
fn interface_into_overlapping_interface_in_inline_fragment() {
expect_passes_rule!(
factory,
r#"
fragment interfaceWithinInterface on Pet { ... on Being { name } }
{ __typename }
"#,
);
}
#[test]
fn interface_into_overlapping_union() {
expect_passes_rule!(
factory,
r#"
fragment interfaceWithinUnion on CatOrDog { ...petFragment }
fragment petFragment on Pet { name }
{ __typename }
"#,
);
}
#[test]
fn different_object_into_object() {
expect_fails_rule!(
factory,
r#"
fragment invalidObjectWithinObject on Cat { ...dogFragment }
fragment dogFragment on Dog { barkVolume }
{ __typename }
"#,
);
}
#[test]
fn different_object_into_object_in_inline_fragment() {
expect_fails_rule!(
factory,
r#"
fragment invalidObjectWithinObjectAnon on Cat {
... on Dog { barkVolume }
}
{ __typename }
"#,
);
}
#[test]
fn object_into_not_implementing_interface() {
expect_fails_rule!(
factory,
r#"
fragment invalidObjectWithinInterface on Pet { ...humanFragment }
fragment humanFragment on Human { pets { name } }
{ __typename }
"#,
);
}
#[test]
fn object_into_not_containing_union() {
expect_fails_rule!(
factory,
r#"
fragment invalidObjectWithinUnion on CatOrDog { ...humanFragment }
fragment humanFragment on Human { pets { name } }
{ __typename }
"#,
);
}
#[test]
fn union_into_not_contained_object() {
expect_fails_rule!(
factory,
r#"
fragment invalidUnionWithinObject on Human { ...catOrDogFragment }
fragment catOrDogFragment on CatOrDog { __typename }
{ __typename }
"#,
);
}
#[test]
fn union_into_non_overlapping_interface() {
expect_fails_rule!(
factory,
r#"
fragment invalidUnionWithinInterface on Pet { ...humanOrAlienFragment }
fragment humanOrAlienFragment on HumanOrAlien { __typename }
{ __typename }
"#,
);
}
#[test]
fn union_into_non_overlapping_union() {
expect_fails_rule!(
factory,
r#"
fragment invalidUnionWithinUnion on CatOrDog { ...humanOrAlienFragment }
fragment humanOrAlienFragment on HumanOrAlien { __typename }
{ __typename }
"#,
);
}
#[test]
fn interface_into_non_implementing_object() {
expect_fails_rule!(
factory,
r#"
fragment invalidInterfaceWithinObject on Cat { ...intelligentFragment }
fragment intelligentFragment on Intelligent { iq }
{ __typename }
"#,
);
}
#[test]
fn interface_into_non_overlapping_interface() {
expect_fails_rule!(
factory,
r#"
fragment invalidInterfaceWithinInterface on Pet {
...intelligentFragment
}
fragment intelligentFragment on Intelligent { iq }
{ __typename }
"#,
);
}
#[test]
fn interface_into_non_overlapping_interface_in_inline_fragment() {
expect_fails_rule!(
factory,
r#"
fragment invalidInterfaceWithinInterfaceAnon on Pet {
...on Intelligent { iq }
}
{ __typename }
"#,
);
}
#[test]
fn interface_into_non_overlapping_union() {
expect_fails_rule!(
factory,
r#"
fragment invalidInterfaceWithinUnion on HumanOrAlien { ...petFragment }
fragment petFragment on Pet { name }
{ __typename }
"#,
);
}
}