Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/polyvariant/smithy4s-caliban
Smithy4s integration for Caliban, a Scala GraphQL library.
https://github.com/polyvariant/smithy4s-caliban
caliban graphql graphql-server scala smithy smithy4s
Last synced: 27 days ago
JSON representation
Smithy4s integration for Caliban, a Scala GraphQL library.
- Host: GitHub
- URL: https://github.com/polyvariant/smithy4s-caliban
- Owner: polyvariant
- License: other
- Created: 2023-05-30T22:22:49.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-22T16:40:31.000Z (about 1 year ago)
- Last Synced: 2024-09-29T12:03:41.735Z (about 1 month ago)
- Topics: caliban, graphql, graphql-server, scala, smithy, smithy4s
- Language: Scala
- Homepage:
- Size: 75.2 KB
- Stars: 15
- Watchers: 5
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# smithy4s-caliban
[Smithy4s](https://disneystreaming.github.io/smithy4s/) integration for [Caliban](https://ghostdogpr.github.io/caliban/).
## Installation
In sbt:
```scala
libraryDependencies ++= Seq("org.polyvariant" %% "smithy4s-caliban" % version)
```In scala-cli:
```
//> using lib "org.polyvariant::smithy4s-caliban:version"
```## Usage
### Set up smithy4s
Follow the [quickstart steps](https://disneystreaming.github.io/smithy4s/docs/overview/quickstart).
### Write a Smithy spec
For example:
```smithy
$version: "2"namespace hello
service HelloService {
operations: [GetHello]
}@readonly
operation GetHello {
input := {
@required
name: String
}
output := {
@required
greeting: String
}
}```
Make sure you can generate Smithy4s code for that spec.
### Implement service
Implement the trait generated by Smithy4s:
```scala mdoc
import hello._
import cats.effect._val impl: HelloService[IO] = new HelloService[IO] {
override def getHello(name: String): IO[GetHelloOutput] =
IO.println("hello, " + name).as(GetHelloOutput("hello, " + name))
}
```### Interpret to GraphQL
This is what the library will allow you to do: convert that service implementation to a GraphQL root.
```scala mdoc
import org.polyvariant.smithy4scaliban._
import caliban.GraphQLval api: Resource[IO, GraphQL[Any]] = CalibanGraphQLInterpreter.server(impl)
```This returns a Resource because it requires (and creates) a `Dispatcher`.
Now, the last part - you have to connect this GraphQL instance to a server. How you do it is up to you, but [http4s](https://ghostdogpr.github.io/caliban/docs/adapters.html#json-handling) is recommended. Here's a full example (requires `caliban-http4s`, `tapir-json-circe` and `http4s-ember-server`):
```scala mdoc
import caliban.Http4sAdapter
import caliban.CalibanError
import caliban.interop.tapir.HttpInterpreter
import sttp.tapir.json.circe._
import caliban.interop.cats.implicits._
import org.http4s.ember.server.EmberServerBuilderval server: IO[Nothing] = api.evalMap { serverApi =>
implicit val rt: zio.Runtime[Any] = zio.Runtime.defaultserverApi
.interpreterAsync[IO]
.map { interp =>
Http4sAdapter.makeHttpServiceF[IO, Any, CalibanError](HttpInterpreter(interp))
}
}
.flatMap { routes =>
EmberServerBuilder.default[IO].withHttpApp(routes.orNotFound).build
}.useForever
```This will launch a server on `localhost:8080` running your Smithy spec as a GraphQL API.