forked from ghostdogpr/caliban
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers and example for using Http4sAdapter with F[_] (ghostdogpr…
- Loading branch information
1 parent
c4e0345
commit 0d0bc2b
Showing
2 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package example.http4s | ||
|
||
import caliban.interop.cats.implicits._ | ||
import caliban.{ CalibanError, Http4sAdapter } | ||
import cats.data.Kleisli | ||
import cats.effect.std.Dispatcher | ||
import cats.effect.{ ExitCode, IO, IOApp } | ||
import example.ExampleData.sampleCharacters | ||
import example.ExampleService.ExampleService | ||
import example.{ ExampleApi, ExampleService } | ||
import org.http4s.StaticFile | ||
import org.http4s.blaze.server.BlazeServerBuilder | ||
import org.http4s.implicits._ | ||
import org.http4s.server.Router | ||
import org.http4s.server.middleware.CORS | ||
import zio.Runtime | ||
import zio.clock.Clock | ||
import zio.console.Console | ||
import zio.internal.Platform | ||
|
||
object ExampleAppF extends IOApp { | ||
|
||
type MyEnv = Console with Clock with ExampleService | ||
|
||
implicit val zioRuntime: Runtime[MyEnv] = | ||
Runtime.unsafeFromLayer(ExampleService.make(sampleCharacters) ++ Console.live ++ Clock.live, Platform.default) | ||
|
||
override def run(args: List[String]): IO[ExitCode] = | ||
Dispatcher[IO].use { implicit dispatcher => | ||
for { | ||
interpreter <- ExampleApi.api.interpreterAsync[IO] | ||
_ <- BlazeServerBuilder[IO] | ||
.bindHttp(8088, "localhost") | ||
.withHttpWebSocketApp(wsBuilder => | ||
Router[IO]( | ||
"/api/graphql" -> | ||
CORS.policy(Http4sAdapter.makeHttpServiceF[IO, MyEnv, CalibanError](interpreter)), | ||
"/ws/graphql" -> | ||
CORS.policy(Http4sAdapter.makeWebSocketServiceF[IO, MyEnv, CalibanError](wsBuilder, interpreter)), | ||
"/graphiql" -> | ||
Kleisli.liftF(StaticFile.fromResource("/graphiql.html", None)) | ||
).orNotFound | ||
) | ||
.serve | ||
.compile | ||
.drain | ||
} yield ExitCode.Success | ||
} | ||
} |