【发布时间】: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