我的后端返回以下响应:
const errorMessage = {
errors: [
{
message: 'User is logged out',
},
],
data: null,
};
return res.status(200).json(errorMessage);
我使用中继的本机应用程序返回以下错误:
RelayObservable: Unhandled Error Error:
Relay request for `HomeQuery` failed by the following reasons:
当我尝试查询后端并返回上面的 'errorMessage' graphql 错误数组时,会出现此错误。在引发红屏之前,我无法在中继网络层中发现此错误。我的中继环境如下所示:
const network = new RelayNetworkLayer([
urlMiddleware({
url: () => Promise.resolve(`${config.endpoint}backend`),
headers: async () => authenticateHeaders(fetchHeaders),
}),
retryMiddleware({ fetchTimeout: 1000 }),
next => req => {
if (!req.uploadbles) {
req.Accept = 'application/json';
req['Content-Type'] = 'application/json';
}
return next(req);
},
next => async req => {
const res = await next(req);
if (isLoggedOut(res)) {
// Using react-navigation, route to the login screen
dispatch(routeToLogin())
// I also tried returning `dispatch(routeToLogin())` and `{ data: null, errors: res.payload.errors }` without luck
}
return res;
}
]);
有没有一种方法可以dispatch(routeToLogin())
在返回 graphql 错误时使用而不会看到红屏错误?
编辑 1
对于react-relay-network-modern
:您可以像这样添加 noThrow 选项:new RelayNetworkLayer(middlewares, { noThrow: true });
noThrow 选项不存在react-relay-network-layer
(接力经典),我能做些什么来解决它吗?