Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Union Properties

Paweł Gutkowski edited this page Jul 28, 2017 · 1 revision

As Kotlin does not support unions, union properties have to be explicitly declared in Schema DSL. Creating union property requires defining resolver and return type. Return Type is reference returned by creation of union type.

Union type resolver is not type checked. Invalid resolver implementation which would return value of type other than members of union type will fail in runtime.

Example

data class UnionMember1(val one : String)

data class UnionMember2(val two : String)

data class Person(val name: String, val age: Int)

KGraphQL.schema {
    type<Person>{
        unionProperty("union"){
            returnType = unionType("UnionExample"){
                 type<UnionMember1>()
                 type<UnionMember2>()
            }
            resolver { person -> if (person.age <= 18) UnionMember1("one") else UnionMember2("two") }
        }
    }
}