Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sangria.schema._
- import sangria.execution._
- import sangria.marshalling.sprayJson._
- import sangria.parser.QueryParser
- import spray.json._
- import scala.concurrent.duration._
- import scala.concurrent.Await
- import scala.concurrent.ExecutionContext.Implicits.global
- import sangria.macros.derive.deriveInputObjectType
- case class Ctx()
- case class ContactInput(id: Int, name: String, email: Option[String])
- object MyJsonProtocol extends DefaultJsonProtocol {
- implicit val contactInput = jsonFormat3(ContactInput.apply)
- }
- import MyJsonProtocol._
- // The schema
- val ContactInputType: InputType[ContactInput] = deriveInputObjectType[ContactInput]()
- val submit: Field[Ctx, Unit] = Field("submit", StringType,
- arguments = List(
- Argument(
- "contacts",
- ListInputType(ContactInputType),
- )
- ),
- resolve = c => "hello" + c.arg("contacts")
- )
- val Mutation: ObjectType[Ctx, Unit] = ObjectType("Mutation", fields[Ctx, Unit](submit))
- val Query: ObjectType[Ctx, Unit] = ObjectType("Query", fields[Ctx, Unit](
- Field("hello", StringType, resolve = _ ⇒ "Hello world!")
- ))
- val schema = Schema(Query, Some(Mutation))
- // Test mutation
- val query = """
- mutation SaveChanges ($contacts: [ContactInput!]!) {
- submit(contacts: $contacts)
- }
- """
- val vars = """{
- "contacts": [{
- "id": 1,
- "name": "Foo",
- "email": "foo@localhost"
- },
- {
- "id": 2,
- "name": "Bar",
- "email": "bar@localhost"
- }]
- }""".parseJson.asJsObject()
- val result = Executor.execute(
- schema,
- QueryParser.parse(query).get,
- userContext = Ctx(),
- variables = vars
- )
- println(Await.result(result, 10.seconds))
- // Expected output:
- //
- // {
- // "hello": "helloVector(...."
- // }
- // Actual output:
- //
- // java.lang.ClassCastException: scala.collection.immutable.Vector cannot be cast to spray.json.JsValue
- // at sangria.marshalling.sprayJson$$anon$2.fromResult(scratch.scala:99)
- // at sangria.execution.ValueCollector$.$anonfun$getArgumentValues$6(scratch.scala:86)
- // at sangria.execution.ValueCoercionHelper.resolveMapValue(scratch.scala:126)
- // at sangria.execution.ValueCollector$.$anonfun$getArgumentValues$4(scratch.scala:87)
- // at #worksheet#.#worksheet#(scratch.scala:118)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement