Skip to content

Commit

Permalink
Add abstractEffectType as a cmdline option
Browse files Browse the repository at this point in the history
Add --abstractEffectType as a command line option and add tests to check
it's parsed correctly.
  • Loading branch information
LaurenceWarne committed Jul 6, 2021
1 parent ed2cfa5 commit 3f28858
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
5 changes: 4 additions & 1 deletion codegen-sbt/src/main/scala/caliban/codegen/CalibanCli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object CalibanCli {

private val genSchemaHelpMsg =
s"""
|calibanGenSchema schemaPath outputPath [--scalafmtPath path] [--headers name:value,name2:value2] [--packageName name] [--effect fqdn.Effect]
|calibanGenSchema schemaPath outputPath [--scalafmtPath path] [--headers name:value,name2:value2] [--packageName name] [--effect fqdn.Effect] [--abstractEffectType]
|
|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 path,
Expand All @@ -61,6 +61,9 @@ object CalibanCli {
|
|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 --abstractEffectType flag can also be used to indicate that the effect
|type is abstract, so that it will be added as a type parameter to the generated
|Query and Mutation classes (if applicable).
|""".stripMargin

private val genClientHelpMsg =
Expand Down
19 changes: 11 additions & 8 deletions tools/src/main/scala/caliban/tools/Codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ object Codegen {
arguments: Options,
genType: GenType
): Task[Unit] = {
val s = ".*/scala[^/]*/(.*)/(.*).scala".r.findFirstMatchIn(arguments.toPath)
val packageName = arguments.packageName.orElse(s.map(_.group(1).split("/").mkString(".")))
val objectName = s.map(_.group(2)).getOrElse("Client")
val effect = arguments.effect.getOrElse("zio.UIO")
val genView = arguments.genView.getOrElse(false)
val scalarMappings = arguments.scalarMappings
val loader = getSchemaLoader(arguments.schemaPath, arguments.headers)
val s = ".*/scala[^/]*/(.*)/(.*).scala".r.findFirstMatchIn(arguments.toPath)
val packageName = arguments.packageName.orElse(s.map(_.group(1).split("/").mkString(".")))
val objectName = s.map(_.group(2)).getOrElse("Client")
val effect = arguments.effect.getOrElse("zio.UIO")
val genView = arguments.genView.getOrElse(false)
val scalarMappings = arguments.scalarMappings
val loader = getSchemaLoader(arguments.schemaPath, arguments.headers)
val abstractEffectType = arguments.abstractEffectType.getOrElse(false)
for {
schema <- loader.load
code = genType match {
case GenType.Schema =>
SchemaWriter.write(schema, packageName, effect, arguments.imports)(ScalarMappings(scalarMappings))
SchemaWriter.write(schema, packageName, effect, arguments.imports, abstractEffectType)(
ScalarMappings(scalarMappings)
)
case GenType.Client =>
ClientWriter.write(schema, objectName, packageName, genView, arguments.imports)(
ScalarMappings(scalarMappings)
Expand Down
9 changes: 6 additions & 3 deletions tools/src/main/scala/caliban/tools/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ final case class Options(
genView: Option[Boolean],
effect: Option[String],
scalarMappings: Option[Map[String, String]],
imports: Option[List[String]]
imports: Option[List[String]],
abstractEffectType: Option[Boolean]
)

object Options {
Expand All @@ -24,7 +25,8 @@ object Options {
genView: Option[Boolean],
effect: Option[String],
scalarMappings: Option[List[String]],
imports: Option[List[String]]
imports: Option[List[String]],
abstractEffectType: Option[Boolean]
)

def fromArgs(args: List[String]): Option[Options] =
Expand Down Expand Up @@ -62,7 +64,8 @@ object Options {
}
}.toMap
},
rawOpts.imports
rawOpts.imports,
rawOpts.abstractEffectType
)
}
case _ => None
Expand Down
34 changes: 32 additions & 2 deletions tools/src/test/scala/caliban/tools/OptionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
None,
None,
None,
None
)
)
Expand All @@ -44,6 +45,7 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
None,
None,
None,
None
)
)
Expand All @@ -56,7 +58,7 @@ object OptionsSpec extends DefaultRunnableSpec {
assert(result)(
equalTo(
Some(
Options("schema", "output", None, None, None, None, None, None, None)
Options("schema", "output", None, None, None, None, None, None, None, None)
)
)
)
Expand Down Expand Up @@ -90,6 +92,7 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
None,
None,
None,
None
)
)
Expand All @@ -111,6 +114,7 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
Some("cats.effect.IO"),
None,
None,
None
)
)
Expand All @@ -132,6 +136,7 @@ object OptionsSpec extends DefaultRunnableSpec {
Some(true),
None,
None,
None,
None
)
)
Expand All @@ -153,6 +158,7 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
None,
Some(Map("Long" -> "scala.Long")),
None,
None
)
)
Expand All @@ -174,7 +180,30 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
None,
None,
Some(List("a.b.Clazz", "b.c._"))
Some(List("a.b.Clazz", "b.c._")),
None
)
)
)
)
},
test("provide abstractEffectType") {
val input = List("schema", "output", "--effect", "F", "--abstractEffectType", "true")
val result = Options.fromArgs(input)
assert(result)(
equalTo(
Some(
Options(
"schema",
"output",
None,
None,
None,
None,
Some("F"),
None,
None,
Some(true)
)
)
)
Expand All @@ -195,6 +224,7 @@ object OptionsSpec extends DefaultRunnableSpec {
None,
None,
None,
None,
None
)
)
Expand Down

0 comments on commit 3f28858

Please sign in to comment.