aPureBase / KGraphQL

Pure Kotlin GraphQL implementation
https://kgraphql.io
MIT License
301 stars 58 forks source link

[Question] How to properly change the default properties of a sealed class? #147

Open NathanPB opened 3 years ago

NathanPB commented 3 years ago

Hello, I'm trying to build a data structure that looks like this:

@Serializable
sealed class Person {

    @Serializable
    class Internal(val uid: String) : Person() {
        val profile: UserProfile?
            get() = findUserById(uid)
    }

    @Serializable
    class External(val email: String, val name: String) : Person()
}
unionType<Person>() {
        type<Person.Internal>() {
            property(Person.Internal::uid) {}
            property(Person.Internal::profile) {}
        }

        type<Person.External>()
    }

The thing actually works as expected, but when I open the Playground I lose the autocomplete and I see this error in the browser console: Uncaught Error: Expected Person to be a GraphQL Interface type..

Since it works it's acceptable, but losing the autocomplete is annoying. Any suggestions on how to make this work?

NathanPB commented 3 years ago

Also tried this and got the same behavior:

@Serializable
class Internal(
    val uid: String,
    @Transient val profile: UserProfile? = findUserById(uid)
) : Person()
unionType<Person>()

and this too

unionType<Person>() {
    type<Person.Internal>() {
        property(Person.Internal::uid) {}
        property<UserProfile?>("profile") {
            resolver { findUserById(it.uid) }
        }
    }
}