简体   繁体   English

如何使用 Graphene-Django Relay 中的外键关系更新模型?

[英]How to update the model with the foreignkey relation in Graphene-Django Relay?

I'm tryting to create the Update mutation of the model that has a foreign key relation.我正在尝试创建具有外键关系的模型的 Update 突变。 I have done everything as per the documentation but it still doesn't work when I provide the foreign Model Input.我已经按照文档完成了所有操作,但是当我提供外部模型输入时它仍然不起作用。

I created the Input with the proper attributes that I should pass in the query but it doesn't work and throws the below shown error.我使用我应该在查询中传递的正确属性创建了输入,但它不起作用并抛出下面显示的错误。 Field 'id' expected a number but got {'id': 2}.字段 'id' 需要一个数字,但得到了 {'id': 2}。

I am unable to understand the reason behind this error.我无法理解此错误背后的原因。 I'm passing the correct input (I believe) could someone, please, help me understand why is this happening?我正在传递正确的输入(我相信)有人可以帮助我理解为什么会这样吗?

Your suggestions and inputs are much appreciated.非常感谢您的建议和意见。

The inputs:输入:

class FuelTypeInput(graphene.InputObjectType):
  # id of the FuelTypeModel
  id = graphene.Int()
  label = graphene.String()

class FuelSubtypeInput(graphene.InputObjectType):
  # Graphene ID
  id = graphene.ID()
  label = graphene.String()
  fuel_type = graphene.Field(FuelTypeInput)

The Update mutation:更新突变:

class UpdateFuelSubType(relay.ClientIDMutation):
  class Input:
    id = Int() # id of the Fuel SubTypeModel
    input = FuelSubtypeInput(required=True)

  ok = True
  fuel_subtype = Field(FuelSubTypeNode)

  def mutate_and_get_payload(root, info, id, input):

    ok = False

    if FuelSubType.objects.filter(pk=id).update(**input):
      fuel_subtype = FuelSubType.objects.get(pk=id)
      ok = True

      return UpdateFuelSubType(fuel_subtype=fuel_subtype)

    return UpdateFuelSubType(fuel_subtype=None)

The mutation query at the Client:客户端的变异查询:

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label", 
                    fuelType: { id: 2 }
                }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}

Final result:最后结果:

{
  "errors": [
    {
      "message": "Field 'id' expected a number but got {'id': 2}.",
      "locations": [
        {
          "line": 48,
          "column": 5
        }
      ],
      "path": [
        "updateFuelSubtype"
      ]
    }
  ],
  "data": {
    "updateFuelSubtype": null
  }
}

I would also like to mention that when I remove the fuelType input from the query, everything works fine, eg:我还想提一下,当我从查询中删除fuelType输入时,一切正常,例如:

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label" }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}

Simply sending the primary key of your foreign key in the mutation query will work.只需在变异查询中发送外键的主键即可。 Instead of代替

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label", 
                    fuelType: { id: 2 }
                }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}

send发送

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label", 
                    fuelType: 2 # ## <--- change
                }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM