【问题标题】:How can I change Apollo Server WS path?如何更改 Apollo Server WS 路径?
【发布时间】:2019-10-12 11:18:03
【问题描述】:

有没有办法改变 Apollo Server WS 的路径(server.subscriptionsPath)?

默认路径是“/graphql”。如何设置其他路线,例如'ws://localhost:3000/api'?

import express from 'express'
import http from 'http'
import { ApolloServer } from 'apollo-server-express'

const app = express()

// ...

const server = new ApolloServer({
  typeDefs,
  resolvers,
})

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

console.log('server.graphqlPath', server.graphqlPath)
console.log('server.subscriptionsPath', server.subscriptionsPath)

httpServer.listen({ port:300 })

【问题讨论】:

    标签: graphql apollo apollo-server


    【解决方案1】:

    您可以在 ApolloServer 配置中包含 subscriptions 对象,并具有以下属性:

    • 路径:
    • 保持活动状态:
    • onConnect:
    • 断开连接:

    subscriptions 也可能只是一个字符串,在这种情况下它仅用于定义路径,或者您可以传入false 以完全禁用订阅。

    有关所有配置选项,请参阅 the docs

    【讨论】:

      【解决方案2】:

      如果其他人正在寻找答案。这对我有用:

      // ...
      
      const server = new ApolloServer({
        typeDefs,
        resolvers,
      })
      
      // This is missing in your example
      server.applyMiddleware({
        app,
        path: '/my-custom-path-here',
      });
      
      // ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-29
        • 2011-09-10
        • 2012-06-21
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多