如何在apollo-client 2.0中处理订阅,applyBatchAfterware和applyBatchMiddleware?
我想将以下代码(我目前在apollo-client 1.9.2中使用)转换为符合apollo-client 2.0标准:
const logErrors = {
applyBatchAfterware({ response }, next) {
if (!response) return next();
if (!response.ok) {
response.clone().text().then((bodyText) => {
console.log('...', `Network Error: ${response.status} (${response.statusText}) - ${bodyText}`);
next();
});
} else {
response.clone().json().then(({ errors }) => {
if (errors) {
console.log('...', 'GraphQL Errors:', errors.map(e => e.message));
}
next();
});
}
},
};
const authMiddleware = {
applyBatchMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {};
}
getStoredState({ storage: localForage }, (err, state = {}) => {
req.options.headers['authorization'] = state.auth ? `Bearer ${state.auth.token}` : null;
next();
});
}
}
const wsClient = new SubscriptionClient(SubscriptionClient_URL, {
reconnect: true,
});
networkInterface.use([authMiddleware]);
if (process.env.NODE_ENV !== 'production') {
networkInterface.useAfter([logErrors]);
}
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient
);
const client = new ApolloClient({
networkInterface: networkInterfaceWithSubscriptions,
dataIdFromObject: (o) => o.id,
addTypeName: true,
});
networkInterface.setClient(client);
export default client;