dgs-codegen icon indicating copy to clipboard operation
dgs-codegen copied to clipboard

Unable to generate constructor param and populate companion object in models and enum

Open shapan1 opened this issue 2 years ago • 1 comments

I am trying to generate an enum class with a label as the constructor param and have a function in the companion object. The schema GraphQL looks like

type Account {
    accountId: String!
    accountNumber: Int!
    accountStatus: AccountStatus
    accountBalance: Float!
}

enum AccountStatus {
    ACTIVE,
    INACTIVE,
    CLOSED,
    UNKNOWN
}

and the generated enum looks like

@Generated(
  value = ["com.netflix.graphql.dgs.codegen.CodeGen"],
  date = "2022-08-09T22:03:45.581297Z",
)
public enum class AccountStatus {
  ACTIVE,
  INACTIVE,
  CLOSED,
  UNKNOWN,
  ;

  public companion object
}

My goal is to have the following enum class generated

enum class AccountStatus(val label: String) {
    UNKNOWN("UNKNOWN"),
    ACTIVE("ACTIVE"),
    INACTIVE("INACTIVE"),
    LOCKED("LOCKED");

    companion object {
        fun fromLabelString(label: String?): AccountStatus {
            return if (label == null) {
                UNKNOWN
            } else {
                values().find { it.label.equals(label) } ?: UNKNOWN
            }
        }
    }
}

I was unable to find documentation regarding generating custom enums and classes and wanted to know if this is currently possible and if not what would be the recommended way to achieve this?

shapan1 avatar Aug 10 '22 18:08 shapan1

Have you tried adding a companion to the generated enum class? AccountStatus.companion.custom() {...}

srinivasankavitha avatar Sep 08 '22 23:09 srinivasankavitha