A quick question - is it possible to use Kotlin in...
# graphql-kotlin
m
A quick question - is it possible to use Kotlin interface and represent it as GraphQL Type? I have some generics constraints which require me to use interface in the Kotlin world, but need to expose it as Type in GraphQL world. Is something like this doable?
d
Depends on your use cases. In geneal Kotlin interfaces will be mapped either to GraphQL interface (if there are some fields) or to a union (if it is a marker/empty interface)
That being said -> JVM generics are not supported as they are not supported in GraphQL
m
I understand, but trying to implement ValueOrError pattern using the union type and need some constraints in our building blocks which require me to use given element as Kotlin interface (cannot inherit from two classes). However at the same time would like to expose it as GraphQL type. Technically I don't think we need to constraint GraphQL types to be derived only from classes, right? Technically Kotlin interface could be exposed as GraphQL Type. WDYT?
So not trying to use generics with GraphQL, only trying to use my building blocks with generic types which are constrained by classes/interfaces exposed as GraphQL schema
Also when I'm at it - have you guys thoughts about using sealed classes pattern to represent union types? What we have right now with the marker interface is neat, but does not necessarily work in the code that well (as you pretty much return a marker interface which does not allow me to do an exhaustive when statement)
d
If you want to define some common traits but not expose it in the schema, you can annotate superclass/interface using
@GraphQLIgnore
which will exclude it from schema
For unions - you can use marker interface (and soon the more flexible annotation see open pr for details)
As for sealed class support -> havent tried it but it might already be supported.
m
Looks like going with interface with
@GraphQLIgnore
might do the job. Looking forward to see what the annotation based union type will look like. Thanks for the help.
­čĹŹ 1
s
The other option is to manually define a Kotlin class with the name you want in the schema, have that implement your interface, and then using hooks return a class which would get mapped to a concrete GraphQL type
But that being said it seems like it doesn't harm your clients to also expose this interface if that truly what it is in code