首页 > 解决方案 > Apollo graphql 订阅,对 graphql 服务器和 websocket 端点使用相同的端点

问题描述

我只是想知道为 graphql 端点和 WebSocket 使用相同的端点是否有任何性能下降或任何劣势。您可以在下面查看示例代码。

import express = require("express");
import { ApolloServer } from "apollo-server-express";
import bodyParser from "body-parser";
import Knex from "knex";
import { execute, subscribe } from "graphql";
import { SubscriptionServer } from "subscriptions-transport-ws";

// graphql api
import api from "./api";

const { createServer } = require("http");

const app: express.Application = express();

const path = "/graphql";

app.use(bodyParser.json());

const graphqlServer = new ApolloServer(api);

graphqlServer.applyMiddleware({ app, path });

const server = createServer(app);

server.listen(process.env.PORT, err => {
  if (err) {
    throw new Error(err);
  }

  new SubscriptionServer(
    {
      execute,
      subscribe,
      schema: api.schema
    },
    {
      server,
// same as the graphql endpoint
      path
    }
  );
  console.log(
    `the server is running at http://localhost:${process.env.PORT}/graphql`
  );
});

标签: apollo-servergraphql-subscriptionsws

解决方案


推荐阅读