Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throwing an exception in codegen for empty enums #1282

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -661,35 +661,34 @@ object ClientWriter {
s"$fieldName.fold(__NullValue: __Value)(value => ${writeInputValue(ListType(ofType, nonNull = true), "value", typeName)})"
}

def writeEnum(
typedef: EnumTypeDefinition,
extensibleEnums: Boolean
): String = {

val enumName = safeTypeName(typedef.name)

val mappingClashedEnumValues = getMappingsClashedNames(
typedef.enumValuesDefinition.map(_.enumValue)
)
def writeEnum(typedef: EnumTypeDefinition, extensibleEnums: Boolean): String =
typedef.enumValuesDefinition match {
case Nil => throw new Exception("Invalid GraphQL Schema: an enum must have at least one value")
case enumValueDefs =>
val enumName = safeTypeName(typedef.name)

val mappingClashedEnumValues = getMappingsClashedNames(
enumValueDefs.map(_.enumValue)
)

def safeEnumValue(enumValue: String): String =
safeName(mappingClashedEnumValues.getOrElse(enumValue, enumValue))
def safeEnumValue(enumValue: String): String =
safeName(mappingClashedEnumValues.getOrElse(enumValue, enumValue))

val enumCases = typedef.enumValuesDefinition
.map(v =>
s"case object ${safeEnumValue(v.enumValue)} extends $enumName { val value: String = ${"\"" + safeEnumValue(v.enumValue) + "\""} }"
) ++
(if (extensibleEnums) Some(s"final case class __Unknown(value: String) extends $enumName") else None)
val enumCases = enumValueDefs
.map(v =>
s"case object ${safeEnumValue(v.enumValue)} extends $enumName { val value: String = ${"\"" + safeEnumValue(v.enumValue) + "\""} }"
) ++
(if (extensibleEnums) Some(s"final case class __Unknown(value: String) extends $enumName") else None)

val decoderCases = typedef.enumValuesDefinition
.map(v => s"""case __StringValue ("${v.enumValue}") => Right($enumName.${safeEnumValue(v.enumValue)})""") ++
(if (extensibleEnums) Some(s"case __StringValue (other) => Right($enumName.__Unknown(other))") else None)
val decoderCases = enumValueDefs
.map(v => s"""case __StringValue ("${v.enumValue}") => Right($enumName.${safeEnumValue(v.enumValue)})""") ++
(if (extensibleEnums) Some(s"case __StringValue (other) => Right($enumName.__Unknown(other))") else None)

val encoderCases = typedef.enumValuesDefinition
.map(v => s"""case ${typedef.name}.${safeEnumValue(v.enumValue)} => __EnumValue("${v.enumValue}")""") ++
(if (extensibleEnums) Some(s"case ${typedef.name}.__Unknown (value) => __EnumValue(value)") else None)
val encoderCases = enumValueDefs
.map(v => s"""case ${typedef.name}.${safeEnumValue(v.enumValue)} => __EnumValue("${v.enumValue}")""") ++
(if (extensibleEnums) Some(s"case ${typedef.name}.__Unknown (value) => __EnumValue(value)") else None)

s"""sealed trait $enumName extends scala.Product with scala.Serializable { def value: String }
s"""sealed trait $enumName extends scala.Product with scala.Serializable { def value: String }
object $enumName {
${enumCases.mkString("\n")}

Expand All @@ -701,12 +700,12 @@ object ClientWriter {
${encoderCases.mkString("\n")}
}

val values: scala.collection.immutable.Vector[$enumName] = scala.collection.immutable.Vector(${typedef.enumValuesDefinition
.map(v => safeEnumValue(v.enumValue))
.mkString(", ")})
val values: scala.collection.immutable.Vector[$enumName] = scala.collection.immutable.Vector(${enumValueDefs
.map(v => safeEnumValue(v.enumValue))
.mkString(", ")})
}
"""
}
}

def writeScalar(
typedef: ScalarTypeDefinition
Expand Down