Hey guys, I was wondering. If I have a post and us...
# prisma-whats-new
r
Hey guys, I was wondering. If I have a post and user, what is the best way to check if a user liked a certain post? Right now I have a likes entity and I query on it to find if a like with the combination of user and post exists but I'm not sure if that is a good approach. Maybe there is a simpler way?
m
I think the best decision can be
Copy code
type User @model {
  ...
  likes: [Post!]! @relation(name: "UserLikes")
}
Copy code
type Post @model {
  ...
  likedUsers: [User!]! @relation(name: "UserLikes")
}
r
so do away with the likes entity
and then just check if the currentUser is among that likedUsers
m
Copy code
allPosts(
    orderBy: createdAt_DESC,
    filter: {
      OR: [{
        user: {
          id_not: $userId
        }
      }, { user: null } ]
    },
    first: $limit
  )
something like this if I understand you correctly
you can get all posts not owned by specific user
and user likes by extra query
then simple check if the post liked by user or not
r
on reactiflux some people suggested using a boolean isLikedByViewer or something along those lines, but I dont understand how you could add a boolean like that and get the value for the currentUser
or maybe I misunderstood or they misunderstood me
but it got me wondering if there was more you could do with currentUser that i wasnt aware of
m
it can be needed only if you have to sort on this
r
not really, I just want a clean way for people to like a post
m
I do same on likedCounts because I have to sort by likedCounts for example
I have function
Copy code
'use latest'
const fromEvent = require('graphcool-lib').fromEvent;


function setLikes(api, postId, likesCount) {

  const request = `
    mutation($styleId: ID!, $likesCount: Int!) {
      updatePost(id: $postId, likesCount: $likesCount) {
        id
        likesCount
      }
    }`

  return api.request(request, { postId, likesCount })
};

module.exports = function (event) {
  const api = fromEvent(event).api('simple/v1')

  if (event.data.Post.updatedFields.indexOf('likesCount') === -1 &&
      event.data.Post.node._likedUsersMeta) {
    event.data.Post.node.likesCount = event.data.Post.node._likedUsersMeta.count;
    return setLikes(api, event.data.Post.node.id, event.data.Post.node._likedUsersMeta.count)
      .then((data) => { return { data: data } });
  }
  return { data: event.data };
}
it works for me as callback function when Post changed and calculates likesCount automatically
r
I see
Im looking more for the simplest way to check if the current user liked the post
but I guess just having a list of users on a post is the best way to do that
m
I just doing it on the frontend
UserLikes is m2m relation between User and Post
UserLikes and LikedUsers
r
do you have a Like entity in your database?
m
no
I think is not needed
r
youre right
its overkill
m
relation 1. works well 2. more secure
and also consistent - if you delete like from user, it automatically deletes from post