A subscription in GraphQL is a way to get real-time updates from the server. It keeps a connection open and sends data to the client whenever an event happens.
Click to reveal answer
beginner
What happens when a client starts a subscription?
When a client starts a subscription, it opens a persistent connection to the server, usually over WebSocket, to listen for specific events or data changes.
Click to reveal answer
intermediate
How does the server send data during a subscription?
The server sends data to the client whenever the subscribed event occurs. This data is sent as a stream of messages over the open connection.
Click to reveal answer
intermediate
What is the role of WebSocket in GraphQL subscriptions?
WebSocket provides a full-duplex communication channel that stays open, allowing the server to push updates to the client instantly during a subscription.
Click to reveal answer
beginner
How does a client stop a subscription?
A client stops a subscription by closing the connection or sending an unsubscribe message, which tells the server to stop sending updates.
Click to reveal answer
What does a GraphQL subscription do?
AProvides real-time updates from the server
BFetches data once like a query
CDeletes data on the server
DUpdates data on the client only
✗ Incorrect
Subscriptions keep a connection open to receive real-time data updates from the server.
Which protocol is commonly used to keep a subscription connection open?
ASMTP
BFTP
CHTTP GET
DWebSocket
✗ Incorrect
WebSocket allows full-duplex communication needed for real-time subscriptions.
When does the server send data in a subscription?
AWhenever the subscribed event happens
BOnly when the client requests it
CAt fixed time intervals
DOnly once at the start
✗ Incorrect
The server pushes data to the client whenever the event the client subscribed to occurs.
How does a client end a subscription?
ABy sending a query
BBy refreshing the page
CBy closing the connection or unsubscribing
DBy sending a mutation
✗ Incorrect
Closing the connection or sending an unsubscribe message stops the subscription.
What is the main benefit of using subscriptions?
AFaster database writes
BReal-time data updates without polling
CSimpler queries
DBetter security
✗ Incorrect
Subscriptions provide real-time updates without the need for repeated polling.
Explain the lifecycle of a GraphQL subscription from start to finish.
Think about how the connection starts, how data flows, and how it ends.
You got /5 concepts.
Describe why WebSocket is important for GraphQL subscriptions.
Consider how data moves between client and server continuously.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a subscription in GraphQL?
easy
A. To fetch data only once from the server
B. To update data on the server
C. To delete data from the database
D. To receive real-time updates when data changes
Solution
Step 1: Understand the role of subscriptions
Subscriptions are designed to keep the client updated automatically when data changes on the server.
Step 2: Compare with other GraphQL operations
Queries fetch data once, mutations change data, but subscriptions push live updates.
Final Answer:
To receive real-time updates when data changes -> Option D
Quick Check:
Subscriptions = real-time updates [OK]
Hint: Subscriptions push live data updates automatically [OK]
Common Mistakes:
Confusing subscriptions with queries
Thinking subscriptions modify data
Believing subscriptions fetch data only once
2. Which of the following is the correct syntax to start a GraphQL subscription named newMessage that listens for content and sender fields?
easy
A. subscription { newMessage { content sender } }
B. query { newMessage { content sender } }
C. mutation { newMessage { content sender } }
D. subscribe { newMessage { content sender } }
Solution
Step 1: Identify the correct keyword for subscriptions
The keyword to start a subscription is subscription, not query, mutation, or subscribe.
Step 2: Check the structure of the subscription
The subscription should specify the event name and requested fields inside curly braces.
Subscription data = event with requested fields [OK]
Hint: Subscription returns data object with event and fields [OK]
Common Mistakes:
Returning id as string instead of number
Missing fields in response
Expecting error when subscription is valid
4. You wrote this subscription:
subscription { newMessage { content sender } }
But your client never receives updates. Which is the most likely cause?
medium
A. The client is using a query instead of a subscription
B. The subscription syntax is invalid
C. The server does not publish events for newMessage
D. The fields content and sender do not exist
Solution
Step 1: Check subscription syntax
The syntax is correct, so syntax errors are unlikely.
Step 2: Consider server event publishing
If the server never sends events for newMessage, the client won't get updates.
Step 3: Verify client usage
The client uses subscription syntax, so it's not using a query.
Step 4: Confirm fields existence
Assuming fields exist, the main issue is missing server events.
Final Answer:
The server does not publish events for newMessage -> Option C
Quick Check:
No server events = no subscription updates [OK]
Hint: Check if server sends events for the subscription [OK]
Common Mistakes:
Assuming syntax error without checking server
Confusing query with subscription on client
Ignoring server-side event publishing
5. You want to create a subscription that only sends updates when a new chat message is sent by user "Alice". Which subscription definition correctly filters messages by sender?
hard
A. subscription { newMessage { content sender } where sender = "Alice" }
B. subscription { newMessage(sender: "Alice") { content sender } }