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

23 lines
858 B
Rust
Raw Normal View History

2020-03-22 08:45:59 +00:00
use crate::visitor::{Visitor, VisitorContext};
2020-03-10 06:14:09 +00:00
use graphql_parser::query::Field;
#[derive(Default)]
pub struct ScalarLeafs;
impl<'a> Visitor<'a> for ScalarLeafs {
2020-03-22 08:45:59 +00:00
fn enter_field(&mut self, ctx: &mut VisitorContext<'a>, field: &'a Field) {
2020-03-10 06:14:09 +00:00
if let Some(ty) = ctx.parent_type() {
if let Some(schema_field) = ty.field_by_name(&field.name) {
2020-03-14 03:46:20 +00:00
if let Some(ty) = ctx.registry.basic_type_by_typename(&schema_field.ty) {
2020-03-10 06:14:09 +00:00
if ty.is_leaf() && !field.selection_set.items.is_empty() {
ctx.report_error(vec![field.position], format!(
"Field \"{}\" must not have a selection since type \"{}\" has no subfields",
field.name, ty.name()
))
}
}
}
}
}
}