Skip to content

Commit

Permalink
Access to configure possibleTypes via TypeDSL for sealed union types
Browse files Browse the repository at this point in the history
  • Loading branch information
jeggy committed Aug 4, 2021
1 parent 600b88f commit 83959cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# KGraphQL version
version=0.17.12
version=0.17.13

# Dependencies
coroutine_version=1.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ class SchemaBuilder internal constructor() {
if (!T::class.isSealed) throw SchemaException("Can't generate a union type out of a non sealed class. '${T::class.simpleName}'")

return unionType(T::class.simpleName!!) {
T::class.sealedSubclasses.forEach { type(it) }
block()
T::class.sealedSubclasses.forEach {
type(it, subTypeBlock) // <-- Adds to schema definition
type(it) // <-- Adds to possible union type
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class UnionTypeDSL() : ItemDSL() {

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

var subTypeBlock: TypeDSL<*>.() -> Unit = {}

fun <T : Any>type(kClass : KClass<T>){
possibleTypes.add(kClass)
}

inline fun <reified T : Any>type(){
type(T::class)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ data class MutableSchemaDefinition (

unions.forEach { union ->
if(union.members.isEmpty()){
throw SchemaException("A Union type must define one or more unique member types")
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/")
}
union.members.forEach { member ->
validateUnionMember(union, member, compiledObjects)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.apurebase.kgraphql.schema.SchemaException
import com.apurebase.kgraphql.GraphQLError
import com.apurebase.kgraphql.helpers.getFields
import com.apurebase.kgraphql.schema.execution.Execution
import kotlinx.coroutines.test.runBlockingTest
import org.amshove.kluent.*
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
Expand Down Expand Up @@ -136,7 +135,7 @@ class UnionsSpecificationTest : BaseSchemaTest() {

@Test
fun `A Union type must define one or more unique member types`(){
expect<SchemaException>("A Union type must define one or more unique member types"){
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/") {
KGraphQL.schema {
unionType("invalid") {}
}
Expand Down Expand Up @@ -241,4 +240,33 @@ class UnionsSpecificationTest : BaseSchemaTest() {
extract<List<String>>("data/returnUnion/fields") shouldBeEqualTo listOf("i", "fields", "s")
}
}

@Test
fun `union types with custom name def resolver`() {
defaultSchema {
unionType<WithFields> {
subTypeBlock = {
name = "Prefix$name"
}
}

query("returnUnion") {
resolver { node: Execution.Node ->
listOf(
WithFields.Value1(1, node.getFields()),
WithFields.Value2("key", node.getFields()),
)
}
}
}.executeBlocking("""
{
returnUnion {
__typename
}
}
""".trimIndent()).also(::println).deserialize().run {
extract<String>("data/returnUnion[0]/__typename") shouldBeEqualTo "PrefixValue1"
extract<String>("data/returnUnion[1]/__typename") shouldBeEqualTo "PrefixValue2"
}
}
}

0 comments on commit 83959cc

Please sign in to comment.