Upgrade to Play 2.6.x
Closed this issue · 2 comments
Play-Json 2.6.0 is at the M3 release state...and already has a bunch of interesting new stuff. One of the best is Json.{Reads/Writes/Format} with defaults, like
case class User(id: Option[Long] = None, name: String)
Json.using[Json.WithDefaultValues].format[User]
This is super-useful so that you don't have to have two case classes, one like UserInput(name: String)
and the other one that is used internally and/or stored to your database using ORM.
At present, I find myself having to write:
implicit val UserType = deriveObjectType[User]
implicit val jsonUser = Json.format[User]
implicit val UserIntputType = deriveInputObjectType[UserInput]
implicit val jsonUserInput = Json.format[UserInput]
Maintaining two types and copying between them becomes very tedious with more complex case classes. Play-Json 2.6.0 seems to solve this... playframework/play-json@697a217
In your example you don't really need implicit val jsonUser = Json.format[User]
, deriveObjectType
works directly with case class and discovers it's structure.
implicit val jsonUserInput = Json.format[UserInput]
is still needed, even with v 2.6.0 of play-json. At the moment, all JSON → case class transformations are handled by FromInput
type class. Conveniently there is one instance of a type class available for play-json's Format
, so as soon as you have one available in scope, sangria is able to transform GraphQL input values to case classes. Similarly, this functionality is available for other JSON libraries as well (like circe).
An alternative would be to handle this transformation natively without reliance on any existing JSON formattes. At some point, I decided to take advantage of existing JSON serialization libraries instead of writing input → case class transformation from scratch because there is already a lot of very good libraries available for this.
Regarding the update. I just published v1.0.1 which now depends on play-json v2.6.0-M3:
https://github.com/sangria-graphql/sangria-play-json/releases/tag/v1.0.1
Thanks. I was working on the update simultaneously. Just did a pull request with a few fixes for deeper issues that people may bump into with play-json 2.6.0.