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 )
// plugins.sbt
addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "<version>")
//since graphql-java-codegen V2.2.1Java code will be generated by default, via set
generatedLanguage =: GeneratedLanguage.SCALAto generate Scala code(since 4.0.0) By the way, Scala code is currently ugly.
// build.sbt
enablePlugins(GraphQLCodegenPlugin)
GraphQLCodegenPluginDependencies
//default graphqlJavaCodegen is release
graphqlJavaCodegenVersion := Some("4.1.0")
graphqlSchemaPaths := List("src/main/resources/schema.graphqls")
modelPackageName := Some("io.github.dreamylost.model")
apiPackageName := Some("io.github.dreamylost.api")
generateClient := false
generateApis := true
// Scala collection cannot be used. The latter one uses the put method, which is not supported by Scala collection.
// in FB, collection is immutable
customTypesMapping := {
val mapping = new util.HashMap[String, String]
mapping.put("Email", "io.github.dreamylost.scalar.EmailScalar")
//Character will conflict with java.lang.Character. maybe because Scala imports it automatically java.lang *.
//So we use Full class name
mapping
}
//Of course, you can also add a suffix to be different from it
modelNameSuffix := Some("DO")
customAnnotationsMapping := {
val mapping = new util.HashMap[String, String]
//must add this annotation
//property is __typename and you must with __typename while invoke, like new CharacterResponseProjection().id().name().typename()
//and in @JsonSubTypes.Type, name is __typename's value
mapping.put("Character",
s"""@com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = "__typename")${System.lineSeparator()}@com.fasterxml.jackson.annotation.JsonSubTypes(value = {
| @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = HumanDO.class, name = "Human"),
| @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidDO.class, name = "Droid")})
|""".stripMargin)
mapping
}import java.util
name := "example-client-scala"
organization := "io.github.jxnu-liguobin"
libraryDependencies ++= Seq(
"org.apache.logging.log4j" %% "log4j-api-scala" % "11.0",
"org.apache.logging.log4j" % "log4j-api" % "2.8.2",
"org.apache.logging.log4j" % "log4j-core" % "2.8.2",
"org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.8.2",
"com.squareup.okhttp3" % "okhttp" % "4.7.2",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.11.3",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.11.3",
"org.json" % "json" % "20190722")
enablePlugins(GraphQLCodegenPlugin)
graphqlJavaCodegenVersion := Some("4.1.0")
GraphQLCodegenPluginDependencies
graphqlSchemaPaths := List("src/main/resources/schema.graphqls")
modelPackageName := Some("io.github.dreamylost.model")
apiPackageName := Some("io.github.dreamylost.api")
generateClient := true
generateApis := true
//TODO wrap it in plugin maybe better? after wrapping, them import automatically
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)
// please pay attention here, codegen have not generated `EpisodeDOEnum.scala` class, so you should create it.
mapping.put("Droid.appearsIn", util.Arrays.asList("@com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[io.github.dreamylost.EpisodeDOEnum])"))
mapping.put("Human.appearsIn", util.Arrays.asList("@com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[io.github.dreamylost.EpisodeDOEnum])"))
mapping
}
generateCodegenTargetPath in GraphQLCodegenConfig := crossTarget.value / "src_managed_graphql_scala"
generateEqualsAndHashCode := true
generateToString := trueSBT 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:
Please refer to Codegen Options
in sbt plugin option
packageNamewas rename togeneratePackageNamegenerateCodegenTargetPathcan be used for setting codegen path where code have bean created by taskgraphqlCodegen, since 3.0.0 .