2017-08-08 114 views
1

在我的服务器上,我的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的剩余时间,以及是否需要更新。

回答

0

您可以处理在onCompleted回调服务器errros:https://github.com/facebook/relay/pull/1938/files

+0

如何将错误对象使用这个'onCompleted'callback?它是否适用于Relay Modern?它是否也可用于查询? – Mendes

+0

它在Relay Modern上可用。我无法描述比文档更多的用法。这是为了突变。当我回答这个问题时,这就是我错过的观点。 – tkymtk

0

我希望我正确地理解你的问题,咬你的QueryRenderer应该有conains错误 https://facebook.github.io/relay/docs/query-renderer.html

render={({error, props}) => { 
if (error) { 
    return <div>{error.message}</div>; 
} else if (props) { 
    return <div>{props.page.name} is great!</div>; 
} 
return <div>Loading</div>; 
+0

这些错误假定应用程序没有失败。事实证明,我并不需要处理错误信息,但仅供参考,它指的是网络级别的环境错误,这通常会导致应用程序发生严重故障。不幸的是,我不再有片段来展示。 –

+1

确定这很有趣,所以QueryRender错误只在某些情况下触发,并且来自网络层的一些错误不会传播到QueryRenderer?那很有意思。谢谢(你的)信息。 – liminal18

+0

只有当GraphQL有效载荷数据为空时,才会呈现'QueryRenderer'错误,否则它将吞下它.... – Mendes