sangria.validation.rules.LoneAnonymousOperation.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sangria-core_2.13 Show documentation
Show all versions of sangria-core_2.13 Show documentation
Scala GraphQL implementation
The newest version!
package sangria.validation.rules
import sangria.ast
import sangria.ast.AstVisitorCommand
import sangria.validation._
/** Lone anonymous operation
*
* A GraphQL document is only valid if when it contains an anonymous operation (the query
* short-hand) that it contains only that one operation definition.
*/
class LoneAnonymousOperation extends ValidationRule {
override def visitor(ctx: ValidationContext): AstValidatingVisitor = new AstValidatingVisitor {
var operationCount = 0
override val onEnter: ValidationVisit = {
case ast.Document(definitions, _, _, _) =>
operationCount = definitions.count(_.isInstanceOf[ast.OperationDefinition])
AstVisitorCommand.RightContinue
case op: ast.OperationDefinition =>
if (op.name.isEmpty && operationCount > 1)
Left(Vector(AnonOperationNotAloneViolation(ctx.sourceMapper, op.location.toList)))
else
AstVisitorCommand.RightContinue
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy