Skip to content

Commit

Permalink
remove errors limit on construtor
Browse files Browse the repository at this point in the history
  • Loading branch information
yanns committed Aug 14, 2023
1 parent 81914d3 commit 42694dc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import sangria.validation.{QueryValidator, RuleBasedQueryValidator, Violation}
@State(Scope.Thread)
class OverlappingFieldsCanBeMergedBenchmark {

val validator: QueryValidator = RuleBasedQueryValidator(
val validator: QueryValidator = new RuleBasedQueryValidator(
List(new rules.OverlappingFieldsCanBeMerged))

val schema: Schema[_, _] =
Expand Down Expand Up @@ -98,7 +98,7 @@ class OverlappingFieldsCanBeMergedBenchmark {
bh.consume(doValidate(validator, deepAbstractConcrete))

private def doValidate(validator: QueryValidator, document: Document): Vector[Violation] = {
val result = validator.validateQuery(schema, document)
val result = validator.validateQuery(schema, document, None)
require(result.isEmpty)
result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ object QueryReducerExecutor {
queryValidator: QueryValidator = QueryValidator.default,
exceptionHandler: ExceptionHandler = ExceptionHandler.empty,
deprecationTracker: DeprecationTracker = DeprecationTracker.empty,
middleware: List[Middleware[Ctx]] = Nil
middleware: List[Middleware[Ctx]] = Nil,
errorsLimit: Option[Int] = None
)(implicit executionContext: ExecutionContext): Future[(Ctx, TimeMeasurement)] = {
val violations = queryValidator.validateQuery(schema, queryAst)
val violations = queryValidator.validateQuery(schema, queryAst, errorsLimit)

if (violations.nonEmpty)
Future.failed(ValidationError(violations, exceptionHandler))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ object BatchExecutor {
middleware: List[Middleware[Ctx]] = Nil,
maxQueryDepth: Option[Int] = None,
queryReducers: List[QueryReducer[Ctx, _]] = Nil,
inferVariableDefinitions: Boolean = true
inferVariableDefinitions: Boolean = true,
errorsLimit: Option[Int] = None
)(implicit
executionContext: ExecutionContext,
marshaller: SymmetricMarshaller[T],
Expand All @@ -100,7 +101,7 @@ object BatchExecutor {
inferVariableDefinitions,
exceptionHandler))
.flatMap { case res @ (updatedDocument, _) =>
val violations = queryValidator.validateQuery(schema, updatedDocument)
val violations = queryValidator.validateQuery(schema, updatedDocument, errorsLimit)

if (violations.nonEmpty) Failure(ValidationError(violations, exceptionHandler))
else Success(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ class ResolverBasedAstSchemaBuilder[Ctx](val resolvers: Seq[AstSchemaResolver[Ct

def validateSchema(
schema: ast.Document,
validator: QueryValidator = ResolverBasedAstSchemaBuilder.validator): Vector[Violation] =
allowKnownDynamicDirectives(validator.validateQuery(validationSchema, schema))
validator: QueryValidator = ResolverBasedAstSchemaBuilder.validator,
errorsLimit: Option[Int] = None): Vector[Violation] =
allowKnownDynamicDirectives(validator.validateQuery(validationSchema, schema, errorsLimit))

def validateSchemaWithException(
schema: ast.Document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import scala.collection.mutable.{ListBuffer, Map => MutableMap, Set => MutableSe
import scala.reflect.{ClassTag, classTag}

trait QueryValidator {
def validateQuery(schema: Schema[_, _], queryAst: ast.Document): Vector[Violation]
// def validateQuery(schema: Schema[_, _], queryAst: ast.Document): Vector[Violation]
def validateQuery(
schema: Schema[_, _],
queryAst: ast.Document,
Expand Down Expand Up @@ -49,41 +49,32 @@ object QueryValidator {
new SingleFieldSubscriptions
)

@deprecated("use ruleBased setting 'errorsLimit' instead", "4.0.1")
def ruleBased(rules: List[ValidationRule]): RuleBasedQueryValidator =
RuleBasedQueryValidator(rules)
def ruleBased(rules: List[ValidationRule], errorsLimit: Option[Int]): RuleBasedQueryValidator =
new RuleBasedQueryValidator(rules, errorsLimit)
new RuleBasedQueryValidator(rules)

val empty: QueryValidator = new QueryValidator {
def validateQuery(schema: Schema[_, _], queryAst: ast.Document): Vector[Violation] =
Vector.empty
def validateQuery(
schema: Schema[_, _],
queryAst: ast.Document,
errorsLimit: Option[Int]): Vector[Violation] = Vector.empty
}

val default: RuleBasedQueryValidator = ruleBased(allRules, errorsLimit = Some(10))
val default: RuleBasedQueryValidator = ruleBased(allRules)
}

class RuleBasedQueryValidator(
rules: List[ValidationRule],
errorsLimit: Option[Int]
rules: List[ValidationRule]
) extends QueryValidator {
def validateQuery(schema: Schema[_, _], queryAst: ast.Document): Vector[Violation] =
validateQuery(schema, queryAst, errorsLimit)

def validateQuery(
schema: Schema[_, _],
queryAst: ast.Document,
errorsLimitOverride: Option[Int]): Vector[Violation] = {
errorsLimit: Option[Int]): Vector[Violation] = {
val ctx = new ValidationContext(
schema,
queryAst,
queryAst.sourceMapper,
new TypeInfo(schema),
errorsLimitOverride)
errorsLimit)

validateUsingRules(queryAst, ctx, rules.map(_.visitor(ctx)), topLevel = true)

Expand Down Expand Up @@ -166,15 +157,10 @@ class RuleBasedQueryValidator(
val cls = classTag[T].runtimeClass
val newRules = rules.filterNot(r => cls.isAssignableFrom(r.getClass))

RuleBasedQueryValidator(newRules)
new RuleBasedQueryValidator(newRules)
}
}

object RuleBasedQueryValidator {
def apply(rules: List[ValidationRule]): RuleBasedQueryValidator =
new RuleBasedQueryValidator(rules, None)
}

class ValidationContext(
val schema: Schema[_, _],
val doc: ast.Document,
Expand Down

0 comments on commit 42694dc

Please sign in to comment.