Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import sangria.schema._
  2. import sangria.execution._
  3. import sangria.marshalling.sprayJson._
  4. import sangria.parser.QueryParser
  5. import spray.json._
  6. import scala.concurrent.duration._
  7. import scala.concurrent.Await
  8. import scala.concurrent.ExecutionContext.Implicits.global
  9. import sangria.macros.derive.deriveInputObjectType
  10.  
  11. case class Ctx()
  12. case class ContactInput(id: Int, name: String, email: Option[String])
  13.  
  14. object MyJsonProtocol extends DefaultJsonProtocol {
  15. implicit val contactInput = jsonFormat3(ContactInput.apply)
  16. }
  17.  
  18. import MyJsonProtocol._
  19.  
  20. // The schema
  21. val ContactInputType: InputType[ContactInput] = deriveInputObjectType[ContactInput]()
  22. val submit: Field[Ctx, Unit] = Field("submit", StringType,
  23. arguments = List(
  24. Argument(
  25. "contacts",
  26. ListInputType(ContactInputType),
  27. )
  28. ),
  29. resolve = c => "hello" + c.arg("contacts")
  30. )
  31.  
  32. val Mutation: ObjectType[Ctx, Unit] = ObjectType("Mutation", fields[Ctx, Unit](submit))
  33. val Query: ObjectType[Ctx, Unit] = ObjectType("Query", fields[Ctx, Unit](
  34. Field("hello", StringType, resolve = _ ⇒ "Hello world!")
  35. ))
  36. val schema = Schema(Query, Some(Mutation))
  37.  
  38. // Test mutation
  39. val query = """
  40. mutation SaveChanges ($contacts: [ContactInput!]!) {
  41. submit(contacts: $contacts)
  42. }
  43. """
  44.  
  45. val vars = """{
  46. "contacts": [{
  47. "id": 1,
  48. "name": "Foo",
  49. "email": "foo@localhost"
  50. },
  51. {
  52. "id": 2,
  53. "name": "Bar",
  54. "email": "bar@localhost"
  55. }]
  56. }""".parseJson.asJsObject()
  57.  
  58. val result = Executor.execute(
  59. schema,
  60. QueryParser.parse(query).get,
  61. userContext = Ctx(),
  62. variables = vars
  63. )
  64. println(Await.result(result, 10.seconds))
  65.  
  66. // Expected output:
  67. //
  68. // {
  69. // "hello": "helloVector(...."
  70. // }
  71.  
  72. // Actual output:
  73. //
  74. // java.lang.ClassCastException: scala.collection.immutable.Vector cannot be cast to spray.json.JsValue
  75. // at sangria.marshalling.sprayJson$$anon$2.fromResult(scratch.scala:99)
  76. // at sangria.execution.ValueCollector$.$anonfun$getArgumentValues$6(scratch.scala:86)
  77. // at sangria.execution.ValueCoercionHelper.resolveMapValue(scratch.scala:126)
  78. // at sangria.execution.ValueCollector$.$anonfun$getArgumentValues$4(scratch.scala:87)
  79. // at #worksheet#.#worksheet#(scratch.scala:118)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement