Skip to content

Commit

Permalink
[Codegen] Support for missing types (#209)
Browse files Browse the repository at this point in the history
* Support for union + other fixes

* Support descriptions

* Support scalar and schema

* Support input objects

* Fixes

* Fix test

* Upgrade ScalaFmt
  • Loading branch information
ghostdogpr authored Feb 9, 2020
1 parent eac7289 commit e5be965
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 126 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ lazy val codegen = project
sbtPlugin := true,
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")),
libraryDependencies ++= Seq(
"org.scalameta" %%% "scalafmt-dynamic" % "2.3.3-RC2",
"com.geirsson" %%% "scalafmt-core" % "1.5.1",
"org.scalameta" %%% "scalafmt-dynamic" % "2.3.2",
"org.scalameta" %%% "scalafmt-core" % "2.3.2",
"dev.zio" %%% "zio-test" % "1.0.0-RC17" % "test",
"dev.zio" %%% "zio-test-sbt" % "1.0.0-RC17" % "test"
)
Expand Down
35 changes: 16 additions & 19 deletions codegen/src/main/scala/caliban/codegen/CodegenPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import zio.{ DefaultRuntime, Task, UIO, ZIO }

object CodegenPlugin extends AutoPlugin {
import Console.Live.console._
override lazy val projectSettings = Seq(commands += codegenCommand)
lazy val codegenCommand =
Command.args("codegen", helpMsg) { (state: State, args: Seq[String]) =>
override lazy val projectSettings = Seq(commands += genSchemaCommand)
lazy val genSchemaCommand =
Command.args("calibanGenSchema", helpMsg) { (state: State, args: Seq[String]) =>
val runtime = new DefaultRuntime {}

runtime.unsafeRun(
Expand All @@ -28,12 +28,11 @@ object CodegenPlugin extends AutoPlugin {
}
private val helpMsg =
"""
|codegen schemaPath outPath ?scalafmtPath
|calibanGenSchema schemaPath outputPath ?scalafmtPath
|
|Command will write a scala file (outPath) containing GraphQL types,
|queries and subscriptions for provided json schema (schemaPath) and will
|format generated code with scalafmt with config in (scalafmtPath) or
|default config provided along with caliban-codegen.
|This command will create a Scala file in `outputPath` containing all the types
|defined in the provided GraphQL schema defined at `schemaPath`. The generated
|code will be formatted with Scalafmt using the configuration defined by `scalafmtPath`.
|
|""".stripMargin

Expand All @@ -47,16 +46,14 @@ object CodegenPlugin extends AutoPlugin {

def doSchemaGenerate(schemaPath: String, toPath: String, fmtPath: Option[String]): ZIO[Console, Throwable, Unit] =
for {
schema_string <- Task(scala.io.Source.fromFile(schemaPath))
.bracket(f => UIO(f.close()), f => Task(f.mkString))
schema <- Parser.parseQuery(schema_string)
code <- Task(Generator.generate(schema)(ScalaWriter.DefaultGQLWriter))
formatted <- fmtPath
.map(Generator.format(code, _))
.getOrElse(Generator.formatStr(code, ScalaWriter.scalafmtConfig))
_ <- Task(new PrintWriter(new File(toPath)))
.bracket(q => UIO(q.close()), { pw =>
Task(pw.println(formatted))
})
_ <- putStrLn(s"Generating schema for $schemaPath")
schema_string <- Task(scala.io.Source.fromFile(schemaPath)).bracket(f => UIO(f.close()), f => Task(f.mkString))
schema <- Parser.parseQuery(schema_string)
code <- Task(Generator.generate(schema)(ScalaWriter.DefaultGQLWriter))
formatted <- fmtPath.fold(Generator.formatStr(code, ScalaWriter.scalafmtConfig))(Generator.format(code, _))
_ <- Task(new PrintWriter(new File(toPath))).bracket(q => UIO(q.close()), { pw =>
Task(pw.println(formatted))
})
_ <- putStrLn(s"Schema generation done")
} yield ()
}
11 changes: 7 additions & 4 deletions codegen/src/main/scala/caliban/codegen/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package caliban.codegen

import java.net.{ URL, URLClassLoader }
import java.nio.file.Paths

import caliban.parsing.adt.Definition.TypeSystemDefinition.{ EnumTypeDefinition, ObjectTypeDefinition }
import caliban.parsing.adt.Type.FieldDefinition
import caliban.parsing.adt.Definition.TypeSystemDefinition.TypeDefinition._
import caliban.parsing.adt.{ Document, Type }
import org.scalafmt.dynamic.ScalafmtReflect
import org.scalafmt.dynamic.utils.ReentrantCache
Expand All @@ -26,7 +24,7 @@ object Generator {
val scalafmtReflect =
ScalafmtReflect(
new URLClassLoader(new Array[URL](0), this.getClass.getClassLoader),
"2.2.1",
"2.3.2",
respectVersion = false
)
val config = scalafmtReflect.parseConfigFromString(fmt)
Expand All @@ -40,9 +38,12 @@ object Generator {

trait GQLWriterContext {
implicit val fieldWriter: GQLWriter[FieldDefinition, ObjectTypeDefinition]
implicit val inputValueWriter: GQLWriter[InputValueDefinition, InputObjectTypeDefinition]
implicit val typeWriter: GQLWriter[Type, Any]
implicit val objectWriter: GQLWriter[ObjectTypeDefinition, Document]
implicit val inputObjectWriter: GQLWriter[InputObjectTypeDefinition, Document]
implicit val enumWriter: GQLWriter[EnumTypeDefinition, Document]
implicit val unionWriter: GQLWriter[Union, Document]
implicit val docWriter: GQLWriter[Document, Any]
implicit val rootQueryWriter: GQLWriter[RootQueryDef, Document]
implicit val queryWriter: GQLWriter[QueryDef, Document]
Expand All @@ -64,6 +65,8 @@ object Generator {

case class Args(field: FieldDefinition)

case class Union(typedef: UnionTypeDefinition, objects: List[ObjectTypeDefinition])

object GQLWriter {
def apply[A, D](implicit instance: GQLWriter[A, D]): GQLWriter[A, D] =
instance
Expand Down
Loading

0 comments on commit e5be965

Please sign in to comment.