Subscription syntax in GraphQL - Time & Space Complexity
When using GraphQL subscriptions, it's important to understand how the time to deliver updates grows as more events happen.
We want to know how the system handles repeated data updates over time.
Analyze the time complexity of the following subscription syntax.
subscription OnNewMessage {
newMessage {
id
content
sender {
name
}
}
}
This subscription listens for new messages and fetches their details as they arrive.
Look at what repeats when new data arrives.
- Primary operation: Sending each new message update to all subscribed clients.
- How many times: Once per new message event, repeated indefinitely as messages arrive.
As more messages come in, the system sends more updates.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | 10 update sends |
| 100 messages | 100 update sends |
| 1000 messages | 1000 update sends |
Pattern observation: The number of operations grows directly with the number of new messages.
Time Complexity: O(n)
This means the work grows linearly with the number of new messages sent to subscribers.
[X] Wrong: "The subscription sends all past messages every time a new message arrives."
[OK] Correct: Subscriptions only send new data as it happens, not the entire history each time.
Understanding how subscriptions scale helps you explain real-time data flow in apps, a useful skill for many projects.
"What if the subscription filtered messages by sender? How would that affect the time complexity?"
Practice
subscription in GraphQL?Solution
Step 1: Understand the role of subscription
Subscriptions are designed to keep the client updated automatically when data changes, without asking repeatedly.Step 2: Compare with other operations
Queries fetch data once, mutations change data, but subscriptions listen for live updates.Final Answer:
To receive real-time updates automatically when data changes -> Option BQuick Check:
Subscription = live updates [OK]
- Confusing subscription with query or mutation
- Thinking subscription fetches data only once
- Assuming subscription modifies data
newMessage requesting id and content fields?Solution
Step 1: Identify the correct keyword for subscription
The keyword to start a subscription issubscription, notquery,mutation, orsubscribe.Step 2: Check the structure
The subscription block should contain the event name and requested fields inside curly braces.Final Answer:
subscription { newMessage { id content } } -> Option CQuick Check:
Keyword subscription + fields = subscription { newMessage { id content } } [OK]
- Using 'query' or 'mutation' instead of 'subscription'
- Using 'subscribe' keyword which is invalid
- Missing curly braces around fields
subscription { newComment { id text author { name } } }Solution
Step 1: Analyze the subscription fields
The subscription requestsid,text, and nestedauthor { name }fields for thenewCommentevent.Step 2: Understand what is sent on event
When a new comment is added, the client receives exactly these fields for that comment, not all comments.Final Answer:
id, text, and author name of the new comment -> Option AQuick Check:
Requested fields = id, text, author name [OK]
- Assuming all comments are sent, not just new ones
- Ignoring nested author name field
- Thinking only scalar fields are sent
subscription newMessage { id content }Solution
Step 1: Check subscription structure
The subscription must specify the event name followed by its fields inside braces, e.g.,subscription { newMessage { id content } }.Step 2: Identify the error in given syntax
The given syntax places fields directly after subscription name without nesting inside event braces.Final Answer:
Fields 'id' and 'content' should be inside the event name braces -> Option DQuick Check:
Fields must be nested inside event braces [OK]
- Placing fields outside event braces
- Using 'query' instead of 'subscription'
- Omitting braces entirely
Solution
Step 1: Understand argument filtering in subscriptions
GraphQL subscriptions accept arguments like queries, so filters are passed as arguments inside parentheses after the event name.Step 2: Identify correct filter syntax
subscription { stockPrice(price_gt: 100) { symbol price } } usesprice_gt: 100as an argument, which is the standard way to filter for price greater than 100.Final Answer:
subscription { stockPrice(price_gt: 100) { symbol price } } -> Option AQuick Check:
Filters go as arguments inside parentheses [OK]
- Trying to filter inside field braces
- Using invalid keywords like 'where' or 'if'
- Placing filters outside subscription block
Ready to prove it?
Pass the test and this pattern moves unknown
