Skip to content

Commit

Permalink
Merge pull request #61 from aPureBase/55-java-arrays
Browse files Browse the repository at this point in the history
Support for Java arrays
  • Loading branch information
jeggy authored Nov 4, 2019
2 parents 1724a8f + 4009974 commit 7f21eb7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.apurebase'
version '0.8.0'
version '0.8.1'

buildscript {

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/apurebase/kgraphql/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ fun KParameter.isNotNullable() = !type.isMarkedNullable

fun KClass<*>.isIterable() = isSubclassOf(Iterable::class)

fun KType.isIterable() = jvmErasure.isIterable()
fun KType.isIterable() = jvmErasure.isIterable() || toString().startsWith("kotlin.Array")

fun KType.getIterableElementType(): KType? {
require(jvmErasure.isIterable()) { "KType $this is not collection type" }
require(isIterable()) { "KType $this is not collection type" }
return arguments.firstOrNull()?.type ?: throw NoSuchElementException("KType $this has no type arguments")
}

fun not(boolean: Boolean) = !boolean
fun not(boolean: Boolean) = !boolean
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor, Coro
value == null -> createNullNode(node, returnType)

//check value, not returnType, because this method can be invoked with element value
value is Collection<*> -> {
value is Collection<*> || value is Array<*> -> {
val values: Collection<*> = when (value) {
is Array<*> -> value.toList()
else -> value as Collection<*>
}
if (returnType.isList()) {
val valuesMap = value.toMapAsync {
val valuesMap = values.toMapAsync {
createNode(ctx, it, node, returnType.unwrapList())
}
value.fold(jsonNodeFactory.arrayNode(value.size)) { array, v ->
values.fold(jsonNodeFactory.arrayNode(values.size)) { array, v ->
array.add(valuesMap[v])
}
} else {
Expand All @@ -166,8 +170,7 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor, Coro
}

private fun <T> createSimpleValueNode(returnType: Type, value: T): JsonNode {
val unwrapped = returnType.unwrapped()
return when (unwrapped) {
return when (val unwrapped = returnType.unwrapped()) {
is Type.Scalar<*> -> {
serializeScalar(jsonNodeFactory, unwrapped, value)
}
Expand Down Expand Up @@ -252,20 +255,14 @@ class ParallelRequestExecutor(val schema: DefaultSchema) : RequestExecutor, Coro
if (include) {
when (field) {
is Field.Kotlin<*, *> -> {
field.kProperty as KProperty1<T, *>
val rawValue = field.kProperty.get(parentValue)
val value: Any?
value = if (field.transformation != null) {
field.transformation.invoke(
funName = field.name,
receiver = rawValue,
inputValues = field.arguments,
args = node.arguments,
ctx = ctx
)
} else {
rawValue
}
val rawValue = (field.kProperty as KProperty1<T, *>).get(parentValue)
val value: Any? = field.transformation?.invoke(
funName = field.name,
receiver = rawValue,
inputValues = field.arguments,
args = node.arguments,
ctx = ctx
) ?: rawValue
return createNode(ctx, value, node, field.returnType)
}
is Field.Function<*, *> -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ class SchemaCompilation(
}

private fun handleCollectionType(kType: KType, typeCategory: TypeCategory): Type {
val type = kType.getIterableElementType()
?: throw SchemaException("Cannot handle collection without element type")
val type = when {
kType.getIterableElementType() != null -> kType.getIterableElementType()
kType.arguments.size == 1 -> kType.arguments.first().type
else -> null
} ?: throw throw SchemaException("Cannot handle collection without element type")

val nullableListType = Type.AList(handleSimpleType(type, typeCategory))
return applyNullability(kType, nullableListType)
Expand Down
18 changes: 16 additions & 2 deletions src/test/kotlin/com/apurebase/kgraphql/schema/SchemaBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,26 @@ class SchemaBuilderTest {
expect<SchemaException>("Generic types are not supported by GraphQL, found () -> kotlin.Int"){
KGraphQL.schema {
query("lambda"){
resolver { -> LambdaWrapper({ 1 }) }
resolver { -> LambdaWrapper { 1 } }
}
}
}
}

@Test
fun `java arrays should be supported`() {
KGraphQL.schema {
query("actors") {
resolver { ->
arrayOf(
Actor("Actor1", 1),
Actor("Actor2", 2)
)
}
}
}.execute("{actors { name } }").let(::println)
}

class InputOne(val string: String)

class InputTwo(val one : InputOne)
Expand Down Expand Up @@ -575,4 +589,4 @@ class SchemaBuilderTest {
assertThat(names, hasItem("TypeAsInput"))
assertThat(names, hasItem("TypeAsObject"))
}
}
}

0 comments on commit 7f21eb7

Please sign in to comment.