All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sangria.validation.rules.LoneAnonymousOperation.scala Maven / Gradle / Ivy

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