From a465f8b5046b8dcb2d1946cea912c14bcaa0a158 Mon Sep 17 00:00:00 2001 From: fokot Date: Fri, 29 Nov 2019 10:33:07 +0100 Subject: [PATCH 1/4] Http4s simple integration --- .../src/main/scala/caliban/Http4sAdapter.scala | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/http4s/src/main/scala/caliban/Http4sAdapter.scala b/http4s/src/main/scala/caliban/Http4sAdapter.scala index 23ed8b28c..e8e19c0ac 100644 --- a/http4s/src/main/scala/caliban/Http4sAdapter.scala +++ b/http4s/src/main/scala/caliban/Http4sAdapter.scala @@ -2,11 +2,11 @@ package caliban import caliban.ResponseValue._ import caliban.Value._ -import cats.data.OptionT +import cats.data.{Kleisli, OptionT} import cats.effect.Effect import cats.effect.syntax.all._ import cats.~> -import fs2.{ Pipe, Stream } +import fs2.{Pipe, Stream} import io.circe.derivation.deriveDecoder import io.circe._ import io.circe.parser._ @@ -95,6 +95,18 @@ object Http4sAdapter { } } + def executeRequest[R0, R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E], provideEnv: R0 => R): HttpApp[RIO[R0, *]] = Kleisli { + req => + object dsl extends Http4sDsl[RIO[R0, *]] + import dsl._ + for { + query <- req.attemptAs[GraphQLRequest].value.absolve + result <- execute(interpreter, query).provideSome[R0](provideEnv) + .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) + response <- Ok(result) + } yield response + } + def makeWebSocketService[R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E]): HttpRoutes[RIO[R, *]] = { object dsl extends Http4sDsl[RIO[R, *]] From daa38d535f9e562fca25d0d93b566ab3e4815544 Mon Sep 17 00:00:00 2001 From: fokot Date: Fri, 29 Nov 2019 11:33:32 +0100 Subject: [PATCH 2/4] Http4s simple integration scalafmt fix --- http4s/src/main/scala/caliban/Http4sAdapter.scala | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/http4s/src/main/scala/caliban/Http4sAdapter.scala b/http4s/src/main/scala/caliban/Http4sAdapter.scala index e8e19c0ac..ffcfbdc4a 100644 --- a/http4s/src/main/scala/caliban/Http4sAdapter.scala +++ b/http4s/src/main/scala/caliban/Http4sAdapter.scala @@ -2,11 +2,11 @@ package caliban import caliban.ResponseValue._ import caliban.Value._ -import cats.data.{Kleisli, OptionT} +import cats.data.{ Kleisli, OptionT } import cats.effect.Effect import cats.effect.syntax.all._ import cats.~> -import fs2.{Pipe, Stream} +import fs2.{ Pipe, Stream } import io.circe.derivation.deriveDecoder import io.circe._ import io.circe.parser._ @@ -95,17 +95,18 @@ object Http4sAdapter { } } - def executeRequest[R0, R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E], provideEnv: R0 => R): HttpApp[RIO[R0, *]] = Kleisli { - req => + def executeRequest[R0, R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E], provideEnv: R0 => R): HttpApp[RIO[R0, *]] = + Kleisli { req => object dsl extends Http4sDsl[RIO[R0, *]] import dsl._ for { query <- req.attemptAs[GraphQLRequest].value.absolve - result <- execute(interpreter, query).provideSome[R0](provideEnv) - .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) + result <- execute(interpreter, query) + .provideSome[R0](provideEnv) + .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) response <- Ok(result) } yield response - } + } def makeWebSocketService[R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E]): HttpRoutes[RIO[R, *]] = { From 27c71f4c9d6117bf5c8726db9edd4b68c0725ba0 Mon Sep 17 00:00:00 2001 From: fokot Date: Sun, 1 Dec 2019 23:44:10 +0100 Subject: [PATCH 3/4] added executeRequestF --- http4s/src/main/scala/caliban/Http4sAdapter.scala | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/http4s/src/main/scala/caliban/Http4sAdapter.scala b/http4s/src/main/scala/caliban/Http4sAdapter.scala index ffcfbdc4a..e2165dd75 100644 --- a/http4s/src/main/scala/caliban/Http4sAdapter.scala +++ b/http4s/src/main/scala/caliban/Http4sAdapter.scala @@ -208,6 +208,15 @@ object Http4sAdapter { .dimap((req: Request[F]) => req.mapK(toTask))((res: Response[Task]) => res.mapK(toF)) } + private def wrapApp[F[_]: Effect](app: HttpApp[Task])(implicit runtime: Runtime[Any]): HttpApp[F] = { + val toF: Task ~> F = λ[Task ~> F](_.toIO.to[F]) + val toTask: F ~> Task = λ[F ~> Task](_.toIO.to[Task]) + + app + .mapK(toF) + .dimap((req: Request[F]) => req.mapK(toTask))((res: Response[Task]) => res.mapK(toF)) + } + def makeWebSocketServiceF[F[_], Q, M, S, E]( interpreter: GraphQL[Any, Q, M, S, E] )(implicit F: Effect[F], runtime: Runtime[Any]): HttpRoutes[F] = @@ -218,4 +227,8 @@ object Http4sAdapter { )(implicit F: Effect[F], runtime: Runtime[Any]): HttpRoutes[F] = wrapRoute(makeRestService[Any, Q, M, S, E](interpreter)) + def executeRequestF[F[_], Q, M, S, E]( + interpreter: GraphQL[Any, Q, M, S, E] + )(implicit F: Effect[F], runtime: Runtime[Any]): HttpApp[F] = + wrapApp(executeRequest[Any, Any, Q, M, S, E](interpreter, identity)) } From ff5799ed92d0f467cfc1431214041a62340a5da0 Mon Sep 17 00:00:00 2001 From: fokot Date: Mon, 2 Dec 2019 10:54:54 +0100 Subject: [PATCH 4/4] moved code to executeToJson --- .../src/main/scala/caliban/Http4sAdapter.scala | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/http4s/src/main/scala/caliban/Http4sAdapter.scala b/http4s/src/main/scala/caliban/Http4sAdapter.scala index e2165dd75..368e5b4eb 100644 --- a/http4s/src/main/scala/caliban/Http4sAdapter.scala +++ b/http4s/src/main/scala/caliban/Http4sAdapter.scala @@ -80,6 +80,13 @@ object Http4sAdapter { ): URIO[R, GraphQLResponse[E]] = interpreter.execute(query.query, query.operationName, query.variables.map(jsonToVariables).getOrElse(Map())) + private def executeToJson[R, Q, M, S, E]( + interpreter: GraphQL[R, Q, M, S, E], + query: GraphQLRequest + ): URIO[R, Json] = + execute(interpreter, query) + .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) + def makeRestService[R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E]): HttpRoutes[RIO[R, *]] = { object dsl extends Http4sDsl[RIO[R, *]] import dsl._ @@ -87,9 +94,8 @@ object Http4sAdapter { HttpRoutes.of[RIO[R, *]] { case req @ POST -> Root => for { - query <- req.attemptAs[GraphQLRequest].value.absolve - result <- execute(interpreter, query) - .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) + query <- req.attemptAs[GraphQLRequest].value.absolve + result <- executeToJson(interpreter, query) response <- Ok(result) } yield response } @@ -100,10 +106,8 @@ object Http4sAdapter { object dsl extends Http4sDsl[RIO[R0, *]] import dsl._ for { - query <- req.attemptAs[GraphQLRequest].value.absolve - result <- execute(interpreter, query) - .provideSome[R0](provideEnv) - .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) + query <- req.attemptAs[GraphQLRequest].value.absolve + result <- executeToJson(interpreter, query).provideSome[R0](provideEnv) response <- Ok(result) } yield response }