async-graphql/src/validation/rules/provided_non_null_arguments.rs

54 lines
2.1 KiB
Rust
Raw Normal View History

2020-03-10 12:35:25 +00:00
use crate::registry::TypeName;
2020-03-24 10:54:22 +00:00
use crate::validation::visitor::{Visitor, VisitorContext};
2020-03-10 12:35:25 +00:00
use graphql_parser::query::Field;
use graphql_parser::schema::Directive;
#[derive(Default)]
pub struct ProvidedNonNullArguments;
impl<'a> Visitor<'a> for ProvidedNonNullArguments {
2020-03-22 08:45:59 +00:00
fn enter_directive(&mut self, ctx: &mut VisitorContext<'a>, directive: &'a Directive) {
2020-03-10 12:35:25 +00:00
if let Some(schema_directive) = ctx.registry.directives.get(&directive.name) {
for arg in schema_directive.args.values() {
2020-03-21 01:32:13 +00:00
if TypeName::create(&arg.ty).is_non_null()
&& arg.default_value.is_none()
&& directive
2020-03-10 12:35:25 +00:00
.arguments
.iter()
.find(|(name, _)| name == arg.name)
.is_none()
2020-03-21 01:32:13 +00:00
{
ctx.report_error(vec![directive.position],
2020-03-10 12:35:25 +00:00
format!(
"Directive \"@{}\" argument \"{}\" of type \"{}\" is required but not provided",
directive.name, arg.name, arg.ty
));
}
}
}
}
2020-03-22 08:45:59 +00:00
fn enter_field(&mut self, ctx: &mut VisitorContext<'a>, field: &'a Field) {
2020-03-10 12:35:25 +00:00
if let Some(parent_type) = ctx.parent_type() {
if let Some(schema_field) = parent_type.field_by_name(&field.name) {
for arg in schema_field.args.values() {
2020-03-21 01:32:13 +00:00
if TypeName::create(&arg.ty).is_non_null()
&& arg.default_value.is_none()
&& field
2020-03-10 12:35:25 +00:00
.arguments
.iter()
.find(|(name, _)| name == arg.name)
.is_none()
2020-03-21 01:32:13 +00:00
{
ctx.report_error(vec![field.position],
2020-03-10 12:35:25 +00:00
format!(
r#"Field "{}" argument "{}" of type "{}" is required but not provided"#,
field.name, arg.name, parent_type.name()
));
}
}
}
}
}
}