Skip to content

Commit

Permalink
chore: update zio-http to RC23 & tapir 0.20.0-M7
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-anymind committed Jan 28, 2022
1 parent bbde4aa commit 2d1a5ab
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 56 deletions.
27 changes: 14 additions & 13 deletions adapters/zio-http/src/main/scala/caliban/ZHttpAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,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 @@ -64,7 +65,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 @@ -120,7 +121,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
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ object ZHttpAdapterSpec extends DefaultRunnableSpec {
_ <- Server
.start(
8088,
Http.route {
case _ -> Root / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
case _ -> Root / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
Http.route[Request] {
case _ -> !! / "api" / "graphql" => ZHttpAdapter.makeHttpService(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
6 changes: 3 additions & 3 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.0"
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.4"
val circeVersion = "0.14.1"
Expand All @@ -17,14 +17,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.3"
val tapirVersion = "0.20.0-M7"
val zioVersion = "1.0.13"
val zioInteropCats2Version = "2.5.1.0"
val zioInteropCats3Version = "3.2.9.0"
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-RC23"

inThisBuild(
List(
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
25 changes: 9 additions & 16 deletions examples/src/main/scala/example/ziohttp/AuthExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import caliban.interop.tapir.{ StreamTransformer, WebSocketHooks }
import caliban.schema.GenericSchema
import example.ExampleData._
import example.{ ExampleApi, ExampleService }
import io.netty.handler.codec.http.{ HttpHeaderNames, HttpHeaderValues }
import zhttp.http._
import zhttp.service.Server
import zio._
Expand Down Expand Up @@ -42,7 +41,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 @@ -84,11 +83,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.getHeaders.getAuthorization.map(_.toString())

ZIO.serviceWith[Auth](_.setUser(user)).as(app)
}
Expand All @@ -108,24 +107,18 @@ 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" => Auth.middleware(ZHttpAdapter.makeHttpService(interpreter))
case _ -> Root / "ws" / "graphql" => Auth.WebSockets.live(interpreter)
case _ -> Root / "graphiql" => graphiql
Http.route[Request] {
case _ -> !! / "api" / "graphql" => Auth.middleware(ZHttpAdapter.makeHttpService(interpreter))
case _ -> !! / "ws" / "graphql" => Auth.WebSockets.live(interpreter)
case _ -> !! / "graphiql" => graphiql
}
)
.forever
Expand Down
17 changes: 5 additions & 12 deletions examples/src/main/scala/example/ziohttp/ExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@ import example.ExampleData._
import example.{ ExampleApi, ExampleService }

import caliban.ZHttpAdapter
import io.netty.handler.codec.http.{HttpHeaderNames, HttpHeaderValues}
import zio._
import zio.stream._
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 @@ -124,7 +124,7 @@ object TapirAdapter {
requestCodec: JsonCodec[GraphQLRequest],
mapCodec: JsonCodec[Map[String, Seq[String]]],
responseCodec: JsonCodec[GraphQLResponse[E]]
): PublicEndpoint[(Seq[Part[Array[Byte]]], ServerRequest), StatusCode, GraphQLResponse[E], Any] =
): PublicEndpoint[UploadRequest, StatusCode, GraphQLResponse[E], Any] =
endpoint.post
.in(multipartBody)
.in(extractFromRequest(identity))
Expand Down

0 comments on commit 2d1a5ab

Please sign in to comment.