Skip to content

Commit

Permalink
Merge pull request #2 from polyvariant/bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored May 31, 2023
2 parents 7181241 + 441d305 commit cffd49f
Show file tree
Hide file tree
Showing 15 changed files with 1,737 additions and 53 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ jobs:
if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-latest'
run: sbt '++ ${{ matrix.scala }}' doc

- run: sbt '++ ${{ matrix.scala }}' docs/mdoc

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p core/target/native-3 target core/target/jvm-3 core/target/js-2.13 core/target/jvm-2.13 core/target/js-3 project/target
run: mkdir -p target modules/core/target/jvm-2.13 modules/core/target/jvm-3 modules/docs/target/jvm-2.13 project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar core/target/native-3 target core/target/jvm-3 core/target/js-2.13 core/target/jvm-2.13 core/target/js-3 project/target
run: tar cf targets.tar target modules/core/target/jvm-2.13 modules/core/target/jvm-3 modules/docs/target/jvm-2.13 project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
95 changes: 93 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,101 @@

## Installation

In sbt:

```scala
TODO
libraryDependencies ++= Seq("org.polyvariant" %% "smithy4s-caliban" % version)
```

In scala-cli:

```
//> using lib "org.polyvariant::smithy4s-caliban:version"
```

## Usage

TODO
### 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]
}
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.GraphQL

val 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.EmberServerBuilder

val server: IO[Nothing] = api.evalMap { serverApi =>
implicit val rt: zio.Runtime[Any] = zio.Runtime.default

serverApi
.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.
37 changes: 35 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,52 @@ val commonSettings = Seq(
)

lazy val core = projectMatrix
.in(file("modules/core"))
.settings(
name := "smithy4s-caliban",
commonSettings,
libraryDependencies ++= Seq(
"com.github.ghostdogpr" %%% "caliban-cats" % "2.2.1",
"com.disneystreaming.smithy4s" %%% "smithy4s-core" % smithy4s.codegen.BuildInfo.version,
"com.disneystreaming" %%% "weaver-cats" % "0.8.3" % Test,
"io.circe" %% "circe-core" % "0.14.5" % Test,
),
testFrameworks += new TestFramework("weaver.framework.CatsEffect"),
Smithy4sCodegenPlugin.defaultSettings(Test),
scalacOptions --= {
if (scalaVersion.value.startsWith("3"))
Seq("-Ykind-projector:underscores")
else
Seq()
},
scalacOptions ++= {
if (scalaVersion.value.startsWith("3"))
Seq("-Ykind-projector")
else
Seq()
},
)
.jvmPlatform(Seq(Scala213, Scala3))
.jsPlatform(Seq(Scala213, Scala3))
.nativePlatform(Seq(Scala3))

lazy val docs = projectMatrix
.in(file("modules/docs"))
.settings(
mdocIn := new File("README.md"),
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-ember-server" % "0.23.19",
"com.github.ghostdogpr" %%% "caliban-http4s" % "2.2.1",
"com.softwaremill.sttp.tapir" %%% "tapir-json-circe" % "1.5.0",
),
ThisBuild / githubWorkflowBuild +=
WorkflowStep.Sbt(
List("docs/mdoc")
),
)
.dependsOn(core)
.jvmPlatform(Seq(Scala213))
.enablePlugins(MdocPlugin, Smithy4sCodegenPlugin)

lazy val root = project
.in(file("."))
.aggregate(core.componentProjects.map(p => p: ProjectReference): _*)
.enablePlugins(NoPublishPlugin)
45 changes: 0 additions & 45 deletions core/src/test/scala/MainTest.scala

This file was deleted.

Loading

0 comments on commit cffd49f

Please sign in to comment.