Created
May 16, 2020 04:58
-
-
Save ghostdogpr/dc9acb0dbf1ab8cc1e1d60a0b8a33980 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.console.putStrLn | |
import zio.ZIO | |
import zio.zquery._ | |
object Test extends zio.App { | |
case class User(firstName: UQuery[String], lastName: UQuery[String], age: UQuery[Int]) | |
case class Query(getUser: String => User) | |
sealed trait GetUser[+A] extends Request[Nothing, A] | |
case class GetFirstName(userId: String) extends GetUser[String] | |
case class GetLastName(userId: String) extends GetUser[String] | |
case class GetAge(userId: String) extends GetUser[Int] | |
val UserDataSource: DataSource[Any, GetUser[Any]] = | |
DataSource.fromFunctionBatched[GetUser[Any], Any]("User") { requests => | |
// build SQL query with only the fields in requests | |
requests.map { | |
case GetFirstName(userId) => s"firstName of $userId" | |
case GetLastName(userId) => s"lastName of $userId" | |
case GetAge(_) => 35 | |
} | |
} | |
def firstName(id: String): UQuery[String] = ZQuery.fromRequest(GetFirstName(id))(UserDataSource) | |
def lastName(id: String): UQuery[String] = ZQuery.fromRequest(GetLastName(id))(UserDataSource) | |
def age(id: String): UQuery[Int] = ZQuery.fromRequest(GetAge(id))(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