Skip to content

Commit

Permalink
Permit scalarMappings to be used with enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Jun 19, 2021
1 parent 6ac0b94 commit e92f3d6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
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

0 comments on commit e92f3d6

Please sign in to comment.