Я пытаюсь выяснить каскадное удаление в GraphQL.
Я пытаюсь удалить узел типа Question
, но тип QuestionVote
имеет обязательное отношение к Question
.Я ищу способ удалить Question
и все его голоса одновременно.
Мутация для удаления Question
:
type Mutation {
deleteQuestion(where: QuestionWhereUniqueInput!): Question!
}
И его преобразователь (яиспользуя Prisma):
function deleteQuestion(parent, args, context, info) {
const userId = getUserId(context)
return context.db.mutation.deleteQuestion(
{
where: {id: args.id}
},
info,
)
}
Как я могу изменить эту мутацию, чтобы также удалить связанные QuestionVote
узлы?Или я должен добавить отдельную мутацию, которая удаляет один или несколько экземпляров 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,
)
}
Спасибо!