Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update prelude & minor validator optimizations #2113

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}