如何更改 Apollo Express 服务器中的订阅服务器端点?

How do I change the subscription server endpoint in Apollo express server?

使用 express 创建 Apollo 服务器时,http 和订阅端点默认为 /graphql。当将 http 端点更改为 /graphql 以外的其他内容时,订阅端点保持指向 /graphql。如何使我的订阅端点与我的 http 端点相同?

这是 Apollo 网站的示例,我只添加了 path: custom_endpoint

const { ApolloServer } = require('apollo-server-express');
const express = require('express');

const PORT = 4000;
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });

server.applyMiddleware({app, path: custom_endpoint})

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

// ⚠️ Pay attention to the fact that we are calling `listen` on the http server variable, and not on `app`.
httpServer.listen(PORT, () => {
  console.log(` Server ready at http://localhost:${PORT}${server.graphqlPath}`) //this changes to my custom_endpoint
  console.log(` Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`) // this does not chnage to my custome_endpoint.```

ApolloServer 构造函数接受一个 subscriptions 参数,可用于将订阅端点自定义为 shown in the docs

subscriptions: <Object> | <String> | false

String defining the path for subscriptions or an Object to customize the subscriptions server. Set to false to disable subscriptions

  • path: <String>
  • keepAlive: <Number>
  • onConnect: <Function>
  • onDisconnect: <Function>

初始化ApolloServer实例如下:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  subscriptions: { path: '/custom-graphql-ws' }
});