【问题标题】:Apollo-Client with subscriptions: failed: Error during WebSocket handshake: Unexpected response code: 400带有订阅的 Apollo 客户端:失败:WebSocket 握手期间出错:意外响应代码:400
【发布时间】:2020-05-20 19:55:42
【问题描述】:

我有一个带有 nestjs + graphql + subscription 服务器的 reactjs 应用程序。

我已经在服务器上成功设置了 graphql+订阅。我可以通过交互式游乐场界面对其进行测试。服务器端一切正常。

现在我一直在尝试在客户端上设置 graphql 订阅,但没有成功。我看过无数的教程和类似(甚至相同)的 github 问题。这是我的客户端设置。

我不知道我做错了什么:

import React from 'react';
import { ApolloProvider } from '@apollo/react-hooks';
import { ApolloClient } from 'apollo-client';
import { getMainDefinition } from 'apollo-utilities';
import { split } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { IntrospectionFragmentMatcher, InMemoryCache } from 'apollo-cache-inmemory';


// this is a factory function I'm using to create a wrapper for the whole application

const ApolloProviderFactory = (props) => {

  const { graphQlUrl, introspectData } = props;

  const httpLink = new HttpLink({
    uri:`http://${graphQlUrl}`,
  });

  const wsLink = new WebSocketLink({
    uri: `ws://${graphQlUrl}`,
    options: {
      reconnect: true,
    },
  });

  const link = split(
    ({ query }) => {
      const definition = getMainDefinition(query);
      return (
        definition.kind === 'OperationDefinition'
        && definition.operation === 'subscription'
      );
    },
    wsLink,
    httpLink,
  );

  const fragmentMatcher = new IntrospectionFragmentMatcher({
    introspectionQueryResultData: introspectData,
  });
  const cache = new InMemoryCache({ fragmentMatcher });

  const client = new ApolloClient({
    link,
    cache,
  });

  // this component will wrap the whole application
  const ApolloWrapper = ({ children }: ApolloWrapperProps) => (
    <ApolloProvider client={client}>{children}</ApolloProvider>
  );
  return ApolloWrapper;
}

这是我在运行应用程序时遇到的错误:

任何帮助将不胜感激。

【问题讨论】:

  • props 中定义的端口?将请求详细信息与工作中的一个(游乐场)进行比较
  • 好的。我让它工作了。在这篇文章github.com/apollographql/apollo-client/issues/3109 之后,尤其是最后一个建议,我禁用了库pace.js,它按预期工作。不过感谢您的帮助

标签: reactjs graphql nestjs subscription apollo-client


【解决方案1】:

我有 pace.js 库,它以某种方式干扰了整个套接字。在我禁用它之后,一切都按预期工作。

【讨论】:

  • 你是从哪里删除的?节点模块?我遇到了完全相同的错误,但没有找到pace.js
  • pace.js 是我安装的一个小型库 (npm pace.js)。所以我只需要删除它。如果您不使用它,那么它通常不在您的 node_modules 或应用程序中
猜你喜欢
  • 1970-01-01
  • 2019-06-25
  • 2019-08-05
  • 2015-12-26
  • 2017-06-04
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
相关资源
最近更新 更多