graphql订阅无法在弹性beantalk上连接到websocket端点

问题描述

我有使用Application Load Balancer在Elastic Beanstalk上运行的graphql-yoga(GraphQL服务器)应用程序。我能够查询和变异。但是,当我尝试使用GraphQL游乐场订阅时,它没有出现以下错误。 { “错误”:“无法连接到WebSocket端点wss://domainname.com/subscriptions。请检查端点URL是否正确。” }

我检查了该应用程序是否在本地以及Heroku dyno中运行良好。 我检查了AWS CloudWatch日志。使用[0mGET / subscriptions [33m404 [0m3.860 ms-152 [0m

解决方法

我实际上遇到了同样的问题,发现它与服务器端口的启动方式以及如何在FE上配置Apollo Provider有关。注意“ ws”不起作用,它也需要“ wss”为我工作。我没有像使用“ / subscriptions”那样将任何内容定向到特定的URL。相反,我只是使用Heroku / localhost提供的任何URI

服务器GraphQL服务器代码:

const server = new GraphQLServer({
  typeDefs,resolvers,context: { pubsub,endpoint: "/" },});

服务器起始代码:

server.start(({ port }) => {
  console.log(`Server listening on ${port} for incoming requests`);
});

FE Apollo提供程序配置

const httpLink = new HttpLink({
  uri: "https://domainName.herokuapp.com,});

const wsLink = new WebSocketLink({
  uri: "wss://domainName.herokuapp.com/",options: {
    reconnect: true,},});

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

const client = new ApolloClient({
  link: splitLink,uri: "https://domainName.herokuapp.com",cache: new InMemoryCache({
    addTypename: false,}),});

希望这对您有所帮助,并使您不断前进。花了半天时间弄清楚自己。我认为对您来说可能是相同的情况,但您的情况稍有不同。编码愉快!

,

我发现与Elastic Beanstalk相连的EC2实例具有默认的Nginx配置。 我已经用

手动更改了nginx.conf
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

它奏效了。More Info