Skip to content

Commit 204fdcb

Browse files
committed
feat: add .add method to Graph
1 parent 4c0c3e9 commit 204fdcb

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

compose-graphql/src/main/scala/compose/graphql/Graph.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ sealed trait Graph {
2222
}
2323
final def binary: Chunk[Byte] = JsonEncoder.encode(Graph.schema, self)
2424
final def toJson: String = new String(binary.toArray)
25+
final def add[Arg, From]: Graph.PartiallyAppliedConnection[Arg, From] =
26+
new Graph.PartiallyAppliedConnection[Arg, From](self)
2527
}
2628

2729
object Graph {
@@ -37,14 +39,14 @@ object Graph {
3739
final case class Concat(left: Graph, right: Graph) extends Graph
3840

3941
def apply[Arg, From]: PartiallyAppliedConnection[Arg, From] =
40-
new PartiallyAppliedConnection[Arg, From](())
42+
new PartiallyAppliedConnection[Arg, From](empty)
4143

42-
final class PartiallyAppliedConnection[Arg, From](val unit: Unit) extends AnyVal {
44+
final class PartiallyAppliedConnection[Arg, From](val graph: Graph) extends AnyVal {
4345
def apply[To](name: String, resolve: (Arg, From) ~> To)(implicit
4446
arg: Schema[Arg],
4547
from: Schema[From],
4648
to: Schema[To],
47-
): Graph = Cons(
49+
): Graph = graph ++ Cons(
4850
name,
4951
arg.asInstanceOf[Schema[Any]],
5052
from.asInstanceOf[Schema[Any]],

compose-graphql/src/test/scala/compose/graphql/internal/JsonPlaceholder.scala

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,30 @@ object JsonPlaceholder {
7474

7575
object fetch {
7676
import Lambda._
77-
def users: Any ~> List[User] = Lambda
77+
78+
def users: Any ~> List[User] = Lambda
7879
.constant(Request(url = "https://jsonplaceholder.typicode.com/users")) >>>
7980
Lambda.http.decode[List[User]]
8081
.fold(Lambda.constant(List.empty[User]))(Lambda.identity[List[User]])
81-
def posts: Any ~> List[Post] = Lambda
82+
83+
def posts: Any ~> List[Post] = Lambda
8284
.constant(Request(url = "https://jsonplaceholder.typicode.com/posts")) >>>
8385
Lambda.http.decode[List[Post]]
8486
.fold(Lambda.constant(List.empty[Post]))(Lambda.identity[List[Post]])
85-
def postUser: Post ~> User = Lambda.die
86-
def userPosts: User ~> List[Post] = Lambda.die
87-
def userAlbums: User ~> List[Album] = Lambda.die
8887

8988
def albums: UserId ~> List[Album] = Lambda
9089
.constant(Request(url = "https://jsonplaceholder.typicode.com/albums")) >>>
9190
Lambda.http.decode[List[Album]]
9291
.fold(Lambda.constant(List.empty[Album]))(Lambda.identity[List[Album]])
9392

94-
def albumPhotos: Album ~> List[Photo] = Lambda.die
95-
def postComments: Post ~> List[Comment] = Lambda.die
96-
def albumUser: Album ~> User = Lambda.die
97-
def userComments: User ~> List[Comment] = Lambda.die
98-
def photoAlbum: Photo ~> Album = Lambda.die
93+
def diePostUser: Post ~> User = Lambda.die
94+
def dieUserPosts: User ~> List[Post] = Lambda.die
95+
def dieUserAlbums: User ~> List[Album] = Lambda.die
96+
def dieAlbumPhotos: Album ~> List[Photo] = Lambda.die
97+
def diePostComments: Post ~> List[Comment] = Lambda.die
98+
def dieAlbumUser: Album ~> User = Lambda.die
99+
def dieUserComments: User ~> List[Comment] = Lambda.die
100+
def diePhotoAlbum: Photo ~> Album = Lambda.die
99101
}
100102

101103
case class UserId(userId: Option[Int])
@@ -104,15 +106,18 @@ object JsonPlaceholder {
104106
val lens = DeriveAccessors.gen[UserId]
105107
}
106108

107-
val graph: Graph = Graph[Unit, Unit]("posts", fetch.posts) ++
108-
Graph[Unit, Unit]("users", fetch.users) ++
109-
Graph[UserId, Unit]("albums", fetch.albums <<< Lambda._1) ++
110-
Graph[Unit, User]("albums", fetch.userAlbums <<< Lambda._2) ++
111-
Graph[Unit, Post]("comments", fetch.postComments <<< Lambda._2) ++
112-
Graph[Unit, User]("comments", fetch.userComments <<< Lambda._2) ++
113-
Graph[Unit, Album]("photos", fetch.albumPhotos <<< Lambda._2) ++
114-
Graph[Unit, User]("posts", fetch.userPosts <<< Lambda._2) ++
115-
Graph[Unit, Album]("user", fetch.albumUser <<< Lambda._2) ++
116-
Graph[Unit, Post]("user", fetch.postUser <<< Lambda._2) ++
117-
Graph[Unit, Photo]("album", fetch.photoAlbum <<< Lambda._2)
109+
val graph: Graph = Graph.empty
110+
.add[Unit, Unit]("posts", fetch.posts)
111+
.add[Unit, Unit]("users", fetch.users)
112+
.add[UserId, Unit]("albums", fetch.albums <<< Lambda._1)
113+
114+
// TODO: remove die implementations
115+
.add[Unit, User]("albums", fetch.dieUserAlbums <<< Lambda._2)
116+
.add[Unit, Post]("comments", fetch.diePostComments <<< Lambda._2)
117+
.add[Unit, User]("comments", fetch.dieUserComments <<< Lambda._2)
118+
.add[Unit, Album]("photos", fetch.dieAlbumPhotos <<< Lambda._2)
119+
.add[Unit, User]("posts", fetch.dieUserPosts <<< Lambda._2)
120+
.add[Unit, Album]("user", fetch.dieAlbumUser <<< Lambda._2)
121+
.add[Unit, Post]("user", fetch.diePostUser <<< Lambda._2)
122+
.add[Unit, Photo]("album", fetch.diePhotoAlbum <<< Lambda._2)
118123
}

0 commit comments

Comments
 (0)