根据https://www.howtographql.com/graphql-python/6-error-handling/中的文档,我用来raise GraphQLError
在我的 Flask GraphQL 应用程序变异函数中显示错误,如下所示:
import graphene
from graphql import GraphQLError
from ...extensions import db
from ...models import User as UserModel
from ..types import User as UserType
class Update(graphene.Mutation):
class Input:
id = graphene.ID(required=True)
# phone = graphene.String()
name = graphene.String(required=False, default_value=None)
# active = graphene.Boolean()
Output = UserType
@staticmethod
def mutate(root, info, **kwargs):
user = graphene.Node.get_node_from_global_id(info, kwargs.pop('id'))
# print(info.context)
# if not user:
raise GraphQLError('eeee')
# user.update(**kwargs)
# db.session.commit()
return user
我期望得到类似 400 状态码的东西,带有 graphql 错误 json 模式。但我得到 200 并且异常打印在带有回溯的控制台中。我在这里做错了吗?
An error occurred while resolving field Mutation.updateUser
Traceback (most recent call last):
File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executor.py", line 447, in resolve_or_error
return executor.execute(resolve_fn, source, info, **args)
File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executors/sync.py", line 16, in execute
return fn(*args, **kwargs)
File "/application/schema/mutation/user.py", line 40, in mutate
raise GraphQLError('eeee')
graphql.error.base.GraphQLError: eeee
Traceback (most recent call last):
File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executor.py", line 447, in resolve_or_error
return executor.execute(resolve_fn, source, info, **args)
File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executors/sync.py", line 16, in execute
return fn(*args, **kwargs)
File "/application/schema/mutation/user.py", line 40, in mutate
raise GraphQLError('eeee')
graphql.error.located_error.GraphQLLocatedError: eeee
127.0.0.1 - - [17/Oct/2018 01:46:54] "POST /graphql? HTTP/1.1" 200 -