Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs “如何解决阿波罗订阅问题”;WebSocket握手时出错:意外响应代码:400“;_Reactjs_Websocket_React Apollo_Apollo Client_Apollo Link - Fatal编程技术网

Reactjs “如何解决阿波罗订阅问题”;WebSocket握手时出错:意外响应代码:400“;

Reactjs “如何解决阿波罗订阅问题”;WebSocket握手时出错:意外响应代码:400“;,reactjs,websocket,react-apollo,apollo-client,apollo-link,Reactjs,Websocket,React Apollo,Apollo Client,Apollo Link,我有一个使用React并订阅GraphQL后端的遗留应用程序。今天,我升级了我所有的软件包,尤其是Those(我支持它们是我问题的根源,但我不确定): react apollo从^2.4.0到^3.1.3 apollo link ws从^1.0.14到^1.0.19 工作守则如下: import {ApolloClient} from 'apollo-client'; import {HttpLink} from 'apollo-link-http'; import {WebSocketLi

我有一个使用React并订阅GraphQL后端的遗留应用程序。今天,我升级了我所有的软件包,尤其是Those(我支持它们是我问题的根源,但我不确定):

  • react apollo
    ^2.4.0^3.1.3

  • apollo link ws
    ^1.0.14^1.0.19

工作守则如下:

import {ApolloClient} from 'apollo-client';
import {HttpLink} from 'apollo-link-http';
import {WebSocketLink} from 'apollo-link-ws';
// Other imports....

const wsLink = new WebSocketLink({
  uri: `ws://localhost:3000/graphql`,
  options: {
    reconnect: true,
  },
});

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

const link = ApolloLink.from([
  onError(({graphQLErrors, networkError}) => {
    // Handle graphQLErrors and networkError
  }),
  withClientState({
    //...
    cache
  }),
  splittedLink
]);

export default new ApolloClient({
  link,
  cache
});
因此,这是上所述的基本代码,但升级后我出现了此错误(在网络中查看graphQL响应时):

WebSocket握手期间出错:意外响应代码:400

在我的开发终端中,我看到一个无限的日志,上面写着:

备注

在我的
setupProxy.js
中,我将
ws
设置为true:

const proxy = require('http-proxy-middleware');

module.exports = app => {
  app.use(proxy('/graphql', {
    target: 'ws://my-backend-url:my-backend-port',
    changeOrigin: true,
    ws: true,
  }));
};
对此有何反馈或经验