The Sangria API expects a type for these of Map[String, Any]
This is not true. Variables for an execution in sangria can be of an arbitrary type T
, the only requirement that you have an instance of InputUnmarshaller[T]
type class for it. All marshalling integration libraries provide an instance of InputUnmarshaller
for correspondent JSON AST type.
This means that sangria-circe defines InputUnmarshaller[io.circe.Json]
and you can import it with import sangria.marshalling.circe._
.
Here is a small and self-contained example of how you can use circe Json
as a variables:
import io.circe.Json
import sangria.schema._
import sangria.execution._
import sangria.macros._
import sangria.marshalling.circe._
val query =
graphql"""
query ($$foo: Int!, $$bar: Int!) {
add(a: $$foo, b: $$bar)
}
"""
val QueryType = ObjectType("Query", fields[Unit, Unit](
Field("add", IntType,
arguments = Argument("a", IntType) :: Argument("b", IntType) :: Nil,
resolve = c ⇒ c.arg[Int]("a") + c.arg[Int]("b"))))
val schema = Schema(QueryType)
val vars = Json.obj(
"foo" → Json.fromInt(123),
"bar" → Json.fromInt(456))
val result: Future[Json] =
Executor.execute(schema, query, variables = vars)
As you can see in this example, I used io.circe.Json
as variables for an execution. The execution would produce following result JSON:
{
"data": {
"add": 579
}
}