0
0
GraphQLquery~10 mins

Subscription filtering in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subscription filtering
Client subscribes with filter
Server receives subscription request
Server applies filter criteria
Events generated on server
Filter checks each event
Send event
Client receives filtered events
The client subscribes with a filter; the server applies it to events and sends only matching events to the client.
Execution Sample
GraphQL
subscription onNewMessage($userId: ID!) {
  messageAdded(filter: {recipientId: $userId}) {
    id
    content
    senderId
  }
}
Client subscribes to new messages only for a specific user ID.
Execution Table
StepEvent GeneratedFilter Condition (recipientId == userId)Pass Filter?ActionClient Receives
1{id: 101, content: 'Hi', senderId: 'A', recipientId: 'U1'}U1 == U1YesSend event{id: 101, content: 'Hi', senderId: 'A'}
2{id: 102, content: 'Hello', senderId: 'B', recipientId: 'U2'}U2 == U1NoIgnore eventNone
3{id: 103, content: 'Hey', senderId: 'C', recipientId: 'U1'}U1 == U1YesSend event{id: 103, content: 'Hey', senderId: 'C'}
4{id: 104, content: 'Yo', senderId: 'D', recipientId: 'U3'}U3 == U1NoIgnore eventNone
5No more events----
💡 No more events generated, subscription ends or waits for new events.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
eventnull{id:101,...}{id:102,...}{id:103,...}{id:104,...}null
filterPassnulltruefalsetruefalsenull
clientReceived[][{id:101,...}][{id:101,...}][{id:101,...},{id:103,...}][{id:101,...},{id:103,...}][{id:101,...},{id:103,...}]
Key Moments - 3 Insights
Why does the client not receive events where recipientId does not match userId?
Because the filter condition fails (see execution_table rows 2 and 4), so those events are ignored and not sent to the client.
What happens if no events match the filter?
The client receives no events (empty list), as shown in execution_table rows where filterPass is false and action is 'Ignore event'.
Does the server send all events to the client before filtering?
No, the server applies the filter first and only sends events that pass the filter, as shown in the 'Action' column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'filterPass' value at Step 3?
Atrue
Bfalse
Cnull
Dundefined
💡 Hint
Check the 'Filter Condition' and 'Pass Filter?' columns at Step 3 in the execution_table.
At which step does the client receive the event with id 103?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Client Receives' column for event id 103 in the execution_table.
If the filter condition changed to recipientId == 'U2', which step would send an event to the client?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Refer to the 'Event Generated' and 'Filter Condition' columns to see which event has recipientId 'U2'.
Concept Snapshot
Subscription filtering lets clients receive only events matching criteria.
Client sends filter with subscription.
Server checks each event against filter.
Only matching events are sent to client.
Non-matching events are ignored.
This saves bandwidth and processing.
Full Transcript
Subscription filtering in GraphQL means the client asks to get only certain events, like messages for a specific user. The server gets this request and checks every new event. If the event matches the filter, it sends it to the client. If not, it ignores it. This way, the client only gets relevant updates. For example, if the client wants messages where recipientId equals their userId, only those messages are sent. Events with other recipientIds are skipped. This filtering happens on the server before sending data, saving resources and making updates efficient.