sangria-relay
sangria-relay copied to clipboard
How to handle object input types in mutations
In the relay-playground, the mutation only accepts primitive types as arguments. I'm trying to have a mutation parameter which is an object. The issue is that in mutateAndGetPayload, the input object is a Map and the input object comes through as a nested Map rather than the correct type. I'm currently handling this using:
val myObj = fromMap[MyInputType](input("myObj").asInstanceOf[Map[String, Any]])
where "fromMap" is a function that instantiates a case class from a Map that I got from https://stackoverflow.com/questions/20684572/scala-convert-map-to-case-class.
This works in my current case but feels wrong and I think will break down if I get to nested objects.
In there a correct pattern for this.
I would suggest you to check this example:
https://github.com/sangria-graphql/sangria-relay/blob/master/src/test/scala/sangria/relay/MutationSpec.scala#L52
It works with scala case classes (wich have a JsonFormat
) instead of Map
s. More docs on this topic: http://sangria-graphql.org/learn/#frominput-type-class