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

Conversation

fokot
Copy link
Contributor

@fokot fokot commented Nov 29, 2019

This allows for simple integrations where we can do things like:

HttpRoutes
            .of[RIO[ZEnv, *]] {
              case req @ POST -> Root / "api" / "graphql" => {
                val token: Task[Token] =
                  UIO(req.headers.get(Authorization).map(_.value).toRight("No `Authorization` header").flatMap(auth.decodeTokenPayload)).absolve
                    .mapError(AuthException)
                Http4sAdapter.executeRequest(
                  graphql.schema.interpreter,
                  (env: ZEnv) =>
                    new Console with Clock with Auth with Storage {
                      override val console: Console.Service[Any] = env.console
                      override val clock: Clock.Service[Any] = env.clock
                      override def auth: Auth.Service = AuthServiceFromToken(token)
                      override def storage: StorageBase[Task] = _storage
                    }
                )(req)
              }
              case GET -> Root / "graphiql" => staticResource("static/graphiql.html")
              case GET -> Root / "login.html" => staticResource("static/login.html")
            }
            .orNotFound

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?

@ghostdogpr
Copy link
Owner

Can you also add a variant executeRequestF for cats-effect users? Similar to makeRestServiceF.

@fokot
Copy link
Contributor Author

fokot commented Dec 1, 2019

Sure, I added executeRequestF

@ghostdogpr ghostdogpr merged commit 25d4b96 into ghostdogpr:master Dec 2, 2019
@ghostdogpr
Copy link
Owner

Thanks!

@fokot fokot deleted the http4s-simple-integration branch December 3, 2019 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants