Skip to content

Commit 68c89da

Browse files
authored
Merge pull request #24 from aPureBase/pgutkowski-pull-63
Allow a kotlin type to be used as graphql type and inputType
2 parents 11a5922 + 7ebba34 commit 68c89da

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

src/main/kotlin/com/apurebase/kgraphql/request/Variables.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ data class Variables(
1818
/**
1919
* map and return object of requested class
2020
*/
21-
fun <T : Any> get(kClass: KClass<T>, kType: KType, key: String, transform: (value: String) -> Any?): T? {
21+
fun <T : Any> get(kClass: KClass<T>, kType: KType, typeName: String?, key: String, transform: (value: String) -> Any?): T? {
2222
val variable = variables?.find { key == it.name }
2323
?: throw IllegalArgumentException("Variable '$key' was not declared for this operation")
2424

2525
val isIterable = kClass.isIterable()
2626

27-
validateVariable(typeDefinitionProvider.typeReference(kType), variable)
27+
validateVariable(typeDefinitionProvider.typeReference(kType), typeName, variable)
2828

2929
var value = variablesJson.get(kClass, kType, key.substring(1))
3030
if(value == null && variable.defaultValue != null){
@@ -57,9 +57,9 @@ data class Variables(
5757
}
5858
}
5959

60-
fun validateVariable(expectedType: TypeReference, variable: OperationVariable){
60+
fun validateVariable(expectedType: TypeReference, expectedTypeName: String?, variable: OperationVariable){
6161
val variableType = variable.type
62-
val invalidName = expectedType.name != variableType.name
62+
val invalidName = (expectedTypeName ?: expectedType.name) != variableType.name
6363
val invalidIsList = expectedType.isList != variableType.isList
6464
val invalidNullability = !expectedType.isNullable && variableType.isNullable && variable.defaultValue == null
6565
val invalidElementNullability = !expectedType.isElementNullable && variableType.isElementNullable

src/main/kotlin/com/apurebase/kgraphql/schema/execution/ArgumentTransformer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ open class ArgumentTransformer(val schema : DefaultSchema) {
1616

1717
fun transformValue(type: Type, value: String, variables: Variables) : Any? {
1818
val kType = type.toKType()
19+
val typeName = type.unwrapped().name
1920

2021
return when {
2122
value.startsWith("$") -> {
2223
variables.get (
23-
kType.jvmErasure, kType, value, { subValue -> transformValue(type, subValue, variables) }
24+
kType.jvmErasure, kType, typeName, value, { subValue -> transformValue(type, subValue, variables) }
2425
)
2526
}
2627
value == "null" && type.isNullable() -> null

src/main/kotlin/com/apurebase/kgraphql/schema/structure2/SchemaCompilation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class SchemaCompilation(
7272

7373
queryTypes = queryTypeProxies + enums + scalars,
7474
inputTypes = inputTypeProxies + enums + scalars,
75-
allTypes = queryTypeProxies + inputTypeProxies + enums + scalars,
75+
allTypes = queryTypeProxies.values + inputTypeProxies.values + enums.values + scalars.values,
7676
directives = definition.directives.map { handlePartialDirective(it) }
7777
)
7878
val schema = DefaultSchema(configuration, model)

src/main/kotlin/com/apurebase/kgraphql/schema/structure2/SchemaModel.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ data class SchemaModel (
1414
val enums: Map<KClass<out Enum<*>>, Type.Enum<out Enum<*>>>,
1515
val scalars : Map<KClass<*>, Type.Scalar<*>>,
1616
val unions : List<Type.Union>,
17-
val allTypes : Map<KClass<*>, Type>,
17+
val allTypes : List<Type>,
1818
val queryTypes: Map<KClass<*>, Type>,
1919
val inputTypes: Map<KClass<*>, Type>,
2020
override val directives: List<Directive>
2121
) : __Schema {
2222

23-
val allTypesByName = allTypes.values.associate { it.name to it }
23+
val allTypesByName = allTypes.associate { it.name to it }
2424

2525
val queryTypesByName = queryTypes.values.associate { it.name to it }
2626

2727
val inputTypesByName = inputTypes.values.associate { it.name to it }
2828

29-
override val types: List<__Type> = allTypes.values.toList()
29+
override val types: List<__Type> = allTypes.toList()
3030
//workaround on the fact that Double and Float are treated as GraphQL Float
3131
.filterNot { it is Type.Scalar<*> && it.kClass == Float::class }
3232
.filterNot { it.kClass?.findAnnotation<NotIntrospected>() != null }

src/test/kotlin/com/apurebase/kgraphql/integration/BaseSchemaTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ abstract class BaseSchemaTest {
245245
}}
246246
}
247247
}
248+
249+
inputType<Actor>() {
250+
name = "ActorInput"
251+
}
252+
253+
mutation("createActorWithAliasedInputType") {
254+
description = "create new actor from full fledged ActorInput as input type"
255+
resolver { newActor: Actor ->
256+
createdActors.add(newActor)
257+
newActor
258+
}
259+
}
248260
}
249261

250262
@After

src/test/kotlin/com/apurebase/kgraphql/integration/MutationTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ class MutationTest : BaseSchemaTest() {
6969
assertNoErrors(map)
7070
assertThat(map.extract<Map<String, Any>>("data/createActor"), equalTo(mapOf<String, Any>("howOld" to testActor.age)))
7171
}
72+
73+
@Test
74+
fun `simple mutation with aliased input type`(){
75+
val map = execute("mutation(\$newActor: ActorInput!) { createActorWithAliasedInputType(newActor: \$newActor) {name}}",
76+
variables = "{\"newActor\": {\"name\": \"${testActor.name}\", \"age\": ${testActor.age}}}")
77+
assertNoErrors(map)
78+
assertThat(map.extract<Map<String, Any>>("data/createActorWithAliasedInputType"), equalTo(mapOf<String, Any>("name" to testActor.name)))
79+
}
7280
}

src/test/kotlin/com/apurebase/kgraphql/schema/SchemaBuilderTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.hamcrest.CoreMatchers.equalTo
2121
import org.hamcrest.CoreMatchers.instanceOf
2222
import org.hamcrest.CoreMatchers.notNullValue
2323
import org.hamcrest.CoreMatchers.nullValue
24+
import org.hamcrest.CoreMatchers.hasItem
2425
import org.hamcrest.MatcherAssert
2526
import org.hamcrest.MatcherAssert.assertThat
2627
import org.junit.Test
@@ -546,4 +547,25 @@ class SchemaBuilderTest {
546547
}
547548
}
548549
}
550+
551+
@Test
552+
fun `Schema can have same type and input type with different names`(){
553+
val schema = defaultSchema {
554+
inputType<InputOne>() {
555+
name="TypeAsInput"
556+
}
557+
type<InputOne>() {
558+
name="TypeAsObject"
559+
}
560+
}
561+
562+
assertThat(schema.typeByKClass(InputOne::class), notNullValue())
563+
assertThat(schema.inputTypeByKClass(InputOne::class), notNullValue())
564+
565+
val introspection = deserialize(schema.execute("{__schema{types{name}}}"))
566+
val types = introspection.extract<List<Map<String,String>>>("data/__schema/types")
567+
val names = types.map {it["name"]}
568+
assertThat(names, hasItem("TypeAsInput"))
569+
assertThat(names, hasItem("TypeAsObject"))
570+
}
549571
}

0 commit comments

Comments
 (0)