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.