Complete the code to define a subscription resolver that listens for new messages.
const resolvers = {
Subscription: {
newMessage: {
subscribe: [1]
}
}
};The subscribe function must return an async iterator from pubsub.asyncIterator to listen for events.
Complete the code to publish a new message event after saving the message.
async function sendMessage(message) {
await saveMessageToDB(message);
pubsub.[1]('NEW_MESSAGE', { newMessage: message });
}To notify subscribers, use pubsub.publish with the event name and payload.
Fix the error in the subscription resolver to correctly return the async iterator.
Subscription: {
messageAdded: {
subscribe() {
return pubsub.[1]('MESSAGE_ADDED');
}
}
}The resolver must return pubsub.asyncIterator to create an async iterator for the subscription.
Fill both blanks to create a subscription resolver that filters messages by channel.
Subscription: {
messageByChannel: {
subscribe: withFilter(
() => pubsub.[1]('MESSAGE_SENT'),
(payload, variables) => payload.channelId [2] variables.channelId
)
}
}Use pubsub.asyncIterator to listen for events and === to filter messages matching the channel ID.
Fill all three blanks to define a subscription resolver with a filter and a custom resolve function.
Subscription: {
commentAdded: {
subscribe: withFilter(
() => pubsub.[1]('COMMENT_ADDED'),
(payload, variables) => payload.postId [2] variables.postId
),
resolve: (payload) => payload.[3]
}
}The subscription uses asyncIterator to listen, === to filter by postId, and the resolve function returns the comment from the payload.