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

Docs for cats interop #58

Merged
merged 1 commit into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = """
Expand All @@ -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
Expand Up @@ -33,6 +33,7 @@ module.exports = {
'schema',
'validation',
'introspection',
'interop',
'examples'
]
}
Expand Down
6 changes: 6 additions & 0 deletions vuepress/docs/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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.