Consider this NestJS GraphQL subscription resolver snippet. What will the client receive when the triggerEvent method is called?
import { Resolver, Subscription, Mutation, Args } from '@nestjs/graphql'; import { PubSub } from 'graphql-subscriptions'; const pubSub = new PubSub(); @Resolver() export class EventResolver { @Subscription(() => String, { filter: (payload, variables) => payload.event === variables.eventName, }) eventTriggered(@Args('eventName') eventName: string) { return pubSub.asyncIterator('eventTriggered'); } @Mutation(() => Boolean) async triggerEvent(@Args('event') event: string) { await pubSub.publish('eventTriggered', { event: event }); return true; } }
Look at the filter function inside the subscription decorator.
The filter function checks if the event payload's event matches the subscription argument eventName. Only matching events are sent to the client.
In a NestJS GraphQL subscription resolver, when is the asyncIterator function called during the subscription lifecycle?
import { Resolver, Subscription } from '@nestjs/graphql'; import { PubSub } from 'graphql-subscriptions'; const pubSub = new PubSub(); @Resolver() export class SampleResolver { @Subscription(() => String) sampleSub() { return pubSub.asyncIterator('sampleEvent'); } }
Think about how subscriptions work per client connection.
Each client subscription triggers the resolver method, which returns a new async iterator instance for that client to listen to events.
Examine this subscription resolver code. Why do clients never receive events?
import { Resolver, Subscription, Mutation, Args } from '@nestjs/graphql'; import { PubSub } from 'graphql-subscriptions'; const pubSub = new PubSub(); @Resolver() export class DebugResolver { @Subscription(() => String) messageSent() { return pubSub.asyncIterator('messageSent'); } @Mutation(() => Boolean) sendMessage(@Args('msg') msg: string) { pubSub.publish('messageSent', { messageSent: msg }); return true; } }
Check the event name and payload keys used in publish and asyncIterator.
The asyncIterator listens for events named 'messageSent'. The publish call sends a payload with key 'messageSent', matching the subscription field name. This ensures data is sent correctly.
Choose the correct syntax for a NestJS GraphQL subscription resolver that filters events by a variable userId.
Remember the filter function receives payload and variables as arguments.
Option C correctly uses the filter function with both payload and variables, and the resolver method correctly uses @Args to get the subscription argument.
Option C misses variables in filter.
Option C misses @Args decorator.
Option C uses loose equality == which is discouraged.
Given this NestJS subscription and mutation, what sequence of messages will a subscribed client receive after calling sendMessages?
import { Resolver, Subscription, Mutation } from '@nestjs/graphql'; import { PubSub } from 'graphql-subscriptions'; const pubSub = new PubSub(); @Resolver() export class SequenceResolver { @Subscription(() => String) messageStream() { return pubSub.asyncIterator('messageStream'); } @Mutation(() => Boolean) async sendMessages() { await pubSub.publish('messageStream', { messageStream: 'First' }); await pubSub.publish('messageStream', { messageStream: 'Second' }); await pubSub.publish('messageStream', { messageStream: 'Third' }); return true; } }
Consider how PubSub handles multiple publishes and async iterators.
PubSub emits each published event in order to all subscribed clients. The async iterator streams all events as they come, so the client receives all three messages in the order published.