Is there a way to use generic types with the schem...
# graphql-kotlin
m
Is there a way to use generic types with the schema generator? What I’m looking for is being able to define an abstract class with a generic type and make it “excluded” from the schema, but still expose the functions defined by the interface in the schema together with the resolved type at the use time. Something like this:
Copy code
abstract class Page<T:Any> {
   @GraphQLIgnore
   abstract fetchItems() ... 

   // these will be exposed in the schema
   fun itemsCount(): Int
   fun hasNextPage(): Boolean
   fun pageItems: List<T>
}

class PageOfStrings: Page<String> {
   override fetchItems() ... 
}

class PageOfInts Page<Int> {
   override fetchItems() ... 
}
The resulting schema would have two types:
Copy code
type PageOfStrings {
   itemsCount: Int!
   hasNextPage: Boolean!
   pageItems: [String]!
}

type PageOfInts {
   itemsCount: Int!
   hasNextPage: Boolean!
   pageItems: [Int]!
}
Is something like this doable?
d
in general, generics are not supported (see https://github.com/ExpediaGroup/graphql-kotlin/issues/292)
you could definitely try to see if your use case works -> annotate the
Page
class as ignored and see whether the other types get generated
unsure if it will work
s
Generics are not supported in the schema but as mentioned, what you are trying to implement in the GraphQL type system is an interface with various implementations. If you never need to return the abstract class then you can just mark it as ignored. If you do want to have that as a return type you may be better using a Kotlin interface
m
Perfect, thanks for the explanation, will try the suggestion. Pretty much the motivation is to have some ability to use shared components standardizing handling of things such as pagination. The concrete classes won’t expose any generics, but need a mechanism how to provide them a building blocks which use generic types.