Django 如何使用GraphQL、Graphene和Relay对现有数据进行变异?

Django 如何使用GraphQL、Graphene和Relay对现有数据进行变异?,django,graphql,relay,graphene-python,Django,Graphql,Relay,Graphene Python,我觉得它应该被记录在某个地方,但我找不到它。我可以添加数据到我的数据库与突变,但我不能改变它 Graphene与Django一起工作,我的db是SQLite 这很具体,我希望有人知道答案。除其他事项外,我尝试了: mutation AppMutation( $input: AddSchoolNodeInput! ) { addSchool(input: $input) { school { id name descrip

我觉得它应该被记录在某个地方,但我找不到它。我可以添加数据到我的数据库与突变,但我不能改变它

Graphene与Django一起工作,我的db是SQLite

这很具体,我希望有人知道答案。除其他事项外,我尝试了:

mutation AppMutation(
  $input: AddSchoolNodeInput!
  ) {
    addSchool(input: $input) {
      school {
        id
        name
        description
     }     
    }
  }
在哪里添加学校nodeInput!is U2Nob29sTm9kZToxMQ==是现有id:

{
  "name": "edit kaas", 
  "description": "edit fails",
  "clientMutationId":"U2Nob29sTm9kZToxMQ=="
 } 
它将创建一个新项并返回以下内容:

{
  "data": {
    "addSchool": {
      "school": {
        "id": "U2Nob29sTm9kZToxNA==",
        "name": "edit kaas",
        "description": "edit fails"
      }
    }
  }
}

我能够通过向上述突变提供ID并在模式中执行此操作来编辑现有学校:

class AddSchool(relay.ClientIDMutation):
    class Input:
        name = String()
        description = String()
        id = String()

    school = Field(SchoolNode)

    @classmethod
    def mutate_and_get_payload(cls, input, context, info):
        name = input.get("name")
        description = input.get("description")
        id = input.get("id")

        if id:
            id = from_global_id(id)[1]
            school = School.objects.get(id=id)
            if name:
                school.name = name
            if description:
                school.description = description

        else:
            school = School(
                name=name,
                description=description
            )

        school.save()

        return AddSchool(school=school)
它获取ID,通过if语句将其放入,然后使用from_global_ID将其从graphene ID传输到django ID,然后从db获取它。如果它能找到它,就会发生错误,因此只有在没有提供id的情况下才会创建新学校


不确定其最佳实践是否有效。

我同意您的观点,即缺乏关于一般突变的文档。出于这个原因,我在这里的链接中记录了我的操作方法:我不确定这是否是一种进行突变的好方法,但它对我也适用。如果我想在Flase:return{message:youcannotthisrequest}时执行该怎么办?我可以用JsonResponse吗?