【问题标题】:Cascade delete related nodes using GraphQL and Prisma使用 GraphQL 和 Prisma 级联删除相关节点
【发布时间】:2018-11-02 23:27:15
【问题描述】:

我正在尝试找出 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 的一个或多个实例?

如果它很重要,这里是创建 QuestionQuestionVote 的突变:

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,
  )
}

谢谢!

【问题讨论】:

    标签: graphql cascading-deletes prisma


    【解决方案1】:

    您可以通过修改数据模型来设置级联删除。

    鉴于您的问题,我假设您的数据模型看起来有点像这样:

    type Question {
      id: ID! @unique
      votes: [QuestionVote!]! @relation(name: "QuestionVotes")
      text: String!
    }
    
    type QuestionVote {
      id: ID! @unique
      question: Question @relation(name: "QuestionVotes")
      isUpvote: Boolean!
    }
    

    然后你必须像这样将onCascade: DELETE 字段添加到@relation 指令中:

    type Question {
      id: ID! @unique
      votes: [QuestionVote!]! @relation(name: "QuestionVotes" onDelete: CASCADE)
      text: String!
    }
    
    type QuestionVote {
      id: ID! @unique
      question: Question @relation(name: "QuestionVotes")
      isUpvote: Boolean!
    }
    

    现在,每删除一个Question 节点,所有相关的QuestionVote 节点也会被删除。

    注意:如果省略onDelete,则默认值自动设置为onDelete: SET_NULL。这意味着删除节点会导致将关系的另一端设置为null

    您可以在 Prisma in the documentation 中阅读有关级联删除的更多信息。

    【讨论】:

      猜你喜欢
      • 2018-02-19
      • 2011-10-12
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多