From 41c556cf2ddef5003a2c9adaa40a2b7e0e1d733d Mon Sep 17 00:00:00 2001 From: Nepomuk Seiler Date: Tue, 24 Jul 2018 17:57:16 +0200 Subject: [PATCH] Fix #38 data decoder (#40) * Fix #38 data decoder * scalafmt --- .../muki/graphql/codegen/ApolloSourceGenerator.scala | 9 +++++++++ .../resources/apollo/starwars-circe/HeroAndFriends.scala | 1 + .../apollo/starwars-circe/HeroFragmentQuery.scala | 1 + .../resources/apollo/starwars-circe/HeroNameQuery.scala | 1 + .../resources/apollo/starwars-circe/InputVariables.scala | 1 + .../resources/apollo/starwars-circe/SearchQuery.scala | 1 + 6 files changed, 14 insertions(+) diff --git a/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala b/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala index 4d0e164..3f624d2 100644 --- a/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala +++ b/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala @@ -54,12 +54,21 @@ case class ApolloSourceGenerator(fileName: String, Lit.String(escapedDocumentString) :: Nil, Nil) + val dataJsonDecoder = + Option(jsonCodeGen.generateFieldDecoder(Type.Name("Data"))) + .filter(_.nonEmpty) + .map { stats => + List[Stat](q"""object Data { ..$stats }""") + } + .getOrElse(List.empty[Stat]) + q""" object $typeName extends ..$additionalInits { type Document = sangria.ast.Document val document: Document = $document case class Variables(..$inputParams) case class Data(..$dataParams) + ..$dataJsonDecoder ..$data }""" } diff --git a/src/test/resources/apollo/starwars-circe/HeroAndFriends.scala b/src/test/resources/apollo/starwars-circe/HeroAndFriends.scala index 900ecf0..0813235 100644 --- a/src/test/resources/apollo/starwars-circe/HeroAndFriends.scala +++ b/src/test/resources/apollo/starwars-circe/HeroAndFriends.scala @@ -23,6 +23,7 @@ object HeroAndFriends { }""" case class Variables() case class Data(hero: Hero) + object Data { implicit val jsonDecoder: Decoder[Data] = deriveDecoder[Data] } case class Hero(name: Option[String], friends: Option[List[Option[Hero.Friends]]]) object Hero { implicit val jsonDecoder: Decoder[Hero] = deriveDecoder[Hero] diff --git a/src/test/resources/apollo/starwars-circe/HeroFragmentQuery.scala b/src/test/resources/apollo/starwars-circe/HeroFragmentQuery.scala index 6496331..fb6d353 100644 --- a/src/test/resources/apollo/starwars-circe/HeroFragmentQuery.scala +++ b/src/test/resources/apollo/starwars-circe/HeroFragmentQuery.scala @@ -14,6 +14,7 @@ object HeroFragmentQuery { }""" case class Variables() case class Data(hero: Hero, human: Option[Human]) + object Data { implicit val jsonDecoder: Decoder[Data] = deriveDecoder[Data] } case class Hero(name: Option[String]) extends CharacterInfo object Hero { implicit val jsonDecoder: Decoder[Hero] = deriveDecoder[Hero] } case class Human(name: Option[String]) extends CharacterInfo diff --git a/src/test/resources/apollo/starwars-circe/HeroNameQuery.scala b/src/test/resources/apollo/starwars-circe/HeroNameQuery.scala index 2291c3b..4c5d5a6 100644 --- a/src/test/resources/apollo/starwars-circe/HeroNameQuery.scala +++ b/src/test/resources/apollo/starwars-circe/HeroNameQuery.scala @@ -11,6 +11,7 @@ object HeroNameQuery { }""" case class Variables() case class Data(hero: Hero) + object Data { implicit val jsonDecoder: Decoder[Data] = deriveDecoder[Data] } case class Hero(name: Option[String]) object Hero { implicit val jsonDecoder: Decoder[Hero] = deriveDecoder[Hero] } } diff --git a/src/test/resources/apollo/starwars-circe/InputVariables.scala b/src/test/resources/apollo/starwars-circe/InputVariables.scala index 49c24b1..c83b915 100644 --- a/src/test/resources/apollo/starwars-circe/InputVariables.scala +++ b/src/test/resources/apollo/starwars-circe/InputVariables.scala @@ -12,6 +12,7 @@ object InputVariables { }""" case class Variables(humanId: String) case class Data(human: Option[Human]) + object Data { implicit val jsonDecoder: Decoder[Data] = deriveDecoder[Data] } case class Human(name: Option[String], homePlanet: Option[String]) object Human { implicit val jsonDecoder: Decoder[Human] = deriveDecoder[Human] } } diff --git a/src/test/resources/apollo/starwars-circe/SearchQuery.scala b/src/test/resources/apollo/starwars-circe/SearchQuery.scala index 91c80cd..f20530e 100644 --- a/src/test/resources/apollo/starwars-circe/SearchQuery.scala +++ b/src/test/resources/apollo/starwars-circe/SearchQuery.scala @@ -22,6 +22,7 @@ object SearchQuery { }""" case class Variables(text: String) case class Data(search: List[Search]) + object Data { implicit val jsonDecoder: Decoder[Data] = deriveDecoder[Data] } sealed trait Search { def __typename: String def name: Option[String]