Skip to content

Commit

Permalink
Fix #38 data decoder (#40)
Browse files Browse the repository at this point in the history
* Fix #38 data decoder

* scalafmt
  • Loading branch information
muuki88 authored Jul 24, 2018
1 parent 9f4b429 commit 41c556c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
}
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/apollo/starwars-circe/SearchQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 41c556c

Please sign in to comment.