juniper/validation/rules/
known_fragment_names.rs

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
use crate::{
    ast::FragmentSpread,
    parser::Spanning,
    validation::{ValidatorContext, Visitor},
    value::ScalarValue,
};

pub struct KnownFragmentNames;

pub fn factory() -> KnownFragmentNames {
    KnownFragmentNames
}

impl<'a, S> Visitor<'a, S> for KnownFragmentNames
where
    S: ScalarValue,
{
    fn enter_fragment_spread(
        &mut self,
        context: &mut ValidatorContext<'a, S>,
        spread: &'a Spanning<FragmentSpread<S>>,
    ) {
        let spread_name = &spread.item.name;
        if !context.is_known_fragment(spread_name.item) {
            context.report_error(&error_message(spread_name.item), &[spread_name.span.start]);
        }
    }
}

fn error_message(frag_name: &str) -> String {
    format!(r#"Unknown fragment: "{frag_name}""#)
}

#[cfg(test)]
mod tests {
    use super::{error_message, factory};

    use crate::{
        parser::SourcePosition,
        validation::{expect_fails_rule, expect_passes_rule, RuleError},
        value::DefaultScalarValue,
    };

    #[test]
    fn known() {
        expect_passes_rule::<_, _, DefaultScalarValue>(
            factory,
            r#"
          {
            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]
    fn unknown() {
        expect_fails_rule::<_, _, DefaultScalarValue>(
            factory,
            r#"
          {
            human(id: 4) {
              ...UnknownFragment1
              ... on Human {
                ...UnknownFragment2
              }
            }
          }
          fragment HumanFields on Human {
            name
            ...UnknownFragment3
          }
        "#,
            &[
                RuleError::new(
                    &error_message("UnknownFragment1"),
                    &[SourcePosition::new(57, 3, 17)],
                ),
                RuleError::new(
                    &error_message("UnknownFragment2"),
                    &[SourcePosition::new(122, 5, 19)],
                ),
                RuleError::new(
                    &error_message("UnknownFragment3"),
                    &[SourcePosition::new(255, 11, 15)],
                ),
            ],
        );
    }
}