Skip to main content
Version: 5.x.x

Enums

Enums are automatically mapped to GraphQL enum type.

enum class MyEnumType {  ONE,  TWO}

Above enum will be generated as following GraphQL object

enum MyEnumType {  ONE  TWO}

Converting a Java enum to a GraphQL Enum#

If you want to use Java enums from another package, but you don't want include everything from that package using supportedPackages or you want to customize the GraphQL type, you can use schema generator hooks to associate the Java enum with a runtime GraphQLEnumType.

Step 1: Create a GraphQLEnumType using the Java enum values

// in some other packagepublic enum Status {  APPROVED,  DECLINED}
val statusEnumType = GraphQLEnumType.newEnum()    .name("Status")    .values(Status.values().map {      GraphQLEnumValueDefinition.newEnumValueDefinition()          .value(it.name)          .build()    })    .build()

Step 2: Add a schema generation hook

class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {
  override fun willGenerateGraphQLType(type: KType): GraphQLType? {    return when (type.classifier as? KClass<*>) {      Status::class.java -> statusEnumType      else -> super.willGenerateGraphQLType(type)    }  }}

Step 3. Use your Java enum anywhere in your schema

@Componentclass StatusQuery : Query {  fun currentStatus: Status = getCurrentStatus()}