Skip to content

Commit

Permalink
Update prelude & minor validator optimizations (#2113)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored Feb 7, 2024
1 parent 5831772 commit 1ddcdea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ val zqueryVersion = "0.6.0"
val zioJsonVersion = "0.6.2"
val zioHttpVersion = "3.0.0-RC4"
val zioOpenTelemetryVersion = "3.0.0-RC21"
val zioPreludeVersion = "1.0.0-RC22"
val zioPreludeVersion = "1.0.0-RC23"

Global / onChangedBuildSource := ReloadOnSourceChanges

Expand Down
20 changes: 12 additions & 8 deletions core/src/main/scala/caliban/validation/Validator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,15 @@ object Validator {
checkDirectivesUniqueness(op.directives, directiveDefinitions).as(op.operationType match {
case OperationType.Query => op.directives.map((_, __DirectiveLocation.QUERY))
case OperationType.Mutation => op.directives.map((_, __DirectiveLocation.MUTATION))
case OperationType.Subscription =>
op.directives.map((_, __DirectiveLocation.SUBSCRIPTION))
case OperationType.Subscription => op.directives.map((_, __DirectiveLocation.SUBSCRIPTION))
})
)
fragmentDirectives <- ZPure.foreach(context.fragments.values)(fragment =>
fragmentDirectives <- ZPure.foreach(context.fragments.values.toList)(fragment =>
checkDirectivesUniqueness(fragment.directives, directiveDefinitions)
.as(fragment.directives.map((_, __DirectiveLocation.FRAGMENT_DEFINITION)))
)
selectionDirectives <- collectDirectives(context.selectionSets, directiveDefinitions)
} yield opDirectives.flatten ++ fragmentDirectives.flatten ++ selectionDirectives
} yield opDirectives.flatten ::: fragmentDirectives.flatten ::: selectionDirectives

private def collectDirectives(
selectionSet: List[Selection],
Expand Down Expand Up @@ -368,7 +367,7 @@ object Validator {

lazy val validateVariables: QueryValidation =
ZPure.serviceWithPure { context =>
ZPure.foreachDiscard(context.operations)(op =>
validateAll(context.operations)(op =>
validateAll(op.variableDefinitions.groupBy(_.name)) { case (name, variables) =>
ZPure.when(variables.length > 1)(
failValidation(
Expand Down Expand Up @@ -1210,10 +1209,15 @@ object Validator {
}
}

// Pure's implementation doesn't check if the Iterable is empty, and this is causing some performance degradation
/**
* Wrapper around `ZPure.foreachDiscard` optimized for cases where the input is empty or has only one element.
*/
private def validateAll[R, A, B](
in: Iterable[A]
)(f: A => EReader[R, ValidationError, B]): EReader[R, ValidationError, Unit] =
if (in.isEmpty) zunit
else ZPure.foreachDiscard(in)(f)
in.sizeCompare(1) match {
case -1 => zunit
case 0 => f(in.head).unit
case _ => ZPure.foreachDiscard(in)(f)
}
}

0 comments on commit 1ddcdea

Please sign in to comment.