0
0
GraphQLquery~20 mins

Subscription resolvers in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subscription Resolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this subscription resolver?

Consider this GraphQL subscription resolver snippet:

subscription {
  messageAdded(roomId: "123") {
    id
    content
  }
}

Assuming a new message with id: "m1" and content: "Hello" is added to room 123, what will the subscription deliver?

GraphQL
subscription {
  messageAdded(roomId: "123") {
    id
    content
  }
}
A{"data": {"messageAdded": {"content": "Hello"}}}
B{"data": {"messageAdded": {"id": "m1"}}}
C{"data": {"messageAdded": {"id": "m1", "content": "Hello"}}}
D{"errors": [{"message": "Room not found"}]}
Attempts:
2 left
💡 Hint

Subscriptions send the full data requested when the event occurs.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes a subscription resolver?

Choose the correct description of a GraphQL subscription resolver.

AIt handles real-time data by pushing updates to clients when events occur.
BIt fetches data only once when a query is executed.
CIt modifies data in the database when a mutation is called.
DIt caches query results to improve performance.
Attempts:
2 left
💡 Hint

Think about what makes subscriptions different from queries and mutations.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this subscription resolver code

Which option contains the correct syntax for a subscription resolver in GraphQL?

const resolvers = {
  Subscription: {
    messageAdded: {
      subscribe: (parent, args, context, info) => {
        return pubsub.asyncIterator('MESSAGE_ADDED')
      }
    }
  }
}
Asubscribe should be a function returning a Promise, not an asyncIterator.
BCorrect as is, no syntax errors.
CMissing parentheses after asyncIterator: pubsub.asyncIterator 'MESSAGE_ADDED'
DSubscription should be lowercase: subscription.
Attempts:
2 left
💡 Hint

Check the use of asyncIterator and function syntax.

🔧 Debug
advanced
2:30remaining
Why does this subscription resolver not send updates?

Given this subscription resolver:

Subscription: {
  messageAdded: {
    subscribe: (parent, args, context) => {
      if (!args.roomId) {
        throw new Error('roomId is required')
      }
      return pubsub.asyncIterator('MESSAGE_ADDED')
    }
  }
}

Clients subscribe with messageAdded(roomId: "123") but receive no updates when messages are added. What is the likely cause?

AThe asyncIterator listens to 'MESSAGE_ADDED' without filtering by roomId, so no matching events are sent.
BThe subscription resolver throws an error because roomId is missing.
CThe pubsub.asyncIterator method is deprecated and does not work.
DThe subscribe function must be async to work properly.
Attempts:
2 left
💡 Hint

Think about how events are filtered for specific rooms.

optimization
expert
3:00remaining
How to optimize subscription resolver for multiple rooms efficiently?

You want to support subscriptions for messages added to multiple rooms. Which approach optimizes resource use and avoids unnecessary event delivery?

AUse a single asyncIterator for all 'MESSAGE_ADDED' events and filter messages by roomId inside the resolver before sending.
BUse polling queries instead of subscriptions to reduce server load.
CSend all 'MESSAGE_ADDED' events to all clients and let clients filter messages on their side.
DCreate a separate asyncIterator for each roomId and subscribe clients only to their room's iterator.
Attempts:
2 left
💡 Hint

Consider how to minimize unnecessary data sent to clients.