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

Run stream of deferred queries sequentially #1981

Merged
merged 2 commits into from
Nov 6, 2023
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 core/src/main/scala/caliban/execution/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ object Executor {
ZStream.succeed(resp) ++ makeDeferStream(more, cache)
})

ZStream.mergeAllUnbounded()(defers.map(run): _*)
ZStream.mergeAll(1)(defers.map(run): _*)
}

def runIncrementalQuery(
Expand Down
31 changes: 28 additions & 3 deletions core/src/test/scala/caliban/execution/DeferredExecutionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import caliban.ResponseValue.StreamValue
import caliban.TestUtils.{ characters, Character }
import caliban.{ CalibanError, GraphQLResponse, ResponseValue, Value }
import zio.test.Assertion.hasSameElements
import zio.test.{ assert, assertTrue, TestAspect, ZIOSpecDefault }
import zio.{ Chunk, UIO, ZIO, ZLayer }
import zio.test.{ assert, assertTrue, TestAspect, TestClock, ZIOSpecDefault }
import zio._

trait CharacterService {
def characterBy(pred: Character => Boolean): UIO[List[Character]]
Expand Down Expand Up @@ -188,7 +188,32 @@ object DeferredExecutionSpec extends ZIOSpecDefault {
"""{"hasNext":false}"""
)
)
}
},
test("deferred fields backed by a datasource") {
import TestDatasourceDeferredSchema._
val query = gqldoc("""
{
foo {
bar { ... @defer { value } }
}
}""")

for {
resp <- interpreter.flatMap(_.execute(query))
rest <- runIncrementalResponses(resp)
head = rest.head.toString
mid = rest.tail.init.toList.map(_.toString)
last = rest.last.toString
} yield assertTrue(
head == """{"foo":{"bar":[{},{},{}]}}""",
mid.sorted == List(
"""{"incremental":[{"data":{"value":"value"},"path":["foo","bar",0]}],"hasNext":true}""",
"""{"incremental":[{"data":{"value":"value"},"path":["foo","bar",1]}],"hasNext":true}""",
"""{"incremental":[{"data":{"value":"value"},"path":["foo","bar",2]}],"hasNext":true}"""
),
last == """{"hasNext":false}"""
)
} @@ TestAspect.nonFlaky(50)
).provide(CharacterService.test)

def runIncrementalResponses(response: GraphQLResponse[CalibanError]) =
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/scala/caliban/execution/TestDeferredSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import caliban.{ graphQL, RootResolver }
import caliban.schema.Annotations.GQLName
import caliban.schema.{ GenericSchema, Schema }
import caliban.wrappers.DeferSupport
import zio.query.{ DataSource, Request, UQuery, ZQuery }
import zio.{ UIO, URIO, ZIO }

object TestDeferredSchema extends GenericSchema[CharacterService] {
Expand Down Expand Up @@ -77,3 +78,22 @@ object TestDeferredSchema extends GenericSchema[CharacterService] {

val interpreter = (graphQL(resolver) @@ DeferSupport.defer).interpreter
}

object TestDatasourceDeferredSchema extends GenericSchema[Any] {
import auto._

case class Bar(value: UQuery[String])
case class Foo(bar: UIO[List[Bar]])
case class Query(foo: UIO[Foo])

case class Req(i: Int) extends Request[Nothing, String]
private val ds = DataSource.fromFunctionZIO("ValuesDS")((_: Req) => ZIO.succeed("value"))

private def makeBar(i: Int): Bar = Bar(ZQuery.fromRequest(Req(i))(ds))

private val resolver = RootResolver(Query(ZIO.succeed {
Foo(bar = ZIO.succeed(List(makeBar(1), makeBar(3), makeBar(3))))
}))

val interpreter = (graphQL(resolver) @@ DeferSupport.defer).interpreter
}