Skip to content

Commit

Permalink
chore: update zio-http (#1216)
Browse files Browse the repository at this point in the history
* chore: update to tapir 0.20.0

* Fix sttp ws issue

Co-authored-by: kokishibata <[email protected]>
Co-authored-by: Pierre Ricadat <[email protected]>
  • Loading branch information
3 people authored Mar 1, 2022
1 parent 1ab9aa8 commit b537c4c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 70 deletions.
32 changes: 28 additions & 4 deletions adapters/http4s/src/main/scala/caliban/Http4sAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,15 @@ object Http4sAdapter {
def convertHttpEndpointToF[F[_], R, E](
endpoint: ServerEndpoint[Any, RIO[R, *]]
)(implicit interop: ToEffect[F, R]): ServerEndpoint[Any, F] =
ServerEndpoint[endpoint.A, endpoint.U, endpoint.I, endpoint.E, endpoint.O, Any, F](
ServerEndpoint[
endpoint.SECURITY_INPUT,
endpoint.PRINCIPAL,
endpoint.INPUT,
endpoint.ERROR_OUTPUT,
endpoint.OUTPUT,
Any,
F
](
endpoint.endpoint,
_ => a => interop.toEffect(endpoint.securityLogic(zioMonadError)(a)),
_ => u => req => interop.toEffect(endpoint.logic(zioMonadError)(u)(req))
Expand All @@ -214,11 +222,27 @@ object Http4sAdapter {

val e = endpoint
.asInstanceOf[
ServerEndpoint.Full[endpoint.A, endpoint.U, endpoint.I, endpoint.E, CalibanPipe, ZioWebSockets, RIO[R, *]]
ServerEndpoint.Full[
endpoint.SECURITY_INPUT,
endpoint.PRINCIPAL,
endpoint.INPUT,
endpoint.ERROR_OUTPUT,
CalibanPipe,
ZioWebSockets,
RIO[R, *]
]
]

ServerEndpoint[endpoint.A, endpoint.U, endpoint.I, endpoint.E, Fs2Pipe, Fs2Streams[F] with WebSockets, F](
e.endpoint.asInstanceOf[Endpoint[endpoint.A, endpoint.I, endpoint.E, Fs2Pipe, Any]],
ServerEndpoint[
endpoint.SECURITY_INPUT,
endpoint.PRINCIPAL,
endpoint.INPUT,
endpoint.ERROR_OUTPUT,
Fs2Pipe,
Fs2Streams[F] with WebSockets,
F
](
e.endpoint.asInstanceOf[Endpoint[endpoint.SECURITY_INPUT, endpoint.INPUT, endpoint.ERROR_OUTPUT, Fs2Pipe, Any]],
_ => a => interop.toEffect(e.securityLogic(zioMonadError)(a)),
_ =>
u =>
Expand Down
29 changes: 15 additions & 14 deletions adapters/zio-http/src/main/scala/caliban/ZHttpAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ object ZHttpAdapter {
queryExecution: QueryExecution = QueryExecution.Parallel,
webSocketHooks: WebSocketHooks[R, E] = WebSocketHooks.empty
): HttpApp[R with Clock, E] =
HttpApp.responseM(
Http.responseZIO[R with Clock, E](
for {
ref <- Ref.make(Map.empty[String, Promise[Any, Unit]])
} yield Response.socket(
socketHandler[R, E](
ref,
interpreter,
skipValidation,
enableIntrospection,
keepAliveTime,
queryExecution,
webSocketHooks
)
)
app <- Response.fromSocketApp[R with Clock](
socketHandler[R, E](
ref,
interpreter,
skipValidation,
enableIntrospection,
keepAliveTime,
queryExecution,
webSocketHooks
)
)
} yield app
)

private def socketHandler[R, E](
Expand All @@ -66,7 +67,7 @@ object ZHttpAdapter {
keepAliveTime: Option[Duration],
queryExecution: QueryExecution,
webSocketHooks: WebSocketHooks[R, E] = WebSocketHooks.empty
): SocketApp[R with Clock, E] = {
): SocketApp[R with Clock] = {
val routes = Socket.collect[WebSocketFrame] { case Text(text) =>
ZStream
.fromEffect(ZIO.fromEither(decode[GraphQLWSInput](text)))
Expand Down Expand Up @@ -122,7 +123,7 @@ object ZHttpAdapter {
.map(output => WebSocketFrame.Text(output.asJson.noSpaces))
}

SocketApp.message(routes) ++ SocketApp.protocol(protocol)
SocketApp(routes).withProtocol(protocol)
}

private val protocol = SocketProtocol.subProtocol("graphql-ws")
Expand Down
10 changes: 4 additions & 6 deletions adapters/zio-http/src/test/scala/caliban/ZHttpAdapterSpec.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package caliban

import caliban.interop.tapir.TestData.sampleCharacters
import caliban.interop.tapir.TestService.TestService
import caliban.interop.tapir.{ FakeAuthorizationInterceptor, TapirAdapterSpec, TestApi, TestService }
import caliban.uploads.Uploads
import sttp.client3.UriContext
import zhttp.http._
import zhttp.service.Server
import zio._
import zio.clock.Clock
import zio.console.Console
import zio.duration._
import zio.test.{ DefaultRunnableSpec, TestFailure, ZSpec }

Expand All @@ -23,19 +21,19 @@ object ZHttpAdapterSpec extends DefaultRunnableSpec {
_ <- Server
.start(
8088,
Http.route {
case _ -> Root / "api" / "graphql" =>
Http.route[Request] {
case _ -> !! / "api" / "graphql" =>
ZHttpAdapter.makeHttpService(
interpreter,
requestInterceptor = FakeAuthorizationInterceptor.bearer
)
case _ -> Root / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
case _ -> !! / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
}
)
.forkManaged
_ <- clock.sleep(3 seconds).toManaged_
} yield ())
.provideCustomLayer(TestService.make(sampleCharacters) ++ Uploads.empty ++ Clock.live)
.provideCustomLayer(TestService.make(sampleCharacters) ++ Uploads.empty +!+ Clock.live)
.toLayer

def spec: ZSpec[ZEnv, Any] = {
Expand Down
17 changes: 9 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ val scala213 = "2.13.8"
val scala3 = "3.1.1"
val allScala = Seq(scala212, scala213, scala3)

val akkaVersion = "2.6.17"
val akkaVersion = "2.6.18"
val catsEffect2Version = "2.5.4"
val catsEffect3Version = "3.3.6"
val catsMtlVersion = "1.2.1"
Expand All @@ -18,14 +18,14 @@ val mercatorVersion = "0.2.1"
val playVersion = "2.8.13"
val playJsonVersion = "2.9.2"
val sttpVersion = "3.3.18"
val tapirVersion = "0.19.4"
val tapirVersion = "0.20.0"
val zioVersion = "1.0.13"
val zioInteropCats2Version = "2.5.1.0"
val zioInteropCats3Version = "3.2.9.1"
val zioConfigVersion = "1.0.10"
val zqueryVersion = "0.2.10"
val zioJsonVersion = "0.1.5"
val zioHttpVersion = "1.0.0.0-RC17"
val zioHttpVersion = "1.0.0.0-RC25"

inThisBuild(
List(
Expand Down Expand Up @@ -239,8 +239,9 @@ lazy val tapirInterop = project
} ++
Seq(
"com.softwaremill.sttp.tapir" %% "tapir-core" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio1" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-sttp-client" % tapirVersion % Test,
"com.softwaremill.sttp.tapir" %% "tapir-sttp-client-ws-zio1" % tapirVersion % Test,
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirVersion % Test,
"com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % sttpVersion % Test,
"dev.zio" %% "zio-test" % zioVersion % Test,
Expand All @@ -262,7 +263,7 @@ lazy val http4s = project
Seq(
"dev.zio" %% "zio-interop-cats" % zioInteropCats3Version,
"org.typelevel" %% "cats-effect" % catsEffect3Version,
"com.softwaremill.sttp.tapir" %% "tapir-zio-http4s-server" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio1-http4s-server" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirVersion,
"org.http4s" %% "http4s-blaze-server" % http4sVersion % Test,
"dev.zio" %% "zio-test" % zioVersion % Test,
Expand All @@ -284,9 +285,9 @@ lazy val zioHttp = project
),
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")),
libraryDependencies ++= Seq(
"io.d11" %% "zhttp" % zioHttpVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio-http-server" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirVersion
"io.d11" %% "zhttp" % zioHttpVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio1-http-server" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirVersion
)
)
.dependsOn(core, tapirInterop % "compile->compile;test->test")
Expand Down
12 changes: 5 additions & 7 deletions examples/src/main/scala/example/stitching/ExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,13 @@ object Configuration {
)
}

import zio._
import zio.stream._
import zhttp.http._
import zhttp.service.Server
import caliban.ZHttpAdapter

object ExampleApp extends App {
private val graphiql =
Http.succeed(Response.http(content = HttpData.fromStream(ZStream.fromResource("graphiql.html"))))
private val graphiql = Http.fromStream(ZStream.fromResource("graphiql.html"))

override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] =
(for {
Expand All @@ -110,10 +108,10 @@ object ExampleApp extends App {
_ <- Server
.start(
8088,
Http.route {
case _ -> Root / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
case _ -> Root / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
case _ -> Root / "graphiql" => graphiql
Http.route[Request] {
case _ -> !! / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
case _ -> !! / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
case _ -> !! / "graphiql" => graphiql
}
)
.forever
Expand Down
24 changes: 9 additions & 15 deletions examples/src/main/scala/example/ziohttp/AuthExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object Auth {
.toLayer

object WebSockets {
private val wsSession = Http.fromEffect(Ref.make[Option[String]](None))
private val wsSession = Http.fromZIO(Ref.make[Option[String]](None))

def live[R <: Has[Auth] with Clock](
interpreter: GraphQLInterpreter[R, CalibanError]
Expand Down Expand Up @@ -85,11 +85,11 @@ object Auth {
}

def middleware[R, B](
app: Http[R, Throwable, Request, Response[R, Throwable]]
app: Http[R, Throwable, Request, Response]
): HttpApp[R with Has[Auth], Throwable] =
Http
.fromEffectFunction[Request] { (request: Request) =>
val user = request.headers.find(_.name == "Authorization").map(_.value.toString())
.fromFunctionZIO[Request] { (request: Request) =>
val user = request.headers.authorization.map(_.toString())

ZIO.serviceWith[Auth](_.setUser(user)).as(app)
}
Expand All @@ -109,27 +109,21 @@ object Authed extends GenericSchema[ZEnv with Has[Auth]] {
}

object AuthExampleApp extends App {
private val graphiql =
Http.succeed(
Response.http(
content = HttpData.fromStream(ZStream.fromResource("graphiql.html")),
headers = List(Header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_HTML))
)
)
private val graphiql = Http.fromStream(ZStream.fromResource("graphiql.html"))

override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] =
(for {
interpreter <- (ExampleApi.api |+| Authed.api).interpreter
_ <- Server
.start(
8088,
Http.route {
case _ -> Root / "api" / "graphql" =>
Http.route[Request] {
case _ -> !! / "api" / "graphql" =>
Auth.middleware(
ZHttpAdapter.makeHttpService(interpreter)
)
case _ -> Root / "ws" / "graphql" => Auth.WebSockets.live(interpreter)
case _ -> Root / "graphiql" => graphiql
case _ -> !! / "ws" / "graphql" => Auth.WebSockets.live(interpreter)
case _ -> !! / "graphiql" => graphiql
}
)
.forever
Expand Down
16 changes: 5 additions & 11 deletions examples/src/main/scala/example/ziohttp/ExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@ import zhttp.http._
import zhttp.service.Server

object ExampleApp extends App {
private val graphiql =
Http.succeed(
Response.http(
content = HttpData.fromStream(ZStream.fromResource("graphiql.html")),
headers = List(Header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_HTML))
)
)
private val graphiql = Http.fromStream(ZStream.fromResource("graphiql.html"))

override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] =
(for {
interpreter <- ExampleApi.api.interpreter
_ <- Server
.start(
8088,
Http.route {
case _ -> Root / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
case _ -> Root / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
case _ -> Root / "graphiql" => graphiql
Http.route[Request] {
case _ -> !! / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
case _ -> !! / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
case _ -> !! / "graphiql" => graphiql
}
)
.forever
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import sttp.capabilities.zio.ZioStreams
import sttp.capabilities.zio.ZioStreams.Pipe
import sttp.model.{ headers => _, _ }
import sttp.monad.MonadError
import sttp.tapir.Codec.{ mediaType, JsonCodec }
import sttp.tapir.Codec.JsonCodec
import sttp.tapir._
import sttp.tapir.model.ServerRequest
import sttp.tapir.server.ServerEndpoint
Expand Down Expand Up @@ -329,7 +329,15 @@ object TapirAdapter {
def convertHttpEndpointToFuture[E, R](
endpoint: ServerEndpoint[Any, RIO[R, *]]
)(implicit runtime: Runtime[R]): ServerEndpoint[Any, Future] =
ServerEndpoint[endpoint.A, endpoint.U, endpoint.I, endpoint.E, endpoint.O, Any, Future](
ServerEndpoint[
endpoint.SECURITY_INPUT,
endpoint.PRINCIPAL,
endpoint.INPUT,
endpoint.ERROR_OUTPUT,
endpoint.OUTPUT,
Any,
Future
](
endpoint.endpoint,
_ => a => runtime.unsafeRunToFuture(endpoint.securityLogic(zioMonadError)(a)).future,
_ => u => req => runtime.unsafeRunToFuture(endpoint.logic(zioMonadError)(u)(req)).future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package caliban.interop.tapir
import caliban.InputValue.ObjectValue
import caliban.Value.StringValue
import caliban.{ CalibanError, GraphQLRequest, GraphQLWSInput }
import sttp.client3.UriContext
import sttp.client3.asynchttpclient.zio._
import sttp.model.{ Header, MediaType, Method, Part, QueryParams, StatusCode, Uri }
import sttp.tapir.client.sttp.SttpClientInterpreter
import sttp.tapir.client.sttp.ws.zio._
import sttp.tapir.client.sttp.ws.zio1._
import sttp.tapir.json.circe._
import sttp.tapir.model.{ ConnectionInfo, ServerRequest }
import zio.clock.Clock
Expand All @@ -17,7 +16,6 @@ import zio.test.Assertion._
import zio.test._
import zio.{ Queue, ZIO }

import scala.collection.immutable
import scala.language.postfixOps

object TapirAdapterSpec {
Expand Down

0 comments on commit b537c4c

Please sign in to comment.