简体   繁体   English

GraphQL Secure Web 插座带快递

[英]GraphQL Secure Web Socket With Express

I am creating Express, Nuxt(SPA, basically Vue), GraphQL website with graphql subscriptions.我正在使用 graphql 订阅创建 Express、Nuxt(SPA,基本上是 Vue)、GraphQL 网站。 My web sockets are supported only via http, which is very unfortunate.我的 web sockets 仅通过 http 支持,这是非常不幸的。 I can not figure out, how to use wss://mydomain/graphql/subscriptions.我不知道如何使用 wss://mydomain/graphql/subscriptions。 When I try to generate my.crt and.key and run it on localhost:3000, i get this error: ERR_SSL_VERSION_OR_CIPHER_MISMATCH.当我尝试生成 my.crt 和 .key 并在 localhost:3000 上运行它时,我收到此错误:ERR_SSL_VERSION_OR_CIPHER_MISMATCH。 I am not sure if the openssl certificates are correct but I have tried like 10 guides and neither of them worked for me.我不确定 openssl 证书是否正确,但我尝试了 10 个指南,但它们都不适合我。

Also, I am deploying this web to Heroku, if that makes any difference.此外,我正在将此 web 部署到 Heroku,如果这有什么不同的话。 (when I push to heroku with this "https" config, the site does not even load). (当我使用这个“https”配置推送到 heroku 时,网站甚至没有加载)。

One more thing: I am using graphql-compose-mongose to generate my schema, thus using Apollo is not possible (I think) because it requires TypeDef and Resolvers.还有一件事:我正在使用 graphql-compose-mongose 来生成我的模式,因此使用 Apollo 是不可能的(我认为),因为它需要 TypeDef 和解析器。

Here is my code.这是我的代码。 For socket on client I use "graphql-subscriptions-client".对于客户端上的套接字,我使用“graphql-subscriptions-client”。

const consola = require("consola");
const {Nuxt, Builder} = require("nuxt");
const cors = require("cors");
const {SubscriptionServer} = require("subscriptions-transport-ws");
const {execute, subscribe} = require("graphql");
// const {createServer} = require("http");
const {createServer} = require("https");
const {graphiqlExpress, graphqlExpress} = require("graphql-server-express");
const bodyParser = require("body-parser");


const fs = require("fs");
const path = require("path");

const server = express();

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js');
config.dev = process.env.NODE_ENV !== 'production';

// GraphQL schema
const schema = require("./graphql/schema");

async function start() {
  // Init Nuxt.js
  const nuxt = new Nuxt(config);

  const {host, port} = nuxt.options.server;

  // accept clients
  server.use('*', cors({origin: `https://${host}:${port}`}));

  // setup graphql server
  server.use('/graphql', bodyParser.json(), graphqlExpress({schema}));
  server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
    subscriptionsEndpoint: `wss://${host}:${port}/subscriptions`
  }));

  // database connection
  await require("./database/connection");

  // generate GraphQL schema
  await require("../scripts/buildSchema");

  await nuxt.ready();
  // Build only in dev mode
  if (config.dev) {
    const builder = new Builder(nuxt);
    await builder.build()
  }

  // Give nuxt middleware to express
  server.use(nuxt.render);

  // wrap express server in web socket
  const serverOptions = {
    cert: fs.readFileSync(path.resolve(__dirname, './mydomain.crt')),
    key: fs.readFileSync(path.resolve(__dirname, './mydomain.key'))
  };


  // const webSocket = createServer(server);
  const webSocket = createServer(server, serverOptions);

  // start server
  webSocket.listen(port, () => {
    consola.ready({
      message: `Server listening on https://${host}:${port}`,
      badge: true
    });

    // Set up the WebSocket for handling GraphQL subscriptions
    new SubscriptionServer({
      execute,
      subscribe,
      schema
    }, {
      server: webSocket,
      path: '/subscriptions',
    });
  });
}

start();

Thank you for your answers.谢谢您的回答。

Turns out Heroku does not provide free SSL.原来 Heroku 不提供免费的 SSL。 It costs 20$/month.它的费用为 20 美元/月。 So this can not be done for free.所以这不能免费完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM