Last active
May 16, 2020 05:13
Revisions
-
ghostdogpr created this gist
May 16, 2020 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ package caliban import caliban.GraphQL._ import zio.ZIO import zio.console.putStrLn import zio.zquery._ object Test2 extends zio.App { case class User(firstName: UQuery[String], lastName: UQuery[String], age: UQuery[Int]) case class Query(getUser: String => User) case class GetUser[+A](userId: String, field: String) extends Request[Nothing, A] val UserDataSource: DataSource[Any, GetUser[Any]] = DataSource.fromFunctionBatched[GetUser[Any], Any]("User") { requests => // build SQL query with only the fields in requests requests.map { req => req.field match { case "first_name" => s"firstName of ${req.userId}" case "last_name" => s"lastName of ${req.userId}" case "age" => 35 } } } def firstName(id: String): UQuery[String] = ZQuery.fromRequest(GetUser(id, "first_name"))(UserDataSource) def lastName(id: String): UQuery[String] = ZQuery.fromRequest(GetUser(id, "last_name"))(UserDataSource) def age(id: String): UQuery[Int] = ZQuery.fromRequest(GetUser(id, "age"))(UserDataSource) val resolver = RootResolver(Query(id => User(firstName(id), lastName(id), age(id)))) val api = graphQL(resolver) override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, Int] = for { interpreter <- api.interpreter.orDie query = "{getUser(value:\"userA\"){firstName lastName age}}" res <- interpreter.execute(query) _ <- putStrLn(res.data.toString) } yield 0 }