
sangria.validation.rules.ScalarLeafs.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sangria_2.11 Show documentation
Show all versions of sangria_2.11 Show documentation
Scala GraphQL implementation
package sangria.validation.rules
import scala.language.postfixOps
import sangria.ast
import sangria.ast.AstVisitorCommand._
import sangria.renderer.SchemaRenderer
import sangria.schema.LeafType
import sangria.validation._
/**
* Scalar leafs
*
* A GraphQL document is valid only if all leaf fields (fields without
* sub selections) are of scalar or enum types.
*/
class ScalarLeafs extends ValidationRule {
override def visitor(ctx: ValidationContext) = new AstValidatingVisitor {
override val onEnter: ValidationVisit = {
case ast.Field(_, name, _, _, sels, pos) ⇒
ctx.typeInfo.tpe match {
case Some(fieldType) ⇒
val named = ctx.typeInfo.getNamedType(fieldType)
named match {
case tpe if tpe.isInstanceOf[LeafType] && sels.nonEmpty ⇒
Left(Vector(NoSubselectionAllowedViolation(name, SchemaRenderer.renderTypeName(tpe, true), ctx.sourceMapper, pos.toList)))
case tpe if !tpe.isInstanceOf[LeafType] && sels.isEmpty ⇒
Left(Vector(RequiredSubselectionViolation(name, SchemaRenderer.renderTypeName(tpe, true), ctx.sourceMapper, pos.toList)))
case _ ⇒ Right(Continue)
}
case None ⇒ Right(Continue)
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy