Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 11 additions & 76 deletions plugins/sbt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,89 +15,25 @@ Server example at https://github.com/jxnu-liguobin/springboot-examples/tree/mast

```scala
// plugins.sbt
addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "<version>")

//since graphql-java-codegen V2.2.1
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.
> 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
### Config for generating scala code

Options example in ```build.sbt```

```scala
// build.sbt


enablePlugins(GraphQLCodegenPlugin)


GraphQLCodegenPluginDependencies

//default graphqlJavaCodegen is release
graphqlJavaCodegenVersion := Some("4.1.1")
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
}
```

### Config(generate scala code)


```scala
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.1")
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
// 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")
Expand All @@ -111,9 +47,10 @@ customAnnotationsMapping := {
| 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])"))
// Note: only for Scala, please pay attention here, codegen have not generated `EpisodeDOTypeRefer.scala` class, so you should create it.
// Since, 4.1.2, support to generate it.
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"
Expand Down Expand Up @@ -141,6 +78,4 @@ Please refer to [Codegen Options](../../docs/codegen-options.md)

> in sbt plugin option
- `packageName` was rename to `generatePackageName`
- `generateCodegenTargetPath` can be used for setting codegen path where code have bean created by task `graphqlCodegen`, since 3.0.0 .


- `generateCodegenTargetPath` can be used for setting codegen path where code have bean created by task `graphqlCodegen`, since 3.0.0 .
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ libraryDependencies ++= Seq(
"org.json" % "json" % "20190722")

enablePlugins(GraphQLCodegenPlugin)
graphqlJavaCodegenVersion := Some("4.1.2-SNAPSHOT")
graphqlJavaCodegenVersion := Some((version in Scope.ThisScope).value)
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\")")
Expand All @@ -36,9 +34,8 @@ customAnnotationsMapping := {
| 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.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"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version in ThisBuild := "4.1.2-SNAPSHOT"
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ libraryDependencies ++= Seq(

enablePlugins(GraphQLCodegenPlugin)

graphqlJavaCodegenVersion := Some("4.1.2-SNAPSHOT")
graphqlJavaCodegenVersion := Some((version in Scope.ThisScope).value)

GraphQLCodegenPluginDependencies

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version in ThisBuild := "4.1.2-SNAPSHOT"
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name := "simple"

lazy val root = (project in file("."))
.settings(
version := "0.1",
scalaVersion := "2.13.2",
apiPackageName := Some("io.github.kobylynskyi.graphql.test.api"),
modelPackageName := Some("io.github.kobylynskyi.graphql.test.model"),
apiReturnType := Some("scala.concurrent.Future"), // if Async class is not at current source, need import dependency
//use full class name is good
graphqlJavaCodegenVersion := Some((version in Scope.ThisScope).value)
//use full class name is good
).enablePlugins(GraphQLCodegenPlugin).settings(GraphQLCodegenPluginDependencies)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version in ThisBuild := "4.1.2-SNAPSHOT"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kobylynskyi.graphql.codegen.model.DeprecatedDefinition;
import com.kobylynskyi.graphql.codegen.model.EnumValueDefinition;
import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedEnumTypeDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedUnionTypeDefinition;
Expand All @@ -16,13 +17,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ANNOTATIONS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.CLASS_NAME;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.FIELDS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMPLEMENTS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.PACKAGE;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.*;

/**
* Map enum definition to a Freemarker data model
Expand Down Expand Up @@ -83,6 +78,7 @@ public Map<String, Object> map(MappingContext mappingContext, ExtendedEnumTypeDe
dataModel.put(JAVA_DOC, definition.getJavaDoc());
dataModel.put(FIELDS, map(mappingContext, definition.getValueDefinitions()));
dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation());
dataModel.put(SERIALIZATION_LIBRARY, MappingConfigConstants.DEFAULT_SERIALIZATION_LIBRARY);
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class DataModelFields {
public static final String RESPONSE_PROJECTION_MAX_DEPTH = "responseProjectionMaxDepth";
public static final String ENUM_IMPORT_IT_SELF_IN_SCALA = "enumImportItSelfInScala";
public static final String PARENT_INTERFACE_PROPERTIES = "parentInterfaceProperties";
public static final String SERIALIZATION_LIBRARY = "serializationLibrary";

private DataModelFields() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class MappingConfigConstants {
public static final GeneratedLanguage DEFAULT_GENERATED_LANGUAGE = GeneratedLanguage.JAVA;
public static final String DEFAULT_GENERATED_LANGUAGE_STRING = "JAVA";

// This library is the only one that supports Scala well and is generally used.
// There is no need to consider the possibility of switching.
public static final String DEFAULT_SERIALIZATION_LIBRARY = "JACKSON";

private MappingConfigConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<#if package?has_content>
package ${package}

</#if>
<#if serializationLibrary == 'JACKSON'>
import com.fasterxml.jackson.core.`type`.TypeReference

</#if>
<#if javaDoc?has_content>
/**
Expand Down Expand Up @@ -38,4 +42,8 @@ object ${className} extends Enumeration<#if implements?has_content> with<#list i
</#list>
</#if>

}
}
<#if serializationLibrary == 'JACKSON'>

class ${className}TypeRefer extends TypeReference[${className}.type]
</#if>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.graphql

import com.fasterxml.jackson.core.`type`.TypeReference
Copy link
Owner

@kobylynskyi kobylynskyi Jan 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also need to do the “if JACKSON” check here (as well as all other similar file templates)?

Copy link
Collaborator Author

@jxnu-liguobin jxnu-liguobin Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Only enumeration is needed
  • Only when Jackson is used


@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
Expand All @@ -12,4 +14,6 @@ object Status extends Enumeration {
val CREATED: Value = Value("CREATED")
val IN_PROGRESS: Value = Value("IN_PROGRESS")

}
}

class StatusTypeRefer extends TypeReference[Status.type]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.fasterxml.jackson.core.`type`.TypeReference

@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
Expand All @@ -7,4 +9,6 @@ object Status extends Enumeration {
type Status = Value


}
}

class StatusTypeRefer extends TypeReference[Status.type]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.fasterxml.jackson.core.`type`.TypeReference

@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
Expand All @@ -10,4 +12,6 @@ object Status extends Enumeration {
val IN_PROGRESS: Value = Value("IN_PROGRESS")
val ASSIGNED: Value = Value("ASSIGNED")

}
}

class StatusTypeRefer extends TypeReference[Status.type]