在我的服务器上,我的GraphQL实现使用Flask,Graphene和SQLAlchemy。理想情况下,我想能够简单地操纵头和返回401错误响应,但GraphQL返回的一切,200如何在Relay中捕获GraphQL错误消息?
使用flask.abort(401),但是我至少能得到这样的响应:
...
"errors": [
{
"message": "401 Unauthorized: The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.",
"locations": [
{
"line": 11,
"column": 3
}
]
}
]
...
我认为这是一种妥协,我可以在这个时间点与之合作。然而;因为没有东西可以是那么简单......我不确定我如何抓住这个错误信息。我读过QueryRenderer
本身可能会导致这些GraphQL错误的问题,但我可以设法在Environment
中的Network
对象中拦截它们......本质上看起来像这样。
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: {…}}
__proto__: Promise[
[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object
data: {viewer: {…}}
errors: Array(4)
0: {locations: Array(1), message: "401 Unauthorized: The server could not verify that…nderstand how to supply the credentials required."}
1: {locations: Array(1), message: "401 Unauthorized: The server could not verify that…nderstand how to supply the credentials required."}
2: {locations: Array(1), message: "401 Unauthorized: The server could not verify that…nderstand how to supply the credentials required."}
3: {locations: Array(1), message: "401 Unauthorized: The server could not verify that…nderstand how to supply the credentials required."}
length: 4
__proto__: Array(0)
__proto__:Object
我真的不觉得在我的环境的网络层处理这个错误将是正确的方式来管理这个。 QueryRenderer似乎是最有意义的...我已经将它设置为实际的单一来源。
如果不是很明显,我正在使用Relay Modern。因此,基于Relay Classic的任何解决方案都不适用。
编辑:抛开错误信息,我的动机是正确处理JWT令牌。我认为我正在寻找的解决方案与处理这些错误响应无关,而是扩大了我对智威汤逊的理解。
我不知道,我的客户就可以轻松地使用包到JWT解码如jwt-decode
,然后让我访问过期信息......最终我预见导致我某种形式的中间件实现的这将评估JWT的剩余时间,以及是否需要更新。
如何将错误对象使用这个'onCompleted'callback?它是否适用于Relay Modern?它是否也可用于查询? – Mendes
它在Relay Modern上可用。我无法描述比文档更多的用法。这是为了突变。当我回答这个问题时,这就是我错过的观点。 – tkymtk