Skip to content
Closed
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
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id "signing"
id "maven-publish"
id "org.sonarqube" version "3.2.0"
id 'org.jetbrains.kotlin.jvm' version '1.8.20-Beta'
Copy link
Owner

Choose a reason for hiding this comment

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

@jxnu-liguobin why are we adding this?

Copy link
Owner

Choose a reason for hiding this comment

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

@jxnu-liguobin I would like not to add kotlin code and dependencies to the main project. instead, please add this as part of example kotlin plugin which we already have.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. We can update the sample synchronously when major changes occur.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The template changes in this PR have been verified, closed it

}

def graphqlCodegenVersion = '5.5.1-SNAPSHOT' // This variable used in the automatic release process
Expand All @@ -29,6 +30,10 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.1"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.1"
testImplementation "org.hamcrest:java-hamcrest:2.0.0.0"
testImplementation "org.scala-lang:scala-library:2.13.10"
testImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation "com.fasterxml.jackson.module:jackson-module-scala_2.13:2.13.3"

}

test.useJUnitPlatform()
Expand Down Expand Up @@ -156,3 +161,13 @@ if (project.hasProperty("signing.keyId")) {
sign publishing.publications.mavenJava
}
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ data class ${className}(
) : GraphQLParametrizedInput {

override fun deepCopy(): ${className} {
val parametrizedInput = ${className}()
<#if fields?has_content>
return ${className}(
<#list fields as field>
parametrizedInput.${field.name}(this.${field.name})
this.${field.name}<#if field_has_next>,</#if>
</#list>
)
<#else>
return ${className}()
</#if>
return parametrizedInput

}

override fun toString(): String {
Expand Down
10 changes: 6 additions & 4 deletions src/main/resources/templates/scala-lang/parametrized_input.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ case class ${className}(
</#if>
) extends GraphQLParametrizedInput {

override def deepCopy(): ${className} {
val parametrizedInput = ${className}()
override def deepCopy(): ${className} = {
<#if fields?has_content>
${className}(
<#list fields as field>
parametrizedInput.${field.name}(this.${field.name})
this.${field.name}<#if field_has_next>,</#if>
</#list>
)
<#else>
${className}()
</#if>
parametrizedInput
}

override def toString(): String = {<#--There is no Option[Seq[T]], Format is not supported in the generated code, so it is very difficult to write template for this format.-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ${className}() extends GraphQLResponseProjection() {
def this(projection: ${className}) = {
this()
if (projection != null) {
for (field <- projection.fields.values) {
for (field <- projection.fields.values.asScala) {
add$(field)
}
}
Expand All @@ -44,7 +44,7 @@ class ${className}() extends GraphQLResponseProjection() {
if (projections != null) {
for (projection <- projections) {
if (projection != null) {
for (field <- projection.fields.values) {
for (field <- projection.fields.values.asScala) {
add$(field)
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ class ${className}() extends GraphQLResponseProjection() {
</#if>
</#list>
</#if>
override def deepCopy$(): ${className} = ${className}(this)
override def deepCopy$(): ${className} = new ${className}(this)

<#if equalsAndHashCode>
override def equals(obj: Any): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void init() {
mappingConfig.setGenerateEqualsAndHashCode(true);
}

@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
}
// @AfterEach
Copy link
Owner

Choose a reason for hiding this comment

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

@jxnu-liguobin can you please revert this

// void cleanup() {
// Utils.deleteDir(outputBuildDir);
// }

@Test
void generate_MultipleInterfacesPerType() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.kobylynskyi.graphql.codegen.kotlin;

import com.kobylynskyi.graphql.codegen.TestUtils;
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

import static java.util.Collections.singletonList;

class GraphQLCodegenVerifyCompileTest {

private final String path = "src/test/java/com/kobylynskyi/graphql/codegen/kotlin/compile-pass";
private final File outputBuildDir = new File(path);

private MappingConfig mappingConfig;

@BeforeEach
void init() {
mappingConfig = new MappingConfig();
mappingConfig.setGenerateParameterizedFieldsResolvers(true);
mappingConfig.setPackageName("com.github.graphql");
mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN);
mappingConfig.setGenerateToString(true);
mappingConfig.setGenerateApis(true);
mappingConfig.setGenerateClient(true);
mappingConfig.setAddGeneratedAnnotation(false);
mappingConfig.setGenerateEqualsAndHashCode(true);
mappingConfig.setModelValidationAnnotation("");
}

@Test
void generate_CheckFiles_Kotlin_Files_Compile_Pass() throws Exception {
mappingConfig.setPackageName("");
generate("src/test/resources/schemas/test.graphqls");

File outputJavaClassesDir = new File(path);
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assert files.length > 0;
}

private void generate(String path) throws IOException {
new KotlinGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
import java.util.Objects

/**
* Create a new event.
*/
open class CreateEventMutationRequest(private val alias: String?) : GraphQLOperationRequest {

companion object {
const val OPERATION_NAME: String = "createEvent"
val OPERATION_TYPE: GraphQLOperation = GraphQLOperation.MUTATION
}

private val input: MutableMap<String, Any?> = LinkedHashMap()
private val useObjectMapperForInputSerialization: MutableSet<String> = HashSet()

constructor(): this(null)

fun setCategoryId(categoryId: String) {
this.input["categoryId"] = categoryId
}

fun setCreatedBy(createdBy: String?) {
this.input["createdBy"] = createdBy
}

override fun getOperationType(): GraphQLOperation = OPERATION_TYPE

override fun getOperationName(): String = OPERATION_NAME

override fun getAlias(): String? = alias ?: OPERATION_NAME

override fun getInput(): MutableMap<String, Any?> = input

override fun getUseObjectMapperForInputSerialization(): MutableSet<String> = useObjectMapperForInputSerialization

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (other == null || javaClass != other.javaClass) {
return false
}
val that = other as CreateEventMutationRequest
return Objects.equals(operationType, that.operationType) &&
Objects.equals(operationName, that.operationName) &&
Objects.equals(input, that.input)
}

override fun hashCode(): Int = Objects.hash(operationType, operationName, input)

override fun toString(): String = Objects.toString(input)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/**
* Create a new event.
*/
interface CreateEventMutationResolver {

/**
* Create a new event.
*/
@Throws(Exception::class)
fun createEvent(categoryId: String, createdBy: String?): Event

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResult

/**
* Create a new event.
*/
open class CreateEventMutationResponse : GraphQLResult<MutableMap<String, Event>>() {

companion object {
const val OPERATION_NAME: String = "createEvent"
}

/**
* Create a new event.
*/
fun createEvent(): Event {
val data: MutableMap<String, Event> = super.getData()
return data.getValue(OPERATION_NAME)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer
import java.util.StringJoiner

/**
* An event that describes a thing that happens
*/
data class Event(
val id: String?,
val categoryId: String?,
val properties: List<EventProperty?>?,
val status: EventStatus?,
val createdBy: String?,
val createdDateTime: String?,
val active: Boolean?,
val rating: Int?
) {

// In the future, it maybe change.
override fun toString(): String {
val joiner = StringJoiner(", ", "{ ", " }")
if (id != null) {
joiner.add("id: " + GraphQLRequestSerializer.getEntry(id))
}
if (categoryId != null) {
joiner.add("categoryId: " + GraphQLRequestSerializer.getEntry(categoryId))
}
if (properties != null) {
joiner.add("properties: " + GraphQLRequestSerializer.getEntry(properties))
}
if (status != null) {
joiner.add("status: " + GraphQLRequestSerializer.getEntry(status))
}
if (createdBy != null) {
joiner.add("createdBy: " + GraphQLRequestSerializer.getEntry(createdBy))
}
if (createdDateTime != null) {
joiner.add("createdDateTime: " + GraphQLRequestSerializer.getEntry(createdDateTime))
}
if (active != null) {
joiner.add("active: " + GraphQLRequestSerializer.getEntry(active))
}
if (rating != null) {
joiner.add("rating: " + GraphQLRequestSerializer.getEntry(rating))
}
return joiner.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
import java.util.Objects

/**
* Single event by ID.
*/
open class EventByIdQueryRequest(private val alias: String?) : GraphQLOperationRequest {

companion object {
const val OPERATION_NAME: String = "eventById"
val OPERATION_TYPE: GraphQLOperation = GraphQLOperation.QUERY
}

private val input: MutableMap<String, Any?> = LinkedHashMap()
private val useObjectMapperForInputSerialization: MutableSet<String> = HashSet()

constructor(): this(null)

fun setId(id: String) {
this.input["id"] = id
}

override fun getOperationType(): GraphQLOperation = OPERATION_TYPE

override fun getOperationName(): String = OPERATION_NAME

override fun getAlias(): String? = alias ?: OPERATION_NAME

override fun getInput(): MutableMap<String, Any?> = input

override fun getUseObjectMapperForInputSerialization(): MutableSet<String> = useObjectMapperForInputSerialization

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (other == null || javaClass != other.javaClass) {
return false
}
val that = other as EventByIdQueryRequest
return Objects.equals(operationType, that.operationType) &&
Objects.equals(operationName, that.operationName) &&
Objects.equals(input, that.input)
}

override fun hashCode(): Int = Objects.hash(operationType, operationName, input)

override fun toString(): String = Objects.toString(input)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/**
* Single event by ID.
*/
interface EventByIdQueryResolver {

/**
* Single event by ID.
*/
@Throws(Exception::class)
fun eventById(id: String): Event

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResult

/**
* Single event by ID.
*/
open class EventByIdQueryResponse : GraphQLResult<MutableMap<String, Event>>() {

companion object {
const val OPERATION_NAME: String = "eventById"
}

/**
* Single event by ID.
*/
fun eventById(): Event {
val data: MutableMap<String, Event> = super.getData()
return data.getValue(OPERATION_NAME)
}
}
Loading