GraphQLでカスケード削除を理解しようとしています。
タイプ のノードを削除しようとしていますがQuestion
、タイプQuestionVote
には との関係が必要ですQuestion
。Question
aとそのすべての投票を一度に削除する方法を探しています。
を削除するためのミューテーションQuestion
:
type Mutation {
deleteQuestion(where: QuestionWhereUniqueInput!): Question!
}
そしてそのリゾルバー(私はプリズマを使用しています):
function deleteQuestion(parent, args, context, info) {
const userId = getUserId(context)
return context.db.mutation.deleteQuestion(
{
where: {id: args.id}
},
info,
)
}
QuestionVote
そのミューテーションを変更して、関連するノードも削除するにはどうすればよいですか? または、 の 1 つまたは複数のインスタンスを削除する別のミューテーションを追加する必要がありQuestionVote
ますか?
重要な場合に備えて、 と を作成するミューテーションを次に示しQuestion
ますQuestionVote
。
function createQuestion(parent, args, context, info) {
const userId = getUserId(context)
return context.db.mutation.createQuestion(
{
data: {
content: args.content,
postedBy: { connect: { id: userId } },
},
},
info,
)
}
async function voteOnQuestion(parent, args, context, info) {
const userId = getUserId(context)
const questionExists = await context.db.exists.QuestionVote({
user: { id: userId },
question: { id: args.questionId },
})
if (questionExists) {
throw new Error(`Already voted for question: ${args.questionId}`)
}
return context.db.mutation.createQuestionVote(
{
data: {
user: { connect: { id: userId } },
question: { connect: { id: args.questionId } },
},
},
info,
)
}
ありがとう!