Last active
May 16, 2020 05:13
-
-
Save ghostdogpr/69fe262f6368b9d8e7910dce496442e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment