reactjs - 如何在 React 中为上传和订阅设置 Apollo 客户端?
问题描述
我想用 React 设置一个 graphql 客户端,用于上传文件和处理来自 graphql 服务器的订阅。文件上传和其他查询运行良好。问题在于订阅。我在浏览器控制台中收到以下错误:
WebSocket connection to 'ws://localhost:3001/subscriptions' failed: Connection closed before receiving a handshake response
我用于apollo-upload-client
文件上传和apollo-link-ws
订阅。
我可以看到 subscriptions-transport-ws建议使用createNetworkInterface
,addGraphQLSubscriptions
但这种方法与apollo-upload-client
仅支持createUploadLink
.
我被困住了。请帮忙。
我这样设置我的客户:
import React from 'react';
import ApolloClient from 'apollo-client';
import { ApolloProvider } from 'react-apollo';
import { createUploadLink } from 'apollo-upload-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloLink, Observable, split } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
const cache = new InMemoryCache();
const request = async (operation) => {
const token = localStorage.getItem('token');
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : '',
},
});
};
const httpLink = createUploadLink({ uri: 'http://localhost:3001/graphql' });
// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: 'ws://localhost:3001/subscriptions',
options: {
reconnect: true
},
});
// using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);
const requestLink = new ApolloLink((operation, forward) =>
new Observable((observer) => {
let handle;
Promise.resolve(operation)
.then(oper => request(oper))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));
return () => {
if (handle) handle.unsubscribe();
};
}));
const apolloClient = new ApolloClient({
link: ApolloLink.from([
requestLink,
link,
]),
cache,
});
export const withApolloClient = App => (
<ApolloProvider client={apolloClient}>
<App client={apolloClient} />
</ApolloProvider>
);
export default apolloClient;
解决方案
我正在使用类似的配置,但不是WebSocketLink
从apollo-link-ws
导入,而是从@apollo/client
.
通过该设置,我可以同时进行订阅和上传。
import { WebSocketLink } from "@apollo/client/link/ws";
推荐阅读
- python - Selium 无法找到类元素
- python - ImportError: cannot import name 'doc' from 'pandas.util._decorators' (C:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py)
- python - Can Pytest exclude certain lines of code?
- python - 给定一个对象列表,希望该对象返回它存储到的列表对象
- python - How to sum up pandas columns where the number is determined by the values of another column?
- c++ - Two methods with the same name but in different classes c++
- scala - How to combine 2 future sequences of type Seq[Either[A,B]]?
- r - 我怎样才能建立这个方程来进行约束最大化?
- node.js - 来自 ReactJS 的 Socket.io 连接没有像其他任何一样被 nginx 重定向
- python - Python - 在没有计数功能的列表中出现一个字符