服务器如何捕获该客户端取消订阅asyncIterator

时间:2019-10-22 09:31:08

标签: graphql observable apollo subscription

用户登录信息保存在Redis中。然后,所有用户都订阅此数据。当用户注销时,客户端退订此数据。因此,当服务器捕获到此取消订阅时,我想更新为redis。

workspaceStatus.ts

import { withFilter } from 'graphql-yoga'
const WORKSPACE_STATUS = 'WORKSPACE_STATUS'

export default {
  Subscription: {
    workspaceStatus: {
      subscribe: withFilter(
        (_: any, __: any, pubsub: any) => {
          return pubsub.asyncIterator(WORKSPACE_STATUS)
        },
        (payload, variables) => {
          return payload.workspaceStatus.workspaceId === variables.workspaceId
        }
      ),
    },
  },
}

workspaceStatus.graphql

type Subscription {
  workspaceStatus(workspaceId: Int!): WorkspaceStatus
}

client.ts

...

workspaceStatusObserverble

componentDidMount(){
  this.workspaceStatusObserverble = client.subscribe({
          query: SUBSCRIBE_WORKSPACE_STATUS,
          variables: {
            workspaceId: workspaceId,
          },
        }).subscribe((data) => {
          const { data: { workspaceStatus } } = data
          this.props.updateWorkspaceStatus(workspaceStatus, (setStatus) => {})
        })
}

componentWillUnmount() {
    window.addEventListener('resize', this.updateDimensions)
    if (this.workspaceStatusObserverble) {
      //unsubscibe
      this.workspaceStatusObserverble.unsubscribe()
    }
  }
...

我该如何在服务器端取消订阅?

0 个答案:

没有答案