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

Http4s simple integration #100

Merged
merged 4 commits into from
Dec 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions http4s/src/main/scala/caliban/Http4sAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.~>
Expand Down Expand Up @@ -80,21 +80,38 @@ 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._

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
}
}

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 <- executeToJson(interpreter, query).provideSome[R0](provideEnv)
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, *]]
Expand Down Expand Up @@ -195,6 +212,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] =
Expand All @@ -205,4 +231,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))
}