【发布时间】:2021-12-24 03:11:01
【问题描述】:
我有一个使用 Type-GraphQL + Apollo 运行的 GraphQL 服务器,所以我想使用订阅。
对于前端,我使用的是 NextJS + Apollos 客户端,但出现以下错误:
WebSocket connection to: 'ws://localhost:8080/graphql' failed: SubscriptionClient.connect @ client.js?2ed1:427 eval @ client.js?2ed1:396
我在前端客户端中的代码如下所示:
const wsLink = process.browser ? new WebSocketLink({ // if you instantiate in the server, the error will be thrown
uri: `ws://localhost:8080/graphql`,
options: {
reconnect: true
}
}) : null;
const httplink = new HttpLink({
uri: 'http://localhost:8080/graphql',
credentials: "include"
});
const link = process.browser ? split( //only create the split in the browser
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
wsLink,
httplink,
) : httplink;
const client = new ApolloClient({
link,
fetch,
credentials: "include",
cache: new InMemoryCache(),
});
export default client;
【问题讨论】:
标签: next.js apollo apollo-client apollo-server typegraphql