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
2 changes: 1 addition & 1 deletion src/main/resources/templates/kotlin-lang/request.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ open class ${className}(private val alias: String?) : GraphQLOperationRequest {
<#else>
<#if MapperUtil.isKotlinPrimitive(field.type)>
<#assign default = MapperUtil.defaultValueKotlinPrimitive(field.type)/>
private var ${field.name}: ${field.type} = default
private var ${field.name}: ${field.type} = ${default}
<#else>
private lateinit var ${field.name}: ${field.type}
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;


class GraphQLCodegenGitHubTest {

private final File outputBuildDir = new File("build/generated");
Expand Down Expand Up @@ -187,4 +188,16 @@ void generate_CustomFieldsResolvers() throws Exception {
getFileByName(files, "AcceptTopicSuggestionPayloadResolver.kt"));
}

@Test
void generate_RequestWithDefaultValue() throws Exception {
mappingConfig.setGenerateBuilder(true);
mappingConfig.setGenerateClient(true);
new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/kt/default.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputktClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/default/" +
"FriendsQueryRequest.kt.txt"),
getFileByName(files, "FriendsQueryRequest.kt"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.graphql

import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
import java.util.Objects

@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
open class FriendsQueryRequest(private val alias: String?) : GraphQLOperationRequest {

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

@JvmStatic fun builder(): Builder = Builder()
}

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

constructor(): this(null)

fun setNum(num: Int) {
this.input["num"] = num
}

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 FriendsQueryRequest
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)

class Builder {

private var `$alias`: String? = null
private var num: Int = 0

fun alias(alias: String?): Builder {
this.`$alias` = alias
return this
}

fun setNum(num: Int): Builder {
this.num = num
return this
}

fun build(): FriendsQueryRequest {
val obj = FriendsQueryRequest(`$alias`)
obj.setNum(num)
return obj
}

}
}
7 changes: 7 additions & 0 deletions src/test/resources/schemas/kt/default.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Query {
friends(num: Int!): [Friend]
}

type Friend {
name: String
}