【问题标题】:Best way to do a delete operation with GraphQL + graphene使用 GraphQL + 石墨烯进行删除操作的最佳方法
【发布时间】:2019-05-04 22:33:50
【问题描述】:

我正在努力让我的删除操作正常工作。 我的 Create、Read 和 Update 工作正常,但 Delete 没有返回任何内容。

class DeleteEmployeeInput(graphene.InputObjectType):
    """Arguments to delete an employee."""
    id = graphene.ID(required=True, description="Global Id of the employee.")


class DeleteEmployee(graphene.Mutation):
    """Delete an employee."""
    employee = graphene.Field(
        lambda: Employee, description="Employee deleted by this mutation.")

    class Arguments:
        input = DeleteEmployeeInput(required=True)

    def mutate(self, info, input):
        data = utils.input_to_dictionary(input)
        #data['edited'] = datetime.utcnow()

        employee = db_session.query(
            EmployeeModel).filter_by(id=data['id'])
        employee.delete(data['id'])
        db_session.commit()
        #employee = db_session.query(
            #EmployeeModel).filter_by(id=data['id']).first()

        #return DeleteEmployee(employee=employee)

删除条目的最佳方法是什么? 我假设我必须返回 OK 或 Error。

当我运行我的突变时:

mutation {
  deleteEmployee (input: {
       id: "RW1wbG95ZWU6MQ=="
  }) 
}

我收到错误Field \"deleteEmployee\" of type \"DeleteEmployee\" must have a sub selection."

注意注释掉的行

【问题讨论】:

    标签: graphql graphene-python


    【解决方案1】:

    尝试将employee = graphene.Field... 替换为ok = graphene.Boolean(),然后将您的变异方法的最后一行设为return DeleteEmployee(ok=True)

    【讨论】:

    • 效果很好。您可能已经注意到,我对 python 比较陌生。返回行return DeleteEmployee(ok=True) 中究竟发生了什么?它返回同一类 DeleteEmployee 的新实例? ok=True 是传递给graphene.Mutation 类的构造函数的kwarg?
    • mutate 方法的最后一行创建了一个作为突变输出的对象。如果mutate 方法始终显示为类方法,那么在文档中会更有意义。
    • 我还是有点迷茫,但我想我必须去参加一些 101 udemy 课程。例如行input = DeleteEmployeeInput(required=True)DeleteEmployeeInput 本身没有(可见)变量required。放在super() 类graphene.InputObjectType?
    • DeleteEmployeeInput 定义了输入的形状,而在class Arguments 内部,您定义了DeleteEmployee 与其输入之间的关系,这就是required 出现的原因。
    • 所以required 是python 约定而不是石墨烯变量?因为我没有看到变量required 是在哪里创建的
    猜你喜欢
    • 2017-05-11
    • 2017-05-13
    • 2020-07-27
    • 2018-03-11
    • 2020-04-22
    • 2018-03-06
    • 2020-09-19
    • 2018-03-22
    • 2019-08-26
    相关资源
    最近更新 更多