-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e62b2c
commit dc4823c
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package gql | ||
|
||
import munit.CatsEffectSuite | ||
import gql.ast._ | ||
import gql.dsl.all._ | ||
import cats.effect._ | ||
import cats.implicits._ | ||
import gql.resolver.Resolver | ||
|
||
class BatchTest extends CatsEffectSuite { | ||
case class Batchers( | ||
getString: Resolver[IO, Set[String], Map[String, String]], | ||
getInt: Resolver[IO, Set[Int], Map[Int, Int]] | ||
) | ||
|
||
lazy val intInputs = IO.ref(List.empty[Set[Int]]).unsafeRunSync() | ||
lazy val stringInputs = IO.ref(List.empty[Set[String]]).unsafeRunSync() | ||
|
||
lazy val batchersF = ( | ||
Resolver.batch[IO, String, String](xs => IO(xs.map(x => (x, x)).toMap) <* stringInputs.update(xs :: _)), | ||
Resolver.batch[IO, Int, Int](xs => IO(xs.map(x => (x, x)).toMap) <* intInputs.update(xs :: _)) | ||
).mapN(Batchers.apply) | ||
|
||
lazy val schema: Schema[IO, Unit, Unit, Unit] = Schema | ||
.stateful { | ||
batchersF.map { bs => | ||
case object OpaqueTpe | ||
implicit lazy val opaqueTpe: Type[IO, OpaqueTpe.type] = builder[IO, OpaqueTpe.type] { b => | ||
b.tpe( | ||
"Opaque", | ||
"b1" -> b(_.as("hello") andThen bs.getString.opt), | ||
"b2" -> b(_.as(1) andThen bs.getInt.opt) | ||
) | ||
} | ||
|
||
SchemaShape.unit[IO]( | ||
builder[IO, Unit] { b => | ||
b.fields( | ||
"opaque" -> b(_.as(OpaqueTpe)), | ||
"b2" -> b(_.as(2) andThen bs.getInt.opt) | ||
) | ||
} | ||
) | ||
} | ||
} | ||
.unsafeRunSync() | ||
|
||
test("should batch the two int resolvers") { | ||
val query = """ | ||
query { | ||
opaque { | ||
b1 | ||
b2 | ||
} | ||
b2 | ||
} | ||
""" | ||
|
||
Compiler[IO] | ||
.compile(schema, query) | ||
.traverse { | ||
case Application.Query(fa) => | ||
fa.flatMap { qr => | ||
assert(clue(qr.errors).isEmpty) | ||
assertEquals(qr.data("b2").get.asNumber.get.toInt.get, 2) | ||
val opq = qr.data("opaque").get.asObject.get | ||
assertEquals(opq("b1").get.asString.get, "hello") | ||
assertEquals(opq("b2").get.asNumber.get.toInt.get, 1) | ||
(intInputs.get, stringInputs.get).mapN { (ii, si) => | ||
assertEquals(ii, List(Set(1, 2))) | ||
assertEquals(si, List(Set("hello"))) | ||
} | ||
} | ||
case x => fail(s"unexpected program ${x.toString()}") | ||
} | ||
} | ||
} |