Reputation: 123
I am trying to use a one to many relationship in Prisma and am confused. My goal is that when creating an athlete it is assigned a team and likewise the team is updated to show all athletes assigned to it. My resolver is shown below.
async function signUpUser(parent, args, { prisma }, info) {
let password = await bcrypt.hash(args.password, 10)
const team = await prisma.team({ id: args.team })
const user = await prisma.createUser({
...args,
password,
team: { connect: { id: team.id } }
})
//create special user based on user type from args
switch(args.userType) {
case "ATHLETE":
createAthlete(user, prisma)
break
case "COACH":
createCoach(user, prisma)
break
case "HEAD_COACH":
createHeadCoach(user, prisma)
break
case "PARENT":
createParent(user, prisma)
break
default:
createAthlete(user, prisma)
}
return {
user
}
}
async function createAthlete(user, prisma) {
const athlete = await prisma.createAthlete({
user: { connect: { id: user.id } }
})
return athlete
}
This code almost works how I would like it. The user is set a team and an athlete. However in my datamodel I want the team they are stored to to show all the athletes that are assigned under that team. I believe I need to use the upsert method but do not know how.
Upvotes: 0
Views: 1737
Reputation: 123
async function createAthlete(user, team, prisma) {
const athlete = await prisma.createAthlete({
user: { connect: { id: user.id } }
})
const updatedTeam = await prisma.updateTeam({
where: { id: team.id },
data: {
athletes: {
connect: {
id: athlete.id,
}
}
},
})
return {athlete, updatedTeam}
}
Upvotes: 1