📖 Scenario: You are creating a simple chat application where users can send messages and receive new messages in real-time.
🎯 Goal: Build a GraphQL subscription resolver that allows clients to listen for new chat messages as they arrive.
📋 What You'll Learn
Create a basic message data structure
Set up a PubSub system for message events
Write a subscription resolver for new messages
Complete the subscription setup to enable real-time updates
💡 Why This Matters
🌍 Real World
Real-time chat apps, live notifications, and collaborative tools use subscription resolvers to push updates instantly to users.
💼 Career
Understanding subscription resolvers is essential for backend developers working with GraphQL APIs that require real-time data updates.
Progress0 / 4 steps
1
DATA SETUP: Create the initial message array
Create an array called messages with these exact objects: { id: 1, content: "Hello!" } and { id: 2, content: "Welcome to the chat." }
GraphQL
Hint
Use const messages = [ ... ] to create the array with two message objects.
2
CONFIGURATION: Set up PubSub for subscriptions
Create a constant called PubSub by importing it from 'graphql-subscriptions' and then create a new instance called pubsub using new PubSub()
GraphQL
Hint
Use const { PubSub } = require('graphql-subscriptions') and then const pubsub = new PubSub().
3
CORE LOGIC: Write the subscription resolver for new messages
Create an object called Subscription with a field messageAdded that has a subscribe function returning pubsub.asyncIterator('MESSAGE_ADDED')
GraphQL
Hint
Define Subscription with messageAdded field and a subscribe function returning pubsub.asyncIterator('MESSAGE_ADDED').
4
COMPLETION: Publish new messages and export subscription
Add a function called addMessage that takes content, creates a new message with an incremented id, pushes it to messages, and calls pubsub.publish('MESSAGE_ADDED', { messageAdded: newMessage }). Then export Subscription and addMessage.
GraphQL
Hint
Create addMessage to add and publish new messages, then export Subscription and addMessage.
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?