diff --git a/modules/core/src/main/scala/sangria/schema/package.scala b/modules/core/src/main/scala/sangria/schema/package.scala index e540a4ec..5b77bd7c 100644 --- a/modules/core/src/main/scala/sangria/schema/package.scala +++ b/modules/core/src/main/scala/sangria/schema/package.scala @@ -2,6 +2,7 @@ package sangria import sangria.marshalling.MarshallerCapability import sangria.validation._ +import scala.util.Try /** Types that describe a GraphQL schema. * @@ -47,12 +48,14 @@ package object schema { case i: BigInt => Right(i.longValue) case d: Double if d.isWhole => Right(d.toLong) case d: BigDecimal if d.isValidLong => Right(d.longValue) + case s: String if Try(s.toLong).isSuccess => Right(s.toLong) case _ => Left(LongCoercionViolation) }, coerceInput = { case ast.IntValue(i, _, _) => Right(i: Long) case ast.BigIntValue(i, _, _) if !i.isValidLong => Left(BigLongCoercionViolation) case ast.BigIntValue(i, _, _) => Right(i.longValue) + case ast.StringValue(s, _, _, _, _) if Try(s.toLong).isSuccess => Right(s.toLong) case _ => Left(LongCoercionViolation) } ) diff --git a/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala b/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala index c924e21f..5939faf3 100644 --- a/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala +++ b/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala @@ -78,6 +78,7 @@ class CoercionSpec extends AnyWordSpec with Matchers { LongType.coerceInput(BigIntValue(BigInt("123234"))) should be(Right(123234L)) LongType.coerceInput(BigIntValue(BigInt("1232342131243432"))) should be( Right(1232342131243432L)) + LongType.coerceInput(StringValue(Long.MaxValue.toString)) should be(Right(Long.MaxValue)) LongType .coerceInput(BigIntValue(BigInt("123234438749837964783648763284768372648723684763287"))) .isLeft should be(true) @@ -85,7 +86,9 @@ class CoercionSpec extends AnyWordSpec with Matchers { LongType.coerceInput(FloatValue(12.34)).isLeft should be(true) LongType.coerceInput(BigDecimalValue(BigDecimal(12.34))).isLeft should be(true) LongType.coerceInput(BooleanValue(true)).isLeft should be(true) - LongType.coerceInput(StringValue("123")).isLeft should be(true) + LongType + .coerceInput(StringValue("123234438749837964783648763284768372648723684763287")) + .isLeft should be(true) } "BigInt" in { @@ -223,7 +226,12 @@ class CoercionSpec extends AnyWordSpec with Matchers { LongType.coerceUserInput(12.34d).isLeft should be(true) LongType.coerceUserInput(BigDecimal(12.34)).isLeft should be(true) LongType.coerceUserInput(true).isLeft should be(true) - LongType.coerceUserInput("123").isLeft should be(true) + LongType.coerceUserInput("123") should be(Right(123L)) + LongType.coerceUserInput("").isLeft should be(true) + LongType + .coerceUserInput("1232344387498237498732974982334324234325435") + .isLeft should be(true) + LongType.coerceUserInput("123.0").isLeft should be(true) LongType.coerceUserInput(new Date).isLeft should be(true) }