Consider this GraphQL subscription resolver snippet:
subscription {
messageAdded(roomId: "123") {
id
content
}
}Assuming a new message with id: "m1" and content: "Hello" is added to room 123, what will the subscription deliver?
subscription {
messageAdded(roomId: "123") {
id
content
}
}Subscriptions send the full data requested when the event occurs.
The subscription resolver sends the full id and content fields of the new message added to the specified room.
Choose the correct description of a GraphQL subscription resolver.
Think about what makes subscriptions different from queries and mutations.
Subscription resolvers enable real-time updates by pushing data to clients when specific events happen.
Which option contains the correct syntax for a subscription resolver in GraphQL?
const resolvers = {
Subscription: {
messageAdded: {
subscribe: (parent, args, context, info) => {
return pubsub.asyncIterator('MESSAGE_ADDED')
}
}
}
}Check the use of asyncIterator and function syntax.
The code correctly defines a subscription resolver using asyncIterator to listen for events.
Given this subscription resolver:
Subscription: {
messageAdded: {
subscribe: (parent, args, context) => {
if (!args.roomId) {
throw new Error('roomId is required')
}
return pubsub.asyncIterator('MESSAGE_ADDED')
}
}
}Clients subscribe with messageAdded(roomId: "123") but receive no updates when messages are added. What is the likely cause?
Think about how events are filtered for specific rooms.
The asyncIterator listens to all 'MESSAGE_ADDED' events but does not filter by roomId, so clients subscribed to a specific room get no matching events.
You want to support subscriptions for messages added to multiple rooms. Which approach optimizes resource use and avoids unnecessary event delivery?
Consider how to minimize unnecessary data sent to clients.
Creating separate asyncIterators per room allows sending only relevant events to subscribed clients, reducing overhead and improving efficiency.