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

Support Short schemas and args #1011

Merged
merged 2 commits into from
Aug 24, 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 client/src/main/scala/caliban/client/ArgEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import scala.annotation.implicitNotFound
*/
@implicitNotFound(
"""Cannot find an ArgEncoder for type ${A}.

Caliban needs it to know how to encode arguments of type ${A}.
"""
)
Expand All @@ -23,6 +23,9 @@ trait ArgEncoder[-A] { self =>

object ArgEncoder {

// In Scala3 there does not appear to be a short2bigDecimal implicit conversion.
implicit val short: ArgEncoder[Short] = (value: Short) => __NumberValue(value.toInt)

implicit val int: ArgEncoder[Int] = (value: Int) => __NumberValue(value)

implicit val long: ArgEncoder[Long] = (value: Long) => __NumberValue(value)
Expand Down
6 changes: 5 additions & 1 deletion client/src/main/scala/caliban/client/ScalarDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import scala.annotation.implicitNotFound
*/
@implicitNotFound(
"""Cannot find a ScalarDecoder for type ${A}.

Caliban needs it to know how to decode a scalar of type ${A}.
"""
)
Expand All @@ -23,6 +23,10 @@ trait ScalarDecoder[+A] {
}

object ScalarDecoder {
implicit val short: ScalarDecoder[Short] = {
case __NumberValue(value) => Right(value.toShort)
case other => Left(DecodingError(s"Can't build a Short from input $other"))
}
implicit val int: ScalarDecoder[Int] = {
case __NumberValue(value) =>
Try(value.toIntExact).toEither.left.map(ex => DecodingError(s"Can't build an Int from input $value", Some(ex)))
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/caliban/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ trait GenericSchema[R] extends SchemaDerivation[R] with TemporalSchema {
implicit val booleanSchema: Schema[Any, Boolean] = scalarSchema("Boolean", None, BooleanValue.apply)
implicit val stringSchema: Schema[Any, String] = scalarSchema("String", None, StringValue.apply)
implicit val uuidSchema: Schema[Any, UUID] = scalarSchema("ID", None, uuid => StringValue(uuid.toString))
implicit val shortSchema: Schema[Any, Short] = scalarSchema("Short", None, IntValue(_))
implicit val intSchema: Schema[Any, Int] = scalarSchema("Int", None, IntValue(_))
implicit val longSchema: Schema[Any, Long] = scalarSchema("Long", None, IntValue(_))
implicit val bigIntSchema: Schema[Any, BigInt] = scalarSchema("BigInt", None, IntValue(_))
Expand Down