Skip to content

Commit 06de9cd

Browse files
authored
Avoid array of Kotlin primitive types to be mapped as array of java primitives (#33)
1 parent 41c7a48 commit 06de9cd

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/main/kotlin/com/expedia/graphql/schema/generator/SchemaGenerator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import kotlin.reflect.full.declaredMemberProperties
4141
import kotlin.reflect.full.isSubclassOf
4242
import kotlin.reflect.full.superclasses
4343
import kotlin.reflect.full.valueParameters
44+
import kotlin.reflect.jvm.javaType
4445
import kotlin.reflect.jvm.jvmErasure
4546

4647
@Suppress("Detekt.UnsafeCast")
@@ -138,7 +139,7 @@ internal class SchemaGenerator(
138139
throw IllegalArgumentException("argument name is null or blank, $it")
139140
} else {
140141
// Kotlin 1.3 will support contracts, until then we need to force non-null
141-
args[name!!] = Parameter(it.type.jvmErasure.java, it.annotations)
142+
args[name!!] = Parameter(it.type.javaType as Class<*>, it.annotations)
142143
}
143144
}
144145

src/test/kotlin/com/expedia/graphql/schema/generator/SchemaGeneratorTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ class SchemaGeneratorTest {
3232
assertEquals(1, geo?.get("query")?.get("id"))
3333
}
3434

35+
@Test
36+
fun `Schema generator exposes arrays of primitive types as function arguments`() {
37+
val schema = toSchema(listOf(TopLevelObjectDef(QueryWithArray())), config = testSchemaConfig)
38+
val firstArgumentType = schema.queryType.getFieldDefinition("sumOf").arguments[0].type.deepName
39+
assertEquals("[Int!]!", firstArgumentType)
40+
41+
val graphQL = GraphQL.newGraphQL(schema).build()
42+
val result = graphQL.execute("{ sumOf(ints: [1, 2, 3]) }")
43+
val sum = result.getData<Map<String, Int>>().values.first()
44+
45+
assertEquals(6, sum)
46+
}
47+
48+
@Test
49+
fun `Schema generator exposes arrays of complex types as function arguments`() {
50+
val schema = toSchema(listOf(TopLevelObjectDef(QueryWithArray())), config = testSchemaConfig)
51+
val firstArgumentType = schema.queryType.getFieldDefinition("sumOfComplexArray").arguments[0].type.deepName
52+
assertEquals("[ComplexWrappingTypeInput!]!", firstArgumentType)
53+
54+
val graphQL = GraphQL.newGraphQL(schema).build()
55+
val result = graphQL.execute("{sumOfComplexArray(objects: [{value: 1}, {value: 2}, {value: 3}])}")
56+
val sum = result.getData<Map<String, Int>>().values.first()
57+
58+
assertEquals(6, sum)
59+
}
60+
3561
@Test
3662
fun `SchemaGenerator ignores fields and functions with @Ignore`() {
3763
val schema = toSchema(listOf(TopLevelObjectDef(QueryWithIgnored())), config = testSchemaConfig)
@@ -179,6 +205,11 @@ class SchemaGeneratorTest {
179205
fun query(@GraphQLDescription("A GraphQL value") value: Int): Geography = Geography(value, GeoType.CITY, listOf())
180206
}
181207

208+
class QueryWithArray {
209+
fun sumOf(ints: Array<Int>): Int = ints.sum()
210+
fun sumOfComplexArray(objects: Array<ComplexWrappingType>): Int = objects.map { it.value }.sum()
211+
}
212+
182213
class QueryWithIgnored {
183214
fun query(): ResultWithIgnored? = null
184215

@@ -200,6 +231,8 @@ class SchemaGeneratorTest {
200231
fun mutation(value: Int): Boolean = value > 0
201232
}
202233

234+
data class ComplexWrappingType(val value: Int)
235+
203236
@GraphQLDescription("A place")
204237
data class Geography(
205238
val id: Int?,

0 commit comments

Comments
 (0)