【问题标题】:graphql subscription Could not connect to websocket endpoint at elastic beanstalkgraphql 订阅无法连接到弹性 beantalk 的 websocket 端点
【发布时间】:2021-01-08 05:59:28
【问题描述】:

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

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

] 记录请求

【问题讨论】:

    标签: amazon-ec2 graphql amazon-elastic-beanstalk aws-application-load-balancer graphql-subscriptions


    【解决方案1】:

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

    服务器 GraphQLServer 代码:

    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,
      }),
    });
    

    希望这可以帮助您并让您继续前进。花了半天时间弄清楚自己。我认为这对您来说可能是相同的情况,只是您的情况略有不同。编码愉快!!!

    【讨论】:

      【解决方案2】:

      我发现附有 Elastic Beanstalk 的 EC2 实例具有默认的 Nginx 配置。 我已经手动更改了 nginx.conf 与

      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      

      它成功了。More Info

      【讨论】:

      • 我试过了,但没有用,你能告诉我整个确切的设置吗?
      猜你喜欢
      • 2022-08-07
      • 2021-09-11
      • 2018-08-24
      • 2019-12-11
      • 2021-09-12
      • 2023-04-03
      • 2018-03-31
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多