Skip to content

Instantly share code, notes, and snippets.

@ghostdogpr
Last active June 28, 2024 06:25
Show Gist options
  • Save ghostdogpr/7eaf3d6b38a78a3a55723954f4fa9ba6 to your computer and use it in GitHub Desktop.
Save ghostdogpr/7eaf3d6b38a78a3a55723954f4fa9ba6 to your computer and use it in GitHub Desktop.
Caliban + Ox demo
//> using dep com.github.ghostdogpr::caliban-tapir:2.8.0
//> using dep com.softwaremill.ox::core:0.2.2
//> using dep com.softwaremill.sttp.tapir::tapir-jsoniter-scala:1.10.10
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.10.10
import caliban.*
import caliban.interop.tapir.*
import caliban.schema.Schema
import _root_.ox.*
import scala.concurrent.duration.*
import sttp.tapir.json.jsoniter.*
import sttp.tapir.server.netty.sync.{NettySyncServer, OxStreams}
import zio.Runtime
def computation1: Int = { sleep(2.seconds); 1 }
def computation2: String = { sleep(1.second); "2" }
case class Query(
result1: () => Int,
result2: () => String
) derives Schema.SemiAuto
val api: GraphQL[Any] =
graphQL(
RootResolver(
Query(
result1 = () => computation1,
result2 = () => computation2
)
)
)
println(api.render)
val endpoints =
HttpInterpreter(api.interpreterUnsafe)
.serverEndpointsIdentity(OxStreams)(Runtime.default)
NettySyncServer().port(8080).addEndpoints(endpoints).startAndWait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment