Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cats-effect 3 and http4s 0.23.0 #891

Merged
merged 14 commits into from
Aug 1, 2021
Merged
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- checkout
- restore_cache:
key: sbtcache
- run: sbt ++3.0.1! core/test catsInterop/compile monixInterop/compile clientJVM/test clientJS/compile zioHttp/compile tapirInterop/test
- run: sbt ++3.0.1! core/test catsInterop/compile monixInterop/compile clientJVM/test clientJS/compile zioHttp/compile tapirInterop/test http4s/test
- save_cache:
key: sbtcache
paths:
Expand Down
243 changes: 132 additions & 111 deletions adapters/http4s/src/main/scala/caliban/Http4sAdapter.scala

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions adapters/http4s/src/test/scala/caliban/Http4sAdapterSpec.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package caliban

import caliban.GraphQL.graphQL
import caliban.schema.GenericSchema
import caliban.schema.{ GenericSchema, Schema }
import caliban.uploads.{ Upload, Uploads }
import cats.effect.Blocker
import cats.syntax.semigroupk._
import io.circe.parser.parse
import io.circe.generic.auto._
Expand Down Expand Up @@ -72,18 +71,20 @@ case class UploadFileArgs(file: Upload)
case class UploadFilesArgs(files: List[Upload])

object TestAPI extends GenericSchema[Blocking with Uploads with Console with Clock] {
val api: GraphQL[Blocking with Uploads with Console with Clock] =
type Env = Blocking with Uploads with Console with Clock

implicit val uploadFileArgsSchema: Schema[Env, UploadFileArgs] = gen[UploadFileArgs]
implicit val mutationsSchema: Schema[Env, Mutations] = gen[Mutations]
implicit val queriesSchema: Schema[Env, Queries] = gen[Queries]

val api: GraphQL[Env] =
graphQL(
RootResolver(
Queries(args => UIO("stub")),
Mutations(args => Service.uploadFile(args.file), args => Service.uploadFiles(args.files))
)
)

implicit val uploadFileArgsSchema = gen[UploadFileArgs]
implicit val mutationsSchema = gen[Mutations]
implicit val queriesSchema = gen[Queries]

case class File(hash: String, path: String, filename: String, mimetype: String)

case class Queries(stub: Unit => UIO[String])
Expand All @@ -102,20 +103,17 @@ object Http4sAdapterSpec extends DefaultRunnableSpec {
Platform.default
)

val blocker = Blocker.liftExecutionContext(runtime.platform.executor.asEC)

val uri = Uri.unsafeParse("http://127.0.0.1:8089/")

val apiLayer: RLayer[R, Has[Server]] =
(for {
interpreter <- TestAPI.api.interpreter.toManaged_
server <- BlazeServerBuilder(runtime.platform.executor.asEC)
server <- BlazeServerBuilder[RIO[R, *]](runtime.platform.executor.asEC)
.bindHttp(uri.port.get, uri.host.get)
.withHttpApp(
(Http4sAdapter.makeHttpUploadService(
interpreter,
Paths.get(System.getProperty("java.io.tmpdir")),
blocker
Paths.get(System.getProperty("java.io.tmpdir"))
) <+> Http4sAdapter.makeHttpService(interpreter)).orNotFound
)
.resource
Expand Down
89 changes: 48 additions & 41 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ val scala213 = "2.13.6"
val scala3 = "3.0.1"
val allScala = Seq(scala212, scala213, scala3)

val akkaVersion = "2.6.15"
val catsEffectVersion = "2.5.2"
val circeVersion = "0.14.1"
val http4sVersion = "0.22.1"
val laminextVersion = "0.13.10"
val magnoliaVersion = "0.17.0"
val mercatorVersion = "0.2.1"
val playVersion = "2.8.8"
val playJsonVersion = "2.9.2"
val sttpVersion = "3.3.12"
val tapirVersion = "0.18.1"
val zioVersion = "1.0.10"
val zioInteropCatsVersion = "2.5.1.0"
val zioConfigVersion = "1.0.6"
val zqueryVersion = "0.2.9"
val zioJsonVersion = "0.1.5"
val zioHttpVersion = "1.0.0.0-RC17"
val akkaVersion = "2.6.15"
val catsEffect2Version = "2.5.2"
val catsEffect3Version = "3.2.1"
val circeVersion = "0.14.1"
val http4sVersion = "0.23.0"
val laminextVersion = "0.13.10"
val magnoliaVersion = "0.17.0"
val mercatorVersion = "0.2.1"
val playVersion = "2.8.8"
val playJsonVersion = "2.9.2"
val sttpVersion = "3.3.12"
val tapirVersion = "0.18.1"
val zioVersion = "1.0.10"
val zioInteropCats2Version = "2.5.1.0"
val zioInteropCats3Version = "3.1.1.0"
val zioConfigVersion = "1.0.6"
val zqueryVersion = "0.2.9"
val zioJsonVersion = "0.1.5"
val zioHttpVersion = "1.0.0.0-RC17"

inThisBuild(
List(
Expand Down Expand Up @@ -190,9 +192,12 @@ lazy val catsInterop = project
.settings(name := "caliban-cats")
.settings(commonSettings)
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio-interop-cats" % zioInteropCatsVersion,
"org.typelevel" %% "cats-effect" % catsEffectVersion
libraryDependencies ++= {
if (scalaVersion.value == scala3) Seq()
else Seq(compilerPlugin(("org.typelevel" %% "kind-projector" % "0.13.0").cross(CrossVersion.full)))
} ++ Seq(
"dev.zio" %% "zio-interop-cats" % zioInteropCats3Version,
"org.typelevel" %% "cats-effect" % catsEffect3Version
)
)
.dependsOn(core)
Expand All @@ -204,7 +209,7 @@ lazy val monixInterop = project
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio-interop-reactivestreams" % "1.3.5",
"dev.zio" %% "zio-interop-cats" % zioInteropCatsVersion,
"dev.zio" %% "zio-interop-cats" % zioInteropCats2Version,
"io.monix" %% "monix" % "3.4.0"
)
)
Expand Down Expand Up @@ -233,24 +238,26 @@ lazy val http4s = project
.settings(name := "caliban-http4s")
.settings(commonSettings)
.settings(
crossScalaVersions -= scala3,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It supports scala 3, nice!

testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")),
libraryDependencies ++= Seq(
"dev.zio" %% "zio-interop-cats" % zioInteropCatsVersion,
"org.typelevel" %% "cats-effect" % catsEffectVersion,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"org.http4s" %% "http4s-blaze-server" % http4sVersion,
"io.circe" %% "circe-parser" % circeVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % sttpVersion % Test,
"com.softwaremill.sttp.client3" %% "circe" % sttpVersion % Test,
"io.circe" %% "circe-generic" % circeVersion % Test,
compilerPlugin(("org.typelevel" %% "kind-projector" % "0.13.0").cross(CrossVersion.full))
)
libraryDependencies ++= {
if (scalaVersion.value == scala3) Seq()
else Seq(compilerPlugin(("org.typelevel" %% "kind-projector" % "0.13.0").cross(CrossVersion.full)))
} ++
Seq(
"dev.zio" %% "zio-interop-cats" % zioInteropCats3Version,
"org.typelevel" %% "cats-effect" % catsEffect3Version,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"org.http4s" %% "http4s-blaze-server" % http4sVersion,
"io.circe" %% "circe-parser" % circeVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % sttpVersion % Test,
"com.softwaremill.sttp.client3" %% "circe" % sttpVersion % Test,
"io.circe" %% "circe-generic" % circeVersion % Test
)
)
.dependsOn(core)
.dependsOn(core, catsInterop)

lazy val zioHttp = project
.in(file("adapters/zio-http"))
Expand Down Expand Up @@ -298,8 +305,8 @@ lazy val finch = project
libraryDependencies ++= Seq(
"com.github.finagle" %% "finchx-core" % "0.32.1",
"com.github.finagle" %% "finchx-circe" % "0.32.1",
"dev.zio" %% "zio-interop-cats" % zioInteropCatsVersion,
"org.typelevel" %% "cats-effect" % catsEffectVersion,
"dev.zio" %% "zio-interop-cats" % zioInteropCats2Version,
"org.typelevel" %% "cats-effect" % catsEffect2Version,
"io.circe" %% "circe-parser" % circeVersion
)
)
Expand Down Expand Up @@ -399,9 +406,9 @@ lazy val examples = project
akkaHttp,
http4s,
catsInterop,
finch,
/*finch,*/
play,
monixInterop,
/*monixInterop,*/
tapirInterop,
clientJVM,
federation,
Expand Down
12 changes: 4 additions & 8 deletions examples/src/main/scala/example/federation/FederatedApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import example.federation.FederationData.episodes.sampleEpisodes
import caliban.Http4sAdapter

import cats.data.Kleisli
import cats.effect.Blocker
import org.http4s.StaticFile
import org.http4s.implicits._
import org.http4s.server.Router
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.middleware.CORS
import zio._
import zio.blocking.Blocking
import zio.interop.catz._

import scala.concurrent.ExecutionContext
Expand All @@ -26,18 +24,17 @@ object FederatedApp extends CatsApp {
.memoize
.use(layer =>
for {
blocker <- ZIO.access[Blocking](_.get.blockingExecutor.asEC).map(Blocker.liftExecutionContext)
interpreter <- FederatedApi.Characters.api.interpreter.map(_.provideCustomLayer(layer))
_ <- BlazeServerBuilder[ExampleTask](ExecutionContext.global)
.bindHttp(8089, "localhost")
.withHttpApp(
Router[ExampleTask](
"/api/graphql" -> CORS(Http4sAdapter.makeHttpService(interpreter)),
"/graphiql" -> Kleisli.liftF(StaticFile.fromResource("/graphiql.html", blocker, None))
"/graphiql" -> Kleisli.liftF(StaticFile.fromResource("/graphiql.html", None))
).orNotFound
)
.resource
.toManaged
.toManagedZIO
.useForever
} yield ()
)
Expand All @@ -47,18 +44,17 @@ object FederatedApp extends CatsApp {
.memoize
.use(layer =>
for {
blocker <- ZIO.access[Blocking](_.get.blockingExecutor.asEC).map(Blocker.liftExecutionContext)
interpreter <- FederatedApi.Episodes.api.interpreter.map(_.provideCustomLayer(layer))
_ <- BlazeServerBuilder[ExampleTask](ExecutionContext.global)
.bindHttp(8088, "localhost")
.withHttpApp(
Router[ExampleTask](
"/api/graphql" -> CORS(Http4sAdapter.makeHttpService(interpreter)),
"/graphiql" -> Kleisli.liftF(StaticFile.fromResource("/graphiql.html", blocker, None))
"/graphiql" -> Kleisli.liftF(StaticFile.fromResource("/graphiql.html", None))
).orNotFound
)
.resource
.toManaged
.toManagedZIO
.useForever
} yield ()
)
Expand Down
2 changes: 2 additions & 0 deletions examples/src/main/scala/example/finch/ExampleApp.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package example.finch
/*

import example.ExampleData.sampleCharacters
import example.ExampleService.ExampleService
Expand Down Expand Up @@ -54,3 +55,4 @@ object ExampleApp extends App with Endpoint.Module[Task] {
Await.ready(server)

}
*/
3 changes: 1 addition & 2 deletions examples/src/main/scala/example/http4s/AuthExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.http4s.server.{ Router, ServiceErrorHandler }
import org.typelevel.ci.CIString
import zio._
import zio.interop.catz._
import zio.interop.catz.implicits._

import scala.concurrent.ExecutionContext

Expand Down Expand Up @@ -62,7 +61,7 @@ object AuthExampleApp extends CatsApp {
.bindHttp(8088, "localhost")
.withHttpApp(Router[Task]("/api/graphql" -> route).orNotFound)
.resource
.toManaged
.toManagedZIO
.useForever
} yield ()).exitCode
}
7 changes: 2 additions & 5 deletions examples/src/main/scala/example/http4s/ExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import example.{ ExampleApi, ExampleService }
import caliban.Http4sAdapter

import cats.data.Kleisli
import cats.effect.Blocker
import org.http4s.StaticFile
import org.http4s.implicits._
import org.http4s.server.Router
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.middleware.CORS
import zio._
import zio.blocking.Blocking
import zio.interop.catz._

import scala.concurrent.ExecutionContext
Expand All @@ -28,19 +26,18 @@ object ExampleApp extends App {
.runtime[ZEnv with ExampleService]
.flatMap(implicit runtime =>
for {
blocker <- ZIO.access[Blocking](_.get.blockingExecutor.asEC).map(Blocker.liftExecutionContext)
interpreter <- ExampleApi.api.interpreter
_ <- BlazeServerBuilder[ExampleTask](ExecutionContext.global)
.bindHttp(8088, "localhost")
.withHttpApp(
Router[ExampleTask](
"/api/graphql" -> CORS(Http4sAdapter.makeHttpService(interpreter)),
"/ws/graphql" -> CORS(Http4sAdapter.makeWebSocketService(interpreter)),
"/graphiql" -> Kleisli.liftF(StaticFile.fromResource("/graphiql.html", blocker, None))
"/graphiql" -> Kleisli.liftF(StaticFile.fromResource("/graphiql.html", None))
).orNotFound
)
.resource
.toManaged
.toManagedZIO
.useForever
} yield ()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import caliban.GraphQL.graphQL
import caliban.RootResolver

import cats.effect.{ ExitCode, IO, IOApp }
import cats.effect.std.Dispatcher
import zio.{ Runtime, ZEnv }

object ExampleCatsInterop extends IOApp {

import caliban.interop.cats.implicits._

implicit val runtime: Runtime[ZEnv] = Runtime.default
implicit val zioRuntime: Runtime[ZEnv] = Runtime.default

case class Number(value: Int)

Expand All @@ -20,7 +21,6 @@ object ExampleCatsInterop extends IOApp {
val randomNumber = IO(scala.util.Random.nextInt()).map(Number)

val queries = Queries(numbers, randomNumber)
val api = graphQL(RootResolver(queries))

val query = """
{
Expand All @@ -34,10 +34,14 @@ object ExampleCatsInterop extends IOApp {
}"""

override def run(args: List[String]): IO[ExitCode] =
for {
interpreter <- api.interpreterAsync[IO]
_ <- interpreter.checkAsync[IO](query)
result <- interpreter.executeAsync[IO](query)
_ <- IO(println(result.data))
} yield ExitCode.Success
Dispatcher[IO].use { implicit dispatcher => // required for a derivation of the schema
val api = graphQL(RootResolver(queries))

for {
interpreter <- api.interpreterAsync[IO]
_ <- interpreter.checkAsync[IO](query)
result <- interpreter.executeAsync[IO](query)
_ <- IO(println(result.data))
} yield ExitCode.Success
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package example.interop.monix
/*

import caliban.GraphQL.graphQL
import caliban.ResponseValue.{ ObjectValue, StreamValue }
Expand Down Expand Up @@ -61,3 +62,4 @@ object ExampleMonixInterop extends TaskApp {
}
} yield ExitCode.Success
}
*/
Loading