Created
July 9, 2019 08:51
-
-
Save vasekboch/0d8cd8f7a47b117199962217b3899baa to your computer and use it in GitHub Desktop.
Failing GQL mutation
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(name: String) | |
object MyJsonProtocol extends DefaultJsonProtocol { | |
implicit val contactInput: RootJsonFormat[ContactInput] = jsonFormat1(ContactInput.apply) | |
} | |
import MyJsonProtocol._ | |
// The schema | |
val ContactInputType: InputType[ContactInput] = deriveInputObjectType[ContactInput]() | |
val submit: Field[Ctx, Unit] = Field("submit", StringType, | |
arguments = Argument("contacts", ListInputType(ContactInputType)) :: Nil, | |
resolve = c => "hello" + c.arg("contacts") | |
) | |
// Schema root | |
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 {submit(contacts:[{name: "Test"}])} | |
""" | |
val result = Executor.execute( | |
schema, | |
QueryParser.parse(query).get, | |
userContext = Ctx(), | |
) | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment