2

GraphQLでカスケード削除を理解しようとしています。

タイプ のノードを削除しようとしていますがQuestion、タイプQuestionVoteには との関係が必要ですQuestionQuestionaとそのすべての投票を一度に削除する方法を探しています。

を削除するためのミューテーション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,
  )
}

ありがとう!

4

1 に答える 1