Created
July 9, 2019 07:41
-
-
Save vasekboch/acb1a61f3812c5e95c26a5f301f4d339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment