Skip to content

Commit

Permalink
Docs for cats interop (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr authored Oct 27, 2019
1 parent e3c2b9d commit 71c1062
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ package caliban.interop.cats

import caliban.GraphQL.graphQL
import caliban.RootResolver
import cats.effect.{ExitCode, IO, IOApp}
import cats.effect.{ ExitCode, IO, IOApp }
import zio.DefaultRuntime

object ExampleCatsInterop extends IOApp {
@@ -15,10 +15,10 @@ object ExampleCatsInterop extends IOApp {

case class Queries(numbers: List[Number], randomNumber: IO[Number])

val numbers = List(1, 2, 3, 4).map(Number)
val numbers = List(1, 2, 3, 4).map(Number)
val randomNumber = IO(scala.util.Random.nextInt()).map(Number)

val queries = Queries(numbers, randomNumber)
val queries = Queries(numbers, randomNumber)
val interpreter = graphQL(RootResolver(queries))

val query = """
@@ -35,6 +35,6 @@ object ExampleCatsInterop extends IOApp {
override def run(args: List[String]): IO[ExitCode] =
for {
result <- interpreter.executeAsync[IO](query)
_ <- IO(println(result))
_ <- IO(println(result))
} yield ExitCode.Success
}
1 change: 1 addition & 0 deletions vuepress/docs/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ module.exports = {
'schema',
'validation',
'introspection',
'interop',
'examples'
]
}
6 changes: 6 additions & 0 deletions vuepress/docs/docs/README.md
Original file line number Diff line number Diff line change
@@ -15,6 +15,12 @@ To use `caliban`, add the following line in your `build.sbt` file:
libraryDependencies += "com.github.ghostdogpr" %% "caliban" % "0.1.0"
```

The following modules are optional:
```
libraryDependencies += "com.github.ghostdogpr" %% "caliban-http4s" % "0.1.0" // routes for http4s
libraryDependencies += "com.github.ghostdogpr" %% "caliban-cats" % "TODO" // interop with cats effect
```

Note that Caliban is also available for ScalaJS.

## A simple example
42 changes: 42 additions & 0 deletions vuepress/docs/docs/interop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Interop (Cats)

If you prefer using [Monix](https://github.com/monix/monix) or [Cats IO](https://github.com/typelevel/cats-effect) rather than ZIO, you can use the `caliban-cats` module.

You first need to import `caliban.interop.cats.implicits._` and have an implicit `zio.Runtime` in scope. Then a few helpers are available:
- the GraphQL object is enriched with `executeAsync` and `checkAsync`, variants of `execute` and `check` that return an `F[_]: Async` instead of a `ZIO`.
- the `Http4sAdapter` also has cats-effect variants named `makeRestServiceF` and `makeWebSocketServiceF`.

In addition to that, a `Schema` for any `F[_]: Effect` is provided. That means you can include fields returning Monix Task for Cats IO in your queries, mutations or subscriptions.

The following example shows how to create an interpreter and run a query while only using Cats IO.
```scala
import caliban.GraphQL.graphQL
import caliban.RootResolver
import caliban.interop.cats.implicits._
import cats.effect.{ ExitCode, IO, IOApp }
import zio.DefaultRuntime

object ExampleCatsInterop extends IOApp {

implicit val runtime = new DefaultRuntime {}

case class Queries(numbers: List[Int], randomNumber: IO[Int])

val queries = Queries(List(1, 2, 3, 4), IO(scala.util.Random.nextInt()))
val interpreter = graphQL(RootResolver(queries))

val query = """
{
numbers
randomNumber
}"""

override def run(args: List[String]): IO[ExitCode] =
for {
result <- interpreter.executeAsync[IO](query)
_ <- IO(println(result))
} yield ExitCode.Success
}
```

You can find this example within the [examples](https://github.com/ghostdogpr/caliban/blob/master/examples/src/main/scala/caliban/interop/cats/ExampleCatsInterop.scala) project.

0 comments on commit 71c1062

Please sign in to comment.