【问题标题】:Apollo Server has no socket connectionApollo Server 没有套接字连接
【发布时间】: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


    【解决方案1】:

    我切换到apollo-link-ws 修复了它

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2012-12-27
    • 2011-06-29
    • 2011-09-18
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多