在我的服务器上,我的 GraphQL 实现使用了 Flask、Graphene 和 SQLAlchemy。理想情况下,我希望能够简单地操作标头并返回 401 错误响应,但 GraphQL 将所有内容都返回为 200。
使用 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 错误,但我可以设法Network
在...中的对象中拦截它们Environment
,基本上看起来像这样。
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 JWT 上剩余的时间以及是否需要刷新。