From 74ca89218f3cb9e1faaf6bd058407bf9473ac071 Mon Sep 17 00:00:00 2001 From: Kyri Petrou Date: Fri, 1 Dec 2023 07:55:39 +0100 Subject: [PATCH] Short-circuit collection of lists with 1 query --- .../src/main/scala/caliban/execution/Executor.scala | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/caliban/execution/Executor.scala b/core/src/main/scala/caliban/execution/Executor.scala index 779a27e79f..b36b19149c 100644 --- a/core/src/main/scala/caliban/execution/Executor.scala +++ b/core/src/main/scala/caliban/execution/Executor.scala @@ -15,7 +15,6 @@ import zio.query.{ Cache, UQuery, URQuery, ZQuery } import zio.stream.ZStream import scala.annotation.tailrec -import scala.collection.mutable import scala.jdk.CollectionConverters._ object Executor { @@ -37,16 +36,18 @@ object Executor { val wrapPureValues = fieldWrappers.exists(_.wrapPureValues) type ExecutionQuery[+A] = ZQuery[R, ExecutionError, A] - val execution = request.operationType match { + val execution = request.operationType match { case OperationType.Query => queryExecution case OperationType.Mutation => QueryExecution.Sequential case OperationType.Subscription => QueryExecution.Sequential } + def collectAll[In, E, A](in: List[In])(as: In => ZQuery[R, E, A]): ZQuery[R, E, List[A]] = - execution match { - case QueryExecution.Sequential => ZQuery.foreach(in)(as) - case QueryExecution.Parallel => ZQuery.foreachPar(in)(as) - case QueryExecution.Batched => ZQuery.foreachBatched(in)(as) + (in, execution) match { + case (head :: Nil, _) => as(head).map(List(_)) + case (_, QueryExecution.Sequential) => ZQuery.foreach(in)(as) + case (_, QueryExecution.Parallel) => ZQuery.foreachPar(in)(as) + case (_, QueryExecution.Batched) => ZQuery.foreachBatched(in)(as) } def reduceStep(