Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.
This repository was archived by the owner on May 17, 2025. It is now read-only.

Please help me, how to filter to send message? Thanks! #322

@allran

Description

@allran

When need send message, how to filter the target to get data?
I only find follow code:

export const resolver = {
  Subscribe: {
    mySubscription: {
      subscribe: subscribe('GREETINGS', {
        filter(_, _, context) {
          console.log(context.connectionId) // the connectionId
        },
        async onAfterSubscribe(_, _, { connectionId, publish }) {
          await publish('GREETINGS', { message: `HI from ${connectionId}!` })
        }
      })
      resolve: (event, args, context) => {
        console.log(context.connectionInitPayload) // payload from connection_init
        return event.payload.message
      },
    },
  },
}

When publishing data to subscribers, we need to make sure that each subscriber gets only the data it needs.
To do so, is there have some like withFilter helper from this package, which wraps AsyncIterator with a filter function, and lets you control each publication for each user.

for example, is there have like pubsub.asyncIterator(triggers).next()to find the target?

import { withFilter } from 'graphql-subscriptions';

const SOMETHING_CHANGED_TOPIC = 'something_changed';

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: withFilter(() => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC), (payload, variables) => {
        return payload.somethingChanged.id === variables.relevantId;
      }),
    },
  },
}

Then, When i get payload and variables data, i can compare if neet subscription message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions