【问题标题】:How do I change the subscription server endpoint in Apollo express server?如何更改 Apollo Express 服务器中的订阅服务器端点?
【发布时间】:2019-11-26 20:18:45
【问题描述】:

使用 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.```

【问题讨论】:

    标签: express graphql apollo apollo-server


    【解决方案1】:

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

    subscriptions:<Object> | <String> | false

    定义订阅路径的字符串或自定义订阅服务器的对象。设置为 false 以禁用订阅

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

    【讨论】:

      【解决方案2】:

      如下初始化ApolloServer实例:

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

      【讨论】:

        猜你喜欢
        • 2019-05-13
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 2020-03-24
        • 2019-01-27
        • 2019-01-22
        • 2020-07-11
        • 1970-01-01
        相关资源
        最近更新 更多