How to implement ToInput when using json4s?

135 views
Skip to first unread message

yin...@tubi.tv

unread,
Aug 28, 2018, 8:03:12 AM8/28/18
to sangria-graphql
Hi,

I am using json4s with sangria-graphql. I got this compile error:

Type Option[scala.math.BigDecimal] cannot be used as a default value. Please consider defining an implicit instance of `ToInput` for it.

I can see the trait is 

ToInput[Val, Raw]

How do I know which type Raw is the compiler looking for? I attempted JValue / JObject but no luck. 

Thanks!
Yingyu

Oleg Ilyenko

unread,
Aug 28, 2018, 2:34:42 PM8/28/18
to sangria-graphql
Since you would like to define the default value with a scala type, you need to wrap the default value in `scalaInput`. This will tell sangria how to interpret the value. For example:

import sangria.marshalling.ScalaInput.scalaInput

val w1 = Argument("myArg", OptionInputType(BigDecimalType), defaultValue = scalaInput(BigDecimal("123")))

Alternatively, you can also use JSON value, like this `..., defaultValue = JsNumber(BigDecimal("123"))`. In this example, I used spray-json. It will work if you have an appropriate import for a marshaller (`import sangria.marshalling.sprayJson._` in this case)

Yingyu Cheng

unread,
Aug 28, 2018, 9:08:55 PM8/28/18
to Oleg Ilyenko, sangria-graphql
Thanks Oleg!

For my case, I am using deriveInputObjectType to a complex case class, so I don't have a default value set.

And I figure it out, I create ToInput[Option[BigDecimal], JValue] and it helps. I was wrong to create ToInput[BigDecimal, JValue]



--
You received this message because you are subscribed to the Google Groups "sangria-graphql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sangria-graph...@googlegroups.com.
To post to this group, send email to sangria...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sangria-graphql/5145b72e-d47b-45e3-be29-0b3b40864a73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oleg Ilyenko

unread,
Aug 29, 2018, 6:50:36 AM8/29/18
to sangria-graphql
Glad that you found a solution. If you are using macro, you can also use `@GraphQLDefault` annotation to provide a default.
Message has been deleted

Pritam

unread,
Mar 14, 2019, 12:43:15 AM3/14/19
to sangria-graphql
I am stuck with a Similar issue. 

Trying to get a derived input object from a case class.

  val UserInputType = deriveInputObjectType[UserRow]()
  case class UserRow(id: Int, name: String, fullName: Option[String] = None, lastModifiedTime: Option[java.sql.Timestamp] = None) 

but getting this following error 

Type Option[java.sql.Timestamp] cannot be used as a default value. Please consider defining an implicit instance of `ToInput` for it.

How do I define a ToInput for Type Option[java.sql.Timestamp]. Not able to find an example. Please help.

Is is something like this ? 

  implicit val OptionalTime = new ToInput[Option[Timestamp], BigInt] {
    override def toInput(value: Option[Timestamp]): (BigInt, InputUnmarshaller[BigInt]) = (value.get.getNanos(), ??? )
  }

Not able to find an example of how to use ToInput Trait for Timestamp. 
Stuck with what needs to be filled up for ???
Reply all
Reply to author
Forward
0 new messages