【发布时间】:2022-10-24 20:28:14
【问题描述】:
我可以在graphene.Mutation 的mutate 方法中引发异常,以便在GraphQL 响应中给出"errors" 列表吗?
这曾经在graphene-django 3.0.0b7中工作:
class DeleteOrderlineMutation(graphene.Mutation):
Output = graphene.ID()
class Arguments:
id = graphene.UUID()
order_id = graphene.ID()
def mutate(cls, info, id, order_id):
user = info.context.user
order = Order.objects.for_user(user).get(pk=order_id)
if not order.is_editable_by(user):
raise GraphQLError(
"Order mutation not allowed, Orderline can not be deleted."
)
这将导致如下响应:
{
...
"errors": [
{"message": "Order mutation not allowed, Orderline can not be deleted.", ...}
]
}
但是,在最新的 3.0.0 中,这给出了
{
'data': {
'deleteOrderline': "<Promise at 0x7f899900d5b0 rejected with GraphQLError('Order mutation not allowed, Orderline can not be deleted.')>"
}
}
我搜索了有关如何返回错误的提示的最新文档 (https://docs.graphene-python.org/projects/django/en/latest/),但我没有找到任何东西。
我已经将标签一分为二,它在 3.0.0b8 中停止工作。更改日志 (https://github.com/graphql-python/graphene-django/releases/tag/v3.0.0b8) 没有列出任何立即听起来与此相关的内容。
在标签 b7 和 b8 (https://github.com/graphql-python/graphene-django/compare/v3.0.0b7..v3.0.0b8) 之间的提交中,我发现所有看起来相关的是使用 .formatted 而不是 format_error (https://github.com/graphql-python/graphene-django/pull/1327) 的更改,因为它位于 GraphQLError 的 except 中,但我不确定。
在 GraphQL 响应中获取错误列表的正确方法是什么?可以通过引发异常来完成,还是我们需要以不同的方式来完成?
编辑:我目前正在研究Graphene errors messages 中概述的方法:定义一个ErrorType 类并将其添加到突变定义中。
更新:已解决。我的错误是 mutate() 不是类方法。添加这个使得引发 GraphQLError 像以前一样工作。
【问题讨论】:
标签: graphene-django