使用 GraphQL + 石墨烯进行删除操作的最佳方式
Best way to do a delete operation with GraphQL + graphene
我正在努力让我的删除操作正常工作。
我的创建、读取和更新工作正常,但删除与 return.
无关
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)
删除条目的最佳方法是什么?
我假设我必须 return OK 或 Error。
当我 运行 我的突变:
mutation {
deleteEmployee (input: {
id: "RW1wbG95ZWU6MQ=="
})
}
我收到错误 Field \"deleteEmployee\" of type \"DeleteEmployee\" must have a sub selection."
注意注释掉的行
尝试将 employee = graphene.Field...
替换为 ok = graphene.Boolean()
,然后将 mutate 方法的最后一行设为 return DeleteEmployee(ok=True)
您的 mutate 方法将类似于:
def mutate(self, info, input):
... skipping deletion code ...
db_session.commit()
return DeleteEmployee(ok=True)
我正在努力让我的删除操作正常工作。 我的创建、读取和更新工作正常,但删除与 return.
无关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)
删除条目的最佳方法是什么? 我假设我必须 return OK 或 Error。
当我 运行 我的突变:
mutation {
deleteEmployee (input: {
id: "RW1wbG95ZWU6MQ=="
})
}
我收到错误 Field \"deleteEmployee\" of type \"DeleteEmployee\" must have a sub selection."
注意注释掉的行
尝试将 employee = graphene.Field...
替换为 ok = graphene.Boolean()
,然后将 mutate 方法的最后一行设为 return DeleteEmployee(ok=True)
您的 mutate 方法将类似于:
def mutate(self, info, input):
... skipping deletion code ...
db_session.commit()
return DeleteEmployee(ok=True)