2

I was just wondering if there was any performance decrease or any disadvantage in using the same endpoint for the graphql endpoint and also for the WebSocket. You can see the sample code below.

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`
  );
});
Mikias Abebe
  • 149
  • 2
  • 6

0 Answers0