Skip to content

Instantly share code, notes, and snippets.

View ghostdogpr's full-sized avatar

Pierre Ricadat ghostdogpr

View GitHub Profile
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])
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])
import scala.deriving._
import scala.quoted._
import scala.quoted.matching._
import scala.compiletime._
trait Show[T] {
def show(x: T): String
}
object Show {
@ghostdogpr
ghostdogpr / caliban-client.scala
Last active February 8, 2020 00:06
Early draft for caliban-client. Feedback welcome!
// Step 1:
// Code generation creates helpers from a GraphQL schema
// Character.* and Queries.* here are auto-generated
// Step 2:
// Build your own queries combining those helpers
val character =
(Character.name ~
Character.nicknames ~
Character.origin).mapN(CharacterView)
import magnolia.{ CaseClass, Magnolia, SealedTrait }
import mercator.Monadic
import zio.random.Random
import zio.test.{ Gen, Sized }
object Generators {
implicit val genUnit: Typeclass[Unit] = Gen.unit
implicit val genBool: Typeclass[Boolean] = Gen.boolean
implicit val genString: Typeclass[String] = Gen.small(Gen.stringN(_)(Gen.alphaNumericChar))
import language.experimental.macros
import language.implicitConversions
import magnolia._
trait MyTypeclass[T] {
def f(t: T): String
}
object MyTypeclass {
type Typeclass[T] = MyTypeclass[T]
def listen(actorSystem: ActorSystem, topic: String): Task[Queue[String]] =
for {
queue <- Queue.bounded[String](1000)
rts <- Task.runtime[Any]
_ <- Task(actorSystem.actorOf(Props(new SubscriberActor(topic, rts, queue))))
} yield queue
case class MessageEnvelope(msg: String)
class SubscriberActor(topic: String, rts: Runtime[Any], queue: Queue[String]) extends Actor {
Stream.fromEffect {
IO.effectAsync[Any, Throwable, List[Message]] { cb =>
client
.receiveMessage(ReceiveMessageRequest.builder.queueUrl(queueUrl).maxNumberOfMessages(10).build)
.handle[Unit]((result, err) => {
err match {
case null => cb(IO.succeed(result.messages.asScala.toList))
case ex => cb(IO.fail(ex))
}
})
def sendMsg(actor: ActorRef, msg: String): Task[String] =
IO.fromFuture { implicit ctx =>
(actor ? msg).mapTo[String]
}
def send(client: SqsAsyncClient, queueUrl: String, msg: String): Task[Unit] =
IO.effectAsync[Any, Throwable, Unit] { cb =>
client
.sendMessage(SendMessageRequest.builder.queueUrl(queueUrl).messageBody(msg).build)
.handle[Unit]((_, err) => {
err match {
case null => cb(IO.unit)
case ex => cb(IO.fail(ex))
}
})