删除一条记录,但还要在graphene_django中返回其值

时间:2019-06-23 19:43:19

标签: python graphene-django

我想创建一个变异,该变异通过id删除一条记录,而且还按以下方式在graphene_django中返回记录的值:

mutation {
  deleteActor(id: 5) {
    ok
    actor {
      id
      name
    }
  }
}

我的变异当前正确删除了记录,但返回了错误

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field ActorType.id.",
      "locations": [
        {
          "line": 1,
          "column": 45
        }
      ],
      "path": [
        "deleteActor",
        "actor",
        "id"
      ]
    }
  ],
  "data": {
    "deleteActor": {
      "ok": true,
      "actor": null
    }
  }
}

我不知道为什么会这样,因为根据石墨烯文档,.delete()方法仅从数据库中删除一个项目,而不接触python实例。这是我的突变代码

class DeleteActor(graphene.Mutation):
    class Arguments:
        id = graphene.Int(required=True)

    ok = graphene.Boolean()
    actor = graphene.Field(ActorType)

    @staticmethod
    def mutate(root, info, id):
        ok = False
        actor_instance = Actor.objects.get(pk=id)
        if actor_instance:
            ok = True
            actor_instance.delete()
            return DeleteActor(ok=ok, actor=actor_instance)
        else:
            return DeleteActor(ok=False, actor=None)

如何解决此错误并退回演员?

0 个答案:

没有答案