hey guys, this is less a graph.cool question but a...
# prisma-whats-new
r
hey guys, this is less a graph.cool question but a bit more general, if you would indulge me. I'm making an app with a bidirectional friendship feature. Like facebook/instagram. Would someone be able to tell me what the best practice is on database level? A friendship table, with 2 rows per friendship? And a status like 'accepted', deleted, sent, ...
a
Why would you need two rows per friendship? One would suffice, with two User fields and additional metadata.
r
But how would you take care of querying, or friend requests
a
For a friend request, you would create a node with the Friendship Type, and set the status to
requested
. If you would call the two User fields 'from' and 'to', then incoming friend requests are all Friendship nodes with 'to' == currentUser && status 'requested', for example
j
You need something like this.
Copy code
enum FriendshipStatus {
  PENDING
  ACCEPTED
  BLOCKED
}

type Friendship implements Node {
  from: User @relation(name: "FriendshipOnUser")
  id: ID! @isUnique
  status: FriendshipStatus!
  to: User @relation(name: "FriendshipOnUser1")
}

type User implements Node {
  firstName: String!
  id: ID! @isUnique
  requestedFriends: [Friendship!]! @relation(name: "FriendshipOnUser")
  targetedFriends: [Friendship!]! @relation(name: "FriendshipOnUser1")
}
Then, you can query the current user friendships like this, where
$userId
is the current logged in user ID.
Copy code
query FriendshipsForCurrentUser($userId: ID!) {
  allFriendships(filter: {OR: [{from: {id: $userId}}, {to: {id: $userId}}], status: PENDING}) {
    id
    status
    from {
      firstName
    }
    to {
      firstName
    }
  }
}
r
ah, so you query both fields
thats why I thought 2 rows would be better, so you just need to query one column
but if you then want to check if a specific other user is a friend of the currentUser, you have to check if either the currentUser OR the other user is one of the two columns, to or from
that makes for some very convoluted queries, no?
a
Convulted queries over redundant data I would say...
r
but so if I query for a user's profile, I need to check both the friendshipsTo and friendshipsFrom relation. Since the UI would be different if I sent it, or they sent a request to me. Im having trouble to making the query look clean and handle the data
but ill figure it out
in any case, thanks for the heads up!