Skip to content

Commit 83959cc

Browse files
committed
Access to configure possibleTypes via TypeDSL for sealed union types
1 parent 600b88f commit 83959cc

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# KGraphQL version
2-
version=0.17.12
2+
version=0.17.13
33

44
# Dependencies
55
coroutine_version=1.5.0

kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/SchemaBuilder.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ class SchemaBuilder internal constructor() {
195195
if (!T::class.isSealed) throw SchemaException("Can't generate a union type out of a non sealed class. '${T::class.simpleName}'")
196196

197197
return unionType(T::class.simpleName!!) {
198-
T::class.sealedSubclasses.forEach { type(it) }
199198
block()
199+
T::class.sealedSubclasses.forEach {
200+
type(it, subTypeBlock) // <-- Adds to schema definition
201+
type(it) // <-- Adds to possible union type
202+
}
200203
}
201204
}
202205

kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/dsl/types/UnionTypeDSL.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ class UnionTypeDSL() : ItemDSL() {
88

99
internal val possibleTypes = mutableSetOf<KClass<*>>()
1010

11+
var subTypeBlock: TypeDSL<*>.() -> Unit = {}
12+
1113
fun <T : Any>type(kClass : KClass<T>){
1214
possibleTypes.add(kClass)
1315
}
1416

1517
inline fun <reified T : Any>type(){
1618
type(T::class)
1719
}
18-
}
20+
}

kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/model/MutableSchemaDefinition.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ data class MutableSchemaDefinition (
6161

6262
unions.forEach { union ->
6363
if(union.members.isEmpty()){
64-
throw SchemaException("A Union type must define one or more unique member types")
64+
throw SchemaException("The union type '${union.name}' has no possible types defined, requires at least one. Please refer to https://kgraphql.io/Reference/Type%20System/unions/")
6565
}
6666
union.members.forEach { member ->
6767
validateUnionMember(union, member, compiledObjects)

kgraphql/src/test/kotlin/com/apurebase/kgraphql/specification/typesystem/UnionsSpecificationTest.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.apurebase.kgraphql.schema.SchemaException
66
import com.apurebase.kgraphql.GraphQLError
77
import com.apurebase.kgraphql.helpers.getFields
88
import com.apurebase.kgraphql.schema.execution.Execution
9-
import kotlinx.coroutines.test.runBlockingTest
109
import org.amshove.kluent.*
1110
import org.hamcrest.CoreMatchers
1211
import org.hamcrest.MatcherAssert
@@ -136,7 +135,7 @@ class UnionsSpecificationTest : BaseSchemaTest() {
136135

137136
@Test
138137
fun `A Union type must define one or more unique member types`(){
139-
expect<SchemaException>("A Union type must define one or more unique member types"){
138+
expect<SchemaException>("The union type 'invalid' has no possible types defined, requires at least one. Please refer to https://kgraphql.io/Reference/Type%20System/unions/") {
140139
KGraphQL.schema {
141140
unionType("invalid") {}
142141
}
@@ -241,4 +240,33 @@ class UnionsSpecificationTest : BaseSchemaTest() {
241240
extract<List<String>>("data/returnUnion/fields") shouldBeEqualTo listOf("i", "fields", "s")
242241
}
243242
}
243+
244+
@Test
245+
fun `union types with custom name def resolver`() {
246+
defaultSchema {
247+
unionType<WithFields> {
248+
subTypeBlock = {
249+
name = "Prefix$name"
250+
}
251+
}
252+
253+
query("returnUnion") {
254+
resolver { node: Execution.Node ->
255+
listOf(
256+
WithFields.Value1(1, node.getFields()),
257+
WithFields.Value2("key", node.getFields()),
258+
)
259+
}
260+
}
261+
}.executeBlocking("""
262+
{
263+
returnUnion {
264+
__typename
265+
}
266+
}
267+
""".trimIndent()).also(::println).deserialize().run {
268+
extract<String>("data/returnUnion[0]/__typename") shouldBeEqualTo "PrefixValue1"
269+
extract<String>("data/returnUnion[1]/__typename") shouldBeEqualTo "PrefixValue2"
270+
}
271+
}
244272
}

0 commit comments

Comments
 (0)