Skip to content

Commit 17ff69e

Browse files
gscheibelmgilbey
authored andcommitted
Remove the dependency on object instances so the schema can be generated as a standalone without datafetchers (#198)
1 parent 5e4337a commit 17ff69e

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/main/kotlin/com/expedia/graphql/TopLevelObject.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ import kotlin.reflect.KClass
99
* @param obj The target object (or proxy to target object)
1010
* @param kClass Optional KClass of the target (or the proxied object)
1111
*/
12-
data class TopLevelObject(val obj: Any, val kClass: KClass<*> = obj::class)
12+
data class TopLevelObject(val obj: Any?, val kClass: KClass<*>) {
13+
constructor(obj: Any) : this(obj, obj::class)
14+
constructor(kClass: KClass<*>) : this(null, kClass)
15+
}

src/main/kotlin/com/expedia/graphql/generator/SubTypeMapper.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ internal class SubTypeMapper(supportedPackages: List<String>) {
77

88
private val reflections = Reflections(supportedPackages)
99

10-
fun getSubTypesOf(kclass: KClass<*>): List<Class<*>> =
11-
reflections.getSubTypesOf(Class.forName(kclass.javaObjectType.name))
12-
.filterNot { it.kotlin.isAbstract }
10+
fun getSubTypesOf(kclass: KClass<*>): List<Class<*>> = reflections.getSubTypesOf(kclass.java).filterNot { it.kotlin.isAbstract }
1311
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package com.expedia.graphql
22

33
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
45

56
class ToSchemaKtTest {
67

7-
class TestClass
8+
@Suppress("FunctionOnlyReturningConstant")
9+
class TestClass {
10+
fun greeting() = "Hello World"
11+
}
812

913
@Test
1014
fun `valid schema`() {
1115
val queries = listOf(TopLevelObject(TestClass()))
1216
toSchema(queries = queries, config = testSchemaConfig)
1317
}
18+
19+
@Test
20+
fun `generate standalone SDL`() {
21+
val queries = listOf(TopLevelObject(TestClass::class))
22+
val schema = toSchema(queries = queries, config = testSchemaConfig)
23+
24+
val greetingField = schema.queryType.getFieldDefinition("greeting")
25+
assertEquals("String!", greetingField.type.toString())
26+
}
1427
}

0 commit comments

Comments
 (0)