如何更改Apollo Server WS路径?

时间:2019-05-26 19:08:37

标签: graphql apollo apollo-server

是否可以更改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 })

2 个答案:

答案 0 :(得分:0)

您可以在subscriptions配置中包含一个具有以下属性的ApolloServer对象:

  
      
  • 路径:
  •   
  • keepAlive:
  •   
  • onConnect:
  •   
  • onDisconnect:
  •   

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

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

答案 1 :(得分:0)

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

// ...

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

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

// ...