sangria-relay icon indicating copy to clipboard operation
sangria-relay copied to clipboard

How to handle object input types in mutations

Open DaveDeCaprio opened this issue 7 years ago • 1 comments

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.

DaveDeCaprio avatar Jun 02 '17 14:06 DaveDeCaprio

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 Maps. More docs on this topic: http://sangria-graphql.org/learn/#frominput-type-class

OlegIlyenko avatar Jun 13 '17 22:06 OlegIlyenko