A subscription resolver is a special function in GraphQL that listens for real-time events and sends updates to clients when data changes.
Click to reveal answer
beginner
How does a subscription resolver differ from a query resolver?
A query resolver fetches data once when requested, while a subscription resolver keeps a connection open to send data updates continuously.
Click to reveal answer
intermediate
What role does PubSub play in subscription resolvers?
PubSub is a system that helps subscription resolvers by publishing events and allowing clients to subscribe to those events for real-time updates.
Click to reveal answer
intermediate
Why do subscription resolvers use WebSockets?
WebSockets keep a live connection between client and server, enabling subscription resolvers to push data instantly without repeated requests.
Click to reveal answer
advanced
What is the typical structure of a subscription resolver function?
It usually has a subscribe method that returns an AsyncIterator to listen for events, and optionally a resolve method to process the event data before sending.
Click to reveal answer
What does a subscription resolver primarily do?
AListens for real-time data changes and sends updates
BFetches data once on request
CDeletes data from the database
DUpdates data in the database
✗ Incorrect
Subscription resolvers listen for events and send real-time updates to clients.
Which protocol is commonly used to maintain live connections for subscriptions?
AWebSocket
BFTP
CHTTP
DSMTP
✗ Incorrect
WebSocket keeps a persistent connection needed for real-time subscriptions.
In GraphQL subscriptions, what does PubSub help with?
AQuery optimization
BSchema validation
CPublishing and subscribing to events
DAuthentication
✗ Incorrect
PubSub manages event publishing and subscription for real-time updates.
What does the subscribe method in a subscription resolver return?
AA Promise
BAn AsyncIterator
CA Boolean
DA String
✗ Incorrect
The subscribe method returns an AsyncIterator to listen for events.
Which of these is NOT a feature of subscription resolvers?
AEvent listening
BContinuous data updates
CReal-time communication
DSingle data fetch
✗ Incorrect
Subscription resolvers do not fetch data just once; they provide continuous updates.
Explain how subscription resolvers work in GraphQL and why they are useful.
Think about how apps get live data like chat messages or notifications.
You got /5 concepts.
Describe the difference between query resolvers and subscription resolvers in GraphQL.
Compare a snapshot photo to a live video stream.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a subscription resolver in GraphQL?
easy
A. To modify data on the server
B. To fetch data only once when a query is made
C. To provide real-time updates to clients when data changes
D. To define the schema types
Solution
Step 1: Understand the role of subscription resolvers
Subscription resolvers are designed to send data updates automatically as events happen, unlike queries which fetch data once.
Step 2: Compare with other resolver types
Queries fetch data once, mutations modify data, and schema defines types. Only subscriptions provide live updates.
Final Answer:
To provide real-time updates to clients when data changes -> Option C
Quick Check:
Subscription = real-time updates [OK]
Hint: Subscriptions push live data updates automatically [OK]
Common Mistakes:
Confusing subscriptions with queries
Thinking subscriptions modify data
Mixing schema definition with resolver purpose
2. Which of the following is the correct way to declare a subscription resolver in GraphQL SDL?
easy
A. type Subscription { newMessage: Message }
B. type Query { newMessage: Message }
C. type Mutation { newMessage: Message }
D. type Resolver { newMessage: Message }
Solution
Step 1: Identify the correct GraphQL operation keyword
Subscriptions use the type Subscription keyword to declare real-time data streams in the schema.
Step 2: Check other operation keywords
Queries use type Query, mutations use type Mutation, and type Resolver is not a valid keyword.
Final Answer:
type Subscription { newMessage: Message } -> Option A
A. The resolver key should be 'Query' instead of 'Subscription'
B. The event name should be an array, not a string
C. Subscription resolvers cannot use pubsub
D. The subscribe function should be a function returning asyncIterator, not the asyncIterator itself
Solution
Step 1: Check the subscribe property type
The subscribe property must be a function that returns the async iterator, not the async iterator itself.
Step 2: Verify correct usage of asyncIterator
It should be subscribe: () => pubsub.asyncIterator('NOTIFICATION_ADDED') to work properly.
Final Answer:
The subscribe function should be a function returning asyncIterator, not the asyncIterator itself -> Option D
Quick Check:
Subscribe must be a function returning asyncIterator [OK]
Hint: Subscribe must be a function returning asyncIterator [OK]
Common Mistakes:
Assigning asyncIterator directly instead of a function
Using string instead of array for event names (optional)
Confusing subscription with query resolvers
5. You want to create a subscription that only sends updates to clients when a new comment is added to a specific post ID. Which resolver implementation correctly filters events by post ID?