【问题标题】:Unhandled error Network error: Network request failed Error: Network request failed Error: at new ApolloError未处理的错误网络错误:网络请求失败错误:网络请求失败错误:在新的 ApolloError
【发布时间】:2017-12-27 00:00:48
【问题描述】:

当 Internet 连接中断时,我的应用程序 ( react-native ) 中出现此错误。 当我在离线模式下启动应用程序时,初始组件呈现,尝试执行查询,应用程序崩溃。 当我的应用程序与互联网连接时,它可以完美运行。但是我的应用程序在没有时崩溃。我该如何处理这个错误?

ExceptionsManager.js:71“未处理的错误网络错误:网络请求失败错误:网络错误:网络请求在新的 ApolloError 失败”

import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { AsyncStorage } from 'react-native';
import {SubscriptionClient, addGraphQLSubscriptions} from 'subscriptions-transport-ws';

const wsClient = new SubscriptionClient('wss://172.20.32.6:5000', {
  reconnect: true,
  connectionParams: {
    accessToken: 'jka sdhkjashd jkashdjk ashdas'
  }
});

const networkInterface = createNetworkInterface({
   uri: 'http://172.20.32.6:8000/graphql',
   opts: {
    credentials: 'same-origin'
    }
});
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
  networkInterface,
  wsClient
);
const client = new ApolloClient({
  dataIdFromObject: o => o.id,
  networkInterface: networkInterfaceWithSubscriptions

});
networkInterface.use([{
  applyMiddleware(req, next) {
    if (!req.options.headers) {
      req.options.headers = {};  // Create the header object if needed.
    }
    // get the authentication token from local storage if it exists
    AsyncStorage.getItem('sessionToken').then((token) => {
      req.options.headers.Authorization = token ? `${token}` : null;
      next();
    }
    );
  }
}]);
export default client;

【问题讨论】:

    标签: android react-native apollo-client


    【解决方案1】:

    为避免这个非常无益的错误,请在呈现查询结果的组件顶部添加类似这样的内容。

    if (data && data.error) {
        return (
          <View>
            <Text>{JSON.stringify(data.error)}</Text>
          </View>
        )
    }
    

    更多关于处理错误的信息可以在这个 github 问题中找到 https://github.com/apollographql/react-apollo/issues/604

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 2018-01-09
      • 2019-07-06
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2020-11-20
      • 2019-09-16
      相关资源
      最近更新 更多