返回 graphql 错误时,如何捕获 RelayObervable Unhandled 错误?

How can I catch RelayObervable Unhandled error when graphql errors are returned?

我的后端返回以下响应:

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:

当我尝试查询后端时出现此错误,它 returns 上面的 '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;
  }
  ]);

有没有一种方法可以在返回 graphql 错误时使用 dispatch(routeToLogin()) 导航而不会看到红屏错误?

编辑 1

对于 react-relay-network-modern:您可以像这样添加 noThrow 选项:new RelayNetworkLayer(middlewares, { noThrow: true });

react-relay-network-layer(经典接力赛)不存在 noThrow 选项,我能做些什么来解决它吗?

请尝试noThrow选项:

const network = new RelayNetworkLayer(middlewares, { noThrow: true });