diff --git a/examples/README.md b/examples/README.md index 0b87ed098a..bdf5702691 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,3 +9,5 @@ libraryDependencies ++= Seq( "com.github.ghostdogpr" %% "caliban-cats" % "0.3.0" ) ``` + +Run `ExampleApp` and open [http://localhost:8088/graphiql](http://localhost:8088/graphiql) diff --git a/examples/src/main/resources/graphiql.html b/examples/src/main/resources/graphiql.html new file mode 100644 index 0000000000..7e35f08cad --- /dev/null +++ b/examples/src/main/resources/graphiql.html @@ -0,0 +1,156 @@ + + + + + + + + + + + + + + + + + + + +
Loading...
+ + + + diff --git a/examples/src/main/scala/caliban/ExampleApp.scala b/examples/src/main/scala/caliban/ExampleApp.scala index fd0a58f589..ece6382ad3 100644 --- a/examples/src/main/scala/caliban/ExampleApp.scala +++ b/examples/src/main/scala/caliban/ExampleApp.scala @@ -2,15 +2,19 @@ package caliban import caliban.ExampleData._ import caliban.GraphQL._ -import caliban.schema.Annotations.{ GQLDeprecated, GQLDescription } +import caliban.schema.Annotations.{GQLDeprecated, GQLDescription} import caliban.schema.GenericSchema +import cats.data.Kleisli +import cats.effect.Blocker +import org.http4s.StaticFile import org.http4s.implicits._ import org.http4s.server.Router import org.http4s.server.blaze.BlazeServerBuilder import org.http4s.server.middleware.CORS import zio._ +import zio.blocking.Blocking import zio.clock.Clock -import zio.console.{ putStrLn, Console } +import zio.console.{Console, putStrLn} import zio.interop.catz._ import zio.stream.ZStream @@ -27,9 +31,9 @@ object ExampleApp extends CatsApp with GenericSchema[Console with Clock] { type ExampleTask[A] = RIO[Console with Clock, A] - implicit val roleSchema = gen[Role] - implicit val characterSchema = gen[Character] - implicit val characterArgsSchema = gen[CharacterArgs] + implicit val roleSchema = gen[Role] + implicit val characterSchema = gen[Character] + implicit val characterArgsSchema = gen[CharacterArgs] implicit val charactersArgsSchema = gen[CharactersArgs] override def run(args: List[String]): ZIO[ZEnv, Nothing, Int] = @@ -45,16 +49,23 @@ object ExampleApp extends CatsApp with GenericSchema[Console with Clock] { Subscriptions(service.deletedEvents) ) ) + blocker <- ZIO + .accessM[Blocking](_.blocking.blockingExecutor.map(_.asEC)) + .map(Blocker.liftExecutionContext) _ <- BlazeServerBuilder[ExampleTask] - .bindHttp(8088, "localhost") - .withHttpApp( - Router( - "/api/graphql" -> CORS(Http4sAdapter.makeRestService(interpreter)), - "/ws/graphql" -> CORS(Http4sAdapter.makeWebSocketService(interpreter)) - ).orNotFound - ) - .resource - .toManaged - .useForever + .bindHttp(8088, "localhost") + .withHttpApp( + Router[ExampleTask]( + "/api/graphql" -> CORS(Http4sAdapter.makeRestService(interpreter)), + "/ws/graphql" -> CORS( + Http4sAdapter.makeWebSocketService(interpreter) + ), + "/graphiql" -> Kleisli + .liftF(StaticFile.fromResource("/graphiql.html", blocker, None)) + ).orNotFound + ) + .resource + .toManaged + .useForever } yield 0).catchAll(err => putStrLn(err.toString).as(1)) }