如何使用Redis PubSub获得阿波罗graphql订阅的已连接客户端?

时间:2020-10-16 22:22:10

标签: javascript redis graphql apollo-server graphql-subscriptions

我有一个apolloServer graphql应用程序,并将订阅与RedisPubSub一起使用。有没有办法让我获得特定的订阅,并找出所有连接的客户端以及它们使用的arg?例如,有没有办法让我说“给我所有actionComplete订阅,然后从中给我每个连接的客户端的actionId”?

还有,我是否可以挂接到SubscriptionServer的'onOperation'和'onOperationComplete'生命周期事件?我使用server.installSubscriptionHandlers(httpServer)

设置了订阅

这是我的订阅定义:


export const actionComplete = subscriptionField('actionComplete', {
  type: ActionCompleteResponse,
  description:
    'A subscription that notifies when the action is complete.',
  args: {
    actionId: stringArg({
      description: 'The id that corresponds to the action',
      nullable: false
    })
  },
  subscribe: withFilter(
    (_root, _args, { pubSub }) => pubSub.asyncIterator(['ACTION_COMPLETE']),
    (payload, args) => {
      return payload.id === args.actionId
    }
  ),
  resolve: payload => {
    return payload
  }
})

我创建服务器的方式如下:

const server = new ApolloServer({... (ommitting the details here) ... })
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
server.applyMiddleware({ app })
const httpServer = createServer(app)
server.installSubscriptionHandlers(httpServer)
httpServer.listen({ port }, () =>
  console.log(`Server ready at http://localhost:${port}/graphql`)
)

0 个答案:

没有答案