Neo Lambada
12/21/2021, 2:49 PMconst followedUser = await context.prisma.followUser.upsert({
where: { followerId: 4, followingId: args.userId },
update: { status: args.status },
})
Maciek K
12/21/2021, 3:15 PMconst followedUser = await context.prisma.followUser.upsert({
where: { followerId: 4, followingId: args.userId },
update: { status: args.status },
create: {
followerId: 4,
followingId: args.userId,
status: args.status
}
})
Neo Lambada
12/21/2021, 5:15 PMNeo Lambada
12/21/2021, 5:22 PMNeo Lambada
12/21/2021, 5:25 PMNeo Lambada
12/21/2021, 5:26 PMMaciek K
12/21/2021, 5:34 PMupsert
works like findUnique
So you only can use one variable in the where clause to find an unique record.
So you can remove on of those ids in the where clause:
const followedUser = await context.prisma.followUser.upsert({
where: { followingId: args.userId },
update: { status: args.status },
create: {
followerId: 4,
followingId: args.userId,
status: args.status
}
})
That's beacase if you have followerId
on FollowUser marked as @unique
there will be no other records in that table with that same followerId
. The same goes for followingId
Neo Lambada
12/21/2021, 5:42 PMMaciek K
12/21/2021, 6:06 PMNeo Lambada
12/21/2021, 6:26 PMNeo Lambada
12/21/2021, 6:26 PMNeo Lambada
12/21/2021, 6:28 PMMaciek K
12/21/2021, 6:32 PM@@unique(fields: [followerId, followingId], name: "myCompoundUniqueName");
const followedUser = await context.prisma.followUser.upsert({
where: { myCompoundUniqueName: { followerId: 4, followingId: args.userId } },
update: { status: args.status },
create: {
followerId: 4,
followingId: args.userId,
status: args.status
}
})
Maciek K
12/21/2021, 6:32 PMMaciek K
12/21/2021, 6:37 PMfollowerId_followingId
Maciek K
12/21/2021, 6:47 PM@unique
attributes from followerId
and followingId
. Because that way if you create one FollowUser with a followerId
, then you won't be allowed to create more in this table with that followerId
. The same goes for followingId
.Neo Lambada
12/21/2021, 7:36 PMNeo Lambada
12/21/2021, 7:36 PMMaciek K
12/21/2021, 7:42 PMMaciek K
12/21/2021, 7:43 PMMaciek K
12/21/2021, 7:43 PMconst followedUser = await context.prisma.followUser.upsert({
where: { followerId_followingId: { followerId: 4, followingId: args.userId } },
update: { status: args.status },
create: {
followerId: 4,
followingId: args.userId,
status: args.status
}
})
Neo Lambada
12/21/2021, 7:43 PMNeo Lambada
12/21/2021, 7:44 PMNeo Lambada
12/21/2021, 7:44 PMMaciek K
12/21/2021, 7:44 PMNeo Lambada
12/21/2021, 7:45 PMNeo Lambada
12/21/2021, 7:45 PMMaciek K
12/21/2021, 7:47 PMNeo Lambada
12/21/2021, 7:52 PMNeo Lambada
12/21/2021, 7:52 PMconst followingUsers = await context.prisma.user.findMany({
include: { following: true },
skip: args.offset || undefined,
take: args.limit || undefined,
})
Neo Lambada
12/21/2021, 7:52 PMNeo Lambada
12/21/2021, 7:53 PMMaciek K
12/21/2021, 8:02 PMNeo Lambada
12/21/2021, 8:06 PMNeo Lambada
12/21/2021, 8:07 PMNeo Lambada
12/21/2021, 8:08 PMNeo Lambada
12/21/2021, 8:08 PMNeo Lambada
12/21/2021, 8:09 PMNeo Lambada
12/21/2021, 8:09 PMNeo Lambada
12/21/2021, 8:09 PMNeo Lambada
12/21/2021, 8:10 PMMaciek K
12/21/2021, 8:30 PMawait context.prisma.user.findMany({
where: {
OR: [
{ followedBy: { followingId: authorizedUserId },
{ following: { followerId: authorizedUserId },
],
}
});
That will get you all followers and all following in one query.Maciek K
12/21/2021, 8:30 PMMaciek K
12/21/2021, 9:07 PMawait context.prisma.user.findUnique({
where: {
id: authorizedUserId,
},
include: {
followers: {
include: {
follower: true,
},
},
following: {
include: {
following: true,
}
}
}
})
Maciek K
12/21/2021, 9:29 PMquery USER_WITH_RELATIONS($id: ID!) {
user(id: $id) {
id
// firstName
// lastName
followedBy {
follower {
id
// firstName
// lastName
}
}
following {
following {
id
// firstName
// lastName
}
}
}
}
Neo Lambada
12/22/2021, 7:32 AMNeo Lambada
12/22/2021, 7:43 AMMaciek K
12/22/2021, 8:39 AMcreatedAt
. But you don't need that. If you still want to have the status then you would want to just add it to the where clause. And if you want to query the followUser table:
const followersAndFollowing = await context.prisma.followUser.findMany({
where: {
status: true,
OR: [
{followingId: authenticatedUserId },
{followerId: authenticatedUserId}
]
},
include: {
follower: true,
following: true,
}
})
Altought that way you will probably need to map that array later to get a list of followers and followings.
You would want to split that to two queries: for followers and followings.
const followers = await context.prisma.followUser.findMany({
where: {
status: true,
followingId: authenticatedUserId
},
include: {
follower: true,
}
})
Add the pagination if you want. Hope it helps.prisma rainbow