How to model user,posts and likes on dgraph?

^ this must not be your exact schema then… your schema most likely has LikedBy: User! and an id field

So if you want to get the likes only by a specific user then apply the filter and cascade. Or rearrange the query:

{
  queryLike @cascade {
    id
    likedBy(filter: {username: {eq: "test"}}) {
      username
    }
  }
}
{
  queryUser(filter: { username: { eq: "test" }) {
    likes {
      id
    }
  }
}

See for reference of work in progress/RFC:

This is business logic and there are several different ways to handle it. Each different way to implement this has its own tradeoffs. do you want to store a list somewhere of sent notifications, or do you want to perpetually store a Like with a delete boolean field that you change if a user deletes the like, but you keep the node itself. But then you have to factor that in when counting likes for posts… This is what makes every schema structure unique and why not every schema is the same because of business logic like this and how the developer wants to handle unique edge cases.

1 Like