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

Permit scalarMappings to be used with enumerations #930

Merged
merged 1 commit into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ object ClientWriter {

val inputs = schema.inputObjectTypeDefinitions.map(writeInputObject).mkString("\n")

val enums = schema.enumTypeDefinitions.map(writeEnum).mkString("\n")
val enums = schema.enumTypeDefinitions
.filter(e => !scalarMappings.scalarMap.exists(_.contains(e.name)))
.map(writeEnum)
.mkString("\n")

val scalars = schema.scalarTypeDefinitions
.filterNot(s => isScalarSupported(s.name))
Expand Down
68 changes: 68 additions & 0 deletions tools/src/test/scala/caliban/tools/ClientWriterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,74 @@ object Client {
}
}

}
"""
)
)
},
testM("scalar mapped enum") {
val schema =
"""
enum Origin {
EARTH
MARS
BELT
}

enum Destination {
EARTH
MARS
BELT
}

input Routes {
origin: Origin!
destinations: [Destination!]!
}
""".stripMargin

assertM(gen(schema, scalarMappings = Map("Destination" -> "com.example.Destination")))(
equalTo(
"""import caliban.client.CalibanClientError.DecodingError
import caliban.client._
import caliban.client.__Value._

object Client {

sealed trait Origin extends scala.Product with scala.Serializable
object Origin {
case object EARTH extends Origin
case object MARS extends Origin
case object BELT extends Origin

implicit val decoder: ScalarDecoder[Origin] = {
case __StringValue("EARTH") => Right(Origin.EARTH)
case __StringValue("MARS") => Right(Origin.MARS)
case __StringValue("BELT") => Right(Origin.BELT)
case other => Left(DecodingError(s"Can't build Origin from input $other"))
}
implicit val encoder: ArgEncoder[Origin] = {
case Origin.EARTH => __EnumValue("EARTH")
case Origin.MARS => __EnumValue("MARS")
case Origin.BELT => __EnumValue("BELT")
}
}

case class Routes(origin: Origin, destinations: List[com.example.Destination] = Nil)
object Routes {
implicit val encoder: ArgEncoder[Routes] = new ArgEncoder[Routes] {
override def encode(value: Routes): __Value =
__ObjectValue(
List(
"origin" -> implicitly[ArgEncoder[Origin]].encode(value.origin),
"destinations" -> __ListValue(
value.destinations.map(value => implicitly[ArgEncoder[com.example.Destination]].encode(value))
)
)
)
}
}

}
"""
)
Expand Down