GraphQL Codegen SBT plugin
This is a sbt plugin for https://github.com/kobylynskyi/graphql-java-codegen
Server example at https://github.com/jxnu-liguobin/springboot-examples/tree/master/graphql-complete (do not use plugin, only a normal graphql server )
Plugin Setup
// plugins.sbt
addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "<version>") // since graphql-java-codegen V2.2.1
Java code will be generated by default, via set
generatedLanguage =: GeneratedLanguage.SCALA
to generate Scala code (since 4.0.0). By the way, Scala code is currently ugly.
Config for generating scala code
Options example in build.sbt
enablePlugins(GraphQLCodegenPlugin)
GraphQLCodegenPluginDependencies
graphqlSchemaPaths := List("src/main/resources/schema.graphqls")
modelPackageName := Some("io.github.dreamylost.model")
apiPackageName := Some("io.github.dreamylost.api")
generateClient := true
generateApis := true
// Default value is com.kobylynskyi.graphql.codegen.model.GeneratedLanguage.JAVA to generate java codes
// Use Java code if your Scala code encounters errors. Configuration is the same. QAQ
generatedLanguage := com.kobylynskyi.graphql.codegen.model.GeneratedLanguage.SCALA
generateImmutableModels := true
modelNameSuffix := Some("DO")
customAnnotationsMapping := {
// in the future, maybe wrap it by scala coolection
val mapping = new util.HashMap[String, util.List[String]]
val annotations = new util.ArrayList[String]()
annotations.add("@com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = \"__typename\")")
annotations.add(
"""@com.fasterxml.jackson.annotation.JsonSubTypes(value = Array(
| new com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = classOf[HumanDO], name = "Human"),
| new com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = classOf[DroidDO], name = "Droid")))""".stripMargin)
mapping.put("Character", annotations)
// Note: only for Scala, please pay attention here, codegen have not generated `EpisodeDOTypeRefer.scala` class, so you should create it.
// Since, 4.1.3, support to generate it.
// Since, 5.1.0, It will be done automatically, no need to add this.
mapping.put("Droid.appearsIn", util.Arrays.asList("@com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[io.github.dreamylost.model.EpisodeDOTypeRefer])"))
mapping.put("Human.appearsIn", util.Arrays.asList("@com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[io.github.dreamylost.model.EpisodeDOTypeRefer])"))
mapping
}
generateCodegenTargetPath in GraphQLCodegenConfig := crossTarget.value / "src_managed_graphql_scala"
generateEqualsAndHashCode := true
generateToString := true
Codegen Options
SBT task
- graphqlSchemaValidate
- use validate at terminal by user, can get args from terminal, such
as
graphqlSchemaValidate src/main/resources/schema.graphqls
, args split with space
- use validate at terminal by user, can get args from terminal, such
as
- graphqlCodegen
- generate java code from graphql schema
- graphqlCodegenValidate
- use validate schemas that config in build.sbt key:
graphqlSchemaPaths
- use validate schemas that config in build.sbt key:
Plugin Options
Please refer to Codegen Options
in sbt plugin option
packageName
was rename togeneratePackageName
generateCodegenTargetPath
Where to store generated files and add the generated code to the classpath, so that they can be referenced. since 3.0.0 .