Search code examples
scalaplayframeworkgraphqlslicksangria

Graphql Sangria Many-to-Many example


I'm working on a Many-to-Many relation with Sangria and Slick. I have 3 objects: Role, Permission and RolePermission (a mapping table)

case class Role(id: Long, name: String)
case class Permission(id: Long, name: String)
case class RolePermission(id: Long, roleId: Long, permissionId: Long)

So far I figured out how to define the relation:

val permissionsByRoleId = Relation[Permission, (RolePermission, Permission), Long]("permissionsByRoleId", tmp ⇒ Seq(tmp._1.roleId), _._2)

I don't know how to define the fetcher and how to add the right field to Role Schema, so that I could retrieve the Role's permissions passing by the intermediate table.

Any help ?


Solution

  • You have to define Relation type for both related entities. It's not easy to answer in short comment on StackOverflow, but I've explained such kind of relation in my blog post: https://scalac.io/akka-http-sangria-graphql-backend#implementing-many-to-many-relation I hope it helps..