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
use crate::executable::{
    document::{Error, Rule, Visitor},
    Cache,
};
use bluejay_core::definition::{FieldsDefinition, SchemaDefinition, TypeDefinitionReference};
use bluejay_core::executable::{ExecutableDocument, Field, Selection, SelectionReference};
use bluejay_core::AsIter;
use std::ops::Not;

pub struct FieldSelections<'a, E: ExecutableDocument, S: SchemaDefinition> {
    errors: Vec<Error<'a, E, S>>,
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> Visitor<'a, E, S>
    for FieldSelections<'a, E, S>
{
    fn new(_: &'a E, _: &'a S, _: &'a Cache<'a, E, S>) -> Self {
        Self { errors: Vec::new() }
    }

    fn visit_selection_set(
        &mut self,
        selection_set: &'a E::SelectionSet,
        r#type: TypeDefinitionReference<'a, S::TypeDefinition>,
    ) {
        if let Some(fields_definition) = r#type.fields_definition() {
            self.errors
                .extend(selection_set.iter().filter_map(|selection| {
                    if let SelectionReference::Field(field) = selection.as_ref() {
                        let name = field.name();
                        fields_definition
                            .contains_field(name)
                            .not()
                            .then_some(Error::FieldDoesNotExistOnType { field, r#type })
                    } else {
                        None
                    }
                }));
        }
    }
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> Rule<'a, E, S>
    for FieldSelections<'a, E, S>
{
    type Error = Error<'a, E, S>;
    type Errors = std::vec::IntoIter<Error<'a, E, S>>;

    fn into_errors(self) -> Self::Errors {
        self.errors.into_iter()
    }
}