Skip to content

Commit

Permalink
Updating documentation to reflect new sbt plugin infra
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Jun 21, 2021
1 parent 8d787bb commit bb67c33
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
39 changes: 38 additions & 1 deletion vuepress/docs/docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,50 @@ And enable it in your `build.sbt` file:
enablePlugins(CodegenPlugin)
```

Then call the `calibanGenClient` sbt command.
At this point, the `caliban` command will cause any files in `src/main/graphql` to be translated into a Caliban-generated client library. This happens automatically any time you `compile`.

By default, all clients are generated with the same client name as the source file, in the `caliban` top-level package.

In order to supply more configuration options to the code generator, you can use the `calibanSettings` sbt setting, combined with the `calibanSetting` function to scope the settings to a particular file:
```scala
// The `file("Service.graphql")` is a path suffix for some file in `src/main/graphql`
Compile / caliban / calibanSettings += calibanSetting(file("Service.graphql"))(
cs =>
cs.packageName("com.example.graphql.client")
.scalarMapping(
"LanguageCode" -> "com.example.models.LanguageCode",
)
.scalarMapping(
"Timestamp" -> "java.sql.Timestamp",
"DayOfWeek" -> "java.time.DayOfWeek",
"IntRange" -> "com.github.tminglei.slickpg.Range[Int]"
)
.imports("com.example.graphql.client.implicits._")
)
```

The settings available on the `cs` (`CalibanSettings`) builder are:
```
def scalafmtPath(path: String): CalibanSettings
def headers(pairs: (String,String)*): CalibanSettings
def packageName(name: String): CalibanSettings
def genView(value: Boolean): CalibanSettings
def effect(tpe: String): CalibanSettings
def scalarMapping(mapping: (String,String)*): CalibanSettings
def imports(values: String*): CalibanSettings
```

### `calibanGenClient`

Should you prefer or otherwise need the previous style of manual client codegen, supplied by the `calibanGenClient` function, that command is still available. Documentation for that command is as follows:

```scala
calibanGenClient schemaPath outputPath [--scalafmtPath path] [--headers name:value,name2:value2] [--genView true|false] [--scalarMappings gqlType:f.q.d.n.Type,gqlType2:f.q.d.n.Type2] [--imports a.b.c._,c.d.E]

calibanGenClient project/schema.graphql src/main/client/Client.scala --genView true
```
This command will generate a Scala file in `outputPath` containing helper functions for all the types defined in the provided GraphQL schema defined at `schemaPath`.
If you need to disable generating clients from `src/main/graphql`, please include `Compile / caliban / calibanGenerator := Seq.empty` in your project settings.
Instead of a file, you can provide a URL and the schema will be obtained using introspection.
The generated code will be formatted with Scalafmt using the configuration defined by `--scalafmtPath` option (default: `.scalafmt.conf`).
If you provide a URL for `schemaPath`, you can provide request headers with `--headers` option.
Expand Down
41 changes: 29 additions & 12 deletions vuepress/docs/docs/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,39 @@ And enable it in your `build.sbt` file:
enablePlugins(CodegenPlugin)
```

Then call the `calibanGenSchema` sbt command.
```scala
calibanGenSchema schemaPath outputPath [--scalafmtPath path] [--headers name:value,name2:value2] [--packageName name] [--effect fqdn.Effect] [--scalarMappings gqlType:f.q.d.n.Type,gqlType2:f.q.d.n.Type2] [--imports a.b.c._,c.d.E]

calibanGenSchema project/schema.graphql src/main/MyAPI.scala
```
This command will create a Scala file in `outputPath` containing all the types defined in the provided GraphQL schema defined at `schemaPath`. Instead of a file, you can provide a URL and the schema will be obtained using introspection.
At this point, the `caliban` command will cause any files in `src/main/graphql` to be translated into a Caliban-generated client library. This happens automatically any time you `compile`.

The generated code will be formatted with Scalafmt using the configuration defined by `--scalafmtPath` option (default: `.scalafmt.conf`). If you provide a URL for `schemaPath`, you can provide request headers with `--headers` option.
By default, all clients are generated with the same client name as the source file, in the `caliban` top-level package.

The package of the generated code is derived from the folder of `outputPath`. This can be overridden by providing an alternative package with the `--packageName` option.
In order to supply more configuration options to the code generator, you can use the `calibanSettings` sbt setting, combined with the `calibanSetting` function to scope the settings to a particular file:
```scala
// The `file("Service.graphql")` is a path suffix for some file in `src/main/graphql`
Compile / caliban / calibanSettings += calibanSetting(file("Service.graphql"))(
cs =>
cs.packageName("com.example.graphql.client")
.scalarMapping(
"LanguageCode" -> "com.example.models.LanguageCode",
)
.scalarMapping(
"Timestamp" -> "java.sql.Timestamp",
"DayOfWeek" -> "java.time.DayOfWeek",
"IntRange" -> "com.github.tminglei.slickpg.Range[Int]"
)
.imports("com.example.graphql.client.implicits._")
)
```

By default, each Query and Mutation will be wrapped into a `zio.UIO` effect. This can be overridden by providing an alternative effect with the `--effect` option.
The settings available on the `cs` (`CalibanSettings`) builder are:
```
def scalafmtPath(path: String): CalibanSettings
def headers(pairs: (String,String)*): CalibanSettings
def packageName(name: String): CalibanSettings
def genView(value: Boolean): CalibanSettings
def effect(tpe: String): CalibanSettings
def scalarMapping(mapping: (String,String)*): CalibanSettings
def imports(values: String*): CalibanSettings
```

If you want to force a mapping between a GraphQL type and a Scala class (such as scalars), you can use the
`--scalarMappings` option. Also you can add additional imports by providing `--imports` option.

## Building Schemas by hand

Expand Down

0 comments on commit bb67c33

Please sign in to comment.