This repository was archived by the owner on May 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Please help me, how to filter to send message? Thanks! #322
Copy link
Copy link
Open
Description
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
Labels
No labels