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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion 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 @@ -95,6 +95,19 @@ 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
Copy link
Owner

Choose a reason for hiding this comment

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

Can you extract this block into a function so that it's not repeated here and in makeRestService?

query <- req.attemptAs[GraphQLRequest].value.absolve
result <- execute(interpreter, query)
                   .foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looks like good idea but in executeRequest I work with Request/Response with environment R0 and executor is in environment R, so it would mean doing two mapK which I do not like much and I find it now more simple. Or I can to common method provideSome and send indentity from makeRestService which also not nice. What do you think?

Copy link
Owner

Choose a reason for hiding this comment

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

Oh I see what you mean. I'd prefer to avoid an unnecessary provideSome for general case. How about just moving execute(interpreter, query).foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) to a function?

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, *]]
Expand Down