heroku 为什么我的GraphQL订阅在本地服务器上工作,但在Live服务器上部署时却不能工作

vxqlmq5t  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在构建一个GraphQL服务器,并在其中订阅。一旦我在本地机器上部署服务器并检查Playground中的订阅,它就可以工作了,即。它会监听事件,我会得到新添加的数据。这意味着订阅实现是正确的,没有任何问题。当我在本地(即localhost)上运行时,订阅工作正常,但当我在live(Heroku)上部署服务器时,当我在Playground中侦听订阅时,它给出以下错误:

{
  "error": "Could not connect to websocket endpoint wss://foodesk.herokuapp.com/graphql. Please check if the endpoint url is correct."
}

只是为了更多的信息,我的查询和突变也在现场服务器上工作,只是订阅不工作。
这是我的代码:

/**
 * third party libraries
 */
const bodyParser = require('body-parser');
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const helmet = require('helmet');
const http = require('http');
const mapRoutes = require('express-routes-mapper');
const mkdirp = require('mkdirp');
const shortid = require('shortid');
/**
 * server configuration
 */
const config = require('../config/');
const auth = require('./policies/auth.policy');
const dbService = require('./services/db.service');
const { schema } = require('./graphql');

// environment: development, testing, production
const environment = process.env.NODE_ENV;

const graphQLServer = new ApolloServer({
  schema,
  uploads: false
});

/**
 * express application
 */
const api = express();
const server = http.createServer(api);

graphQLServer.installSubscriptionHandlers(server)

const mappedRoutes = mapRoutes(config.publicRoutes, 'api/controllers/');
const DB = dbService(environment, config.migrate).start();

// allow cross origin requests
// configure to allow only requests from certain origins
api.use(function (req, res, next) {

  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', '*');

  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', '*');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', '*');

  // Pass to next layer of middleware
  next();
});

// secure express app
api.use(helmet({
  dnsPrefetchControl: false,
  frameguard: false,
  ieNoOpen: false,
}));

// parsing the request bodys
api.use(bodyParser.urlencoded({ extended: false }));
api.use(bodyParser.json());

// public REST API
api.use('/rest', mappedRoutes);

// private GraphQL API
api.post('/graphql', (req, res, next) => auth(req, res, next));

graphQLServer.applyMiddleware({
  app: api,
  cors: {
    origin: true,
    credentials: true,
    methods: ['POST'],
    allowedHeaders: [
      'X-Requested-With',
      'X-HTTP-Method-Override',
      'Content-Type',
      'Accept',
      'Authorization',
      'Access-Control-Allow-Origin',
    ],
  },
  playground: {
    settings: {
      'editor.theme': 'light',
    },
  },
});

server.listen(config.port, () => {
  if (environment !== 'production'
    && environment !== 'development'
    && environment !== 'testing'
  ) {
    console.error(`NODE_ENV is set to ${environment}, but only production and development are valid.`);
    process.exit(1);
  }
  return DB;
});
8yparm6h

8yparm6h1#

基于您的错误;

{
  "error": "Could not connect to websocket endpoint wss://foodesk.herokuapp.com/graphql. Please check if the endpoint url is correct."
}

您的应用程序运行在heroku应用程序服务上,即“herokuapp.com“。就该服务而言,您无法控制服务器配置。
在开发过程中,您在“ws://”协议下提供订阅服务,当您部署到heroku时,该协议转换为安全版本“wss://”。我的解决方案需要你通过ssh访问服务器。这意味着您可以升级您的heroku订阅,以拥有具有专用IP地址的VM或任何其他云提供商。如果你有这样的地方,做以下事情;
1.在vm上托管应用程序并提供服务,然后编辑apache以代理“yourdomain.com“,以便在端口3000处提供应用程序,如下所示

<VirtualHost *:80>
 ServerName yourdomain.com
 ProxyPreserveHost on
 ProxyPass / http://localhost:3000/
 ProxyPassReverse / http://localhost:3000/
RewriteEngine on

</VirtualHost>

1.去你的域名注册商,并添加一个子域,通过它你将服务于你的订阅又名网络套接字。这是通过添加一个A记录与子域名例如“WebSocket”指向您的vm IP地址。这将使“websocket.yourdomain.com“可供使用。
1.在你的服务器上安装Apache服务器并包括一个虚拟主机,如下所示

<VirtualHost *:80>
 ServerName websocket.yourdomain.com
 ProxyPreserveHost on
 ProxyPass / ws://localhost:3000/
 ProxyPassReverse / ws://localhost:3000/
RewriteEngine on

</VirtualHost>

1.重启Apache服务器
此时,您的应用程序正在“www.example.com“上运行yourdomain.com,这也是您所有graphql变化和查询的地方。但是所有的订阅都将在“www.example.com“上建立websocket.yourdomain.com,它将把所有的请求代理到“ws://”协议,因此没有错误地到达你的订阅服务器。
N/B您的客户端代码将使用“wss:websocket.yourdomain.com“进行订阅连接

相关问题