Mapping result example.
nandotorterolo opened this issue · comments
Fernando commented
Hi, could you provide me an example of mapping result from Fauna database in Scala, thanks!
val result2 = client.query(
Paginate(Ref("databases"))
).map{
res => println(res.to[String])
}
.recover {
case NonFatal(t) => println(s"paginate database: ${t.getMessage}")
}
res
VFail(List(FieldError(Expected String; found value ObjectV(Map(data -> ArrayV(Vector(RefV(my-first-database,Some(RefV(databases,None,None)),None))))) of type faunadb.values.ObjectV.,/)))
Eduardo V. Bruno commented
Hi there,
There are a few things you can do:
res.to[A]
whereA
is any scala native type likeString
,Int
,Boolean
, etc.. or even:res.to[Seq[A]]
,res.to[Map[String, A]
,res.to[Option[A]]
andres.to[Either[L, R]]
- You can traverse and convert: https://github.com/fauna/faunadb-jvm/blob/master/faunadb-scala/src/test/scala/faunadb/ClientSpec.scala#L134
- Or you can create a case class with a
Record
codec and dores.to[User]
, for example: https://github.com/fauna/faunadb-jvm/blob/master/faunadb-scala/src/test/scala/faunadb/CodecSpec.scala#L42. - one more: https://github.com/retroryan/learn-fauna-scala/blob/lesson6/src/main/scala/com/fauna/learnfauna/SpellExample.scala#L243
If you had more questions, you're welcome to join: https://fauna-community.slack.com
ps: thanks @erickpintor, I borrowed your answer.
Fernando commented
👍 thanks !