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
use crate::executable::{
    document::{ArgumentError, Error, Path, Rule, Visitor},
    Cache,
};
use bluejay_core::definition::{
    DirectiveDefinition, FieldDefinition, InputType, InputValueDefinition, SchemaDefinition,
};
use bluejay_core::executable::{ExecutableDocument, Field};
use bluejay_core::{Argument, AsIter, Directive};
use std::collections::HashMap;

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

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> RequiredArguments<'a, E, S> {
    fn visit_directive<
        const CONST: bool,
        F: Fn(ArgumentError<'a, CONST, E, S>) -> Error<'a, E, S>,
    >(
        &mut self,
        directive: &'a <E as ExecutableDocument>::Directive<CONST>,
        build_error: F,
    ) {
        if let Some(directive_definition) = self
            .schema_definition
            .get_directive_definition(directive.name())
        {
            self.visit_arguments(
                directive.arguments(),
                directive_definition.arguments_definition(),
                |missing_argument_definitions| {
                    build_error(ArgumentError::DirectiveMissingRequiredArguments {
                        directive,
                        directive_definition,
                        missing_argument_definitions,
                    })
                },
            )
        }
    }

    fn visit_arguments<
        const CONST: bool,
        F: Fn(Vec<&'a S::InputValueDefinition>) -> Error<'a, E, S>,
    >(
        &mut self,
        arguments: Option<&'a E::Arguments<CONST>>,
        arguments_definition: Option<&'a S::ArgumentsDefinition>,
        build_error: F,
    ) {
        if let Some(arguments_definition) = arguments_definition {
            let indexed_arguments = arguments
                .map(|arguments| {
                    arguments.iter().fold(
                        HashMap::new(),
                        |mut indexed_arguments: HashMap<&'a str, &'a E::Argument<CONST>>,
                         argument| {
                            indexed_arguments.insert(argument.name(), argument);
                            indexed_arguments
                        },
                    )
                })
                .unwrap_or_default();
            let missing_argument_definitions = arguments_definition
                .iter()
                .filter(|ivd| {
                    ivd.r#type().is_required()
                        && ivd.default_value().is_none()
                        && !indexed_arguments.contains_key(ivd.name())
                })
                .collect::<Vec<_>>();
            if !missing_argument_definitions.is_empty() {
                self.errors.push(build_error(missing_argument_definitions));
            }
        }
    }
}

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

    fn visit_field(
        &mut self,
        field: &'a <E as ExecutableDocument>::Field,
        field_definition: &'a S::FieldDefinition,
        _: &Path<'a, E>,
    ) {
        self.visit_arguments(
            field.arguments(),
            field_definition.arguments_definition(),
            |missing_argument_definitions| {
                Error::InvalidVariableArgument(ArgumentError::FieldMissingRequiredArguments {
                    field,
                    field_definition,
                    missing_argument_definitions,
                })
            },
        )
    }

    fn visit_variable_directive(
        &mut self,
        directive: &'a <E as ExecutableDocument>::Directive<false>,
        _: bluejay_core::definition::DirectiveLocation,
    ) {
        self.visit_directive(directive, Error::InvalidVariableArgument)
    }

    fn visit_const_directive(
        &mut self,
        directive: &'a <E as ExecutableDocument>::Directive<true>,
        _: bluejay_core::definition::DirectiveLocation,
    ) {
        self.visit_directive(directive, Error::InvalidConstArgument)
    }
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> Rule<'a, E, S>
    for RequiredArguments<'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()
    }
}