Bird
Raised Fist0
GraphQLquery~20 mins

Subscription resolvers in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Compare with other resolver types

    Queries fetch data once, mutations modify data, and schema defines types. Only subscriptions provide live updates.
  3. Final Answer:

    To provide real-time updates to clients when data changes -> Option C
  4. 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

  1. Step 1: Identify the correct GraphQL operation keyword

    Subscriptions use the type Subscription keyword to declare real-time data streams in the schema.
  2. Step 2: Check other operation keywords

    Queries use type Query, mutations use type Mutation, and type Resolver is not a valid keyword.
  3. Final Answer:

    type Subscription { newMessage: Message } -> Option A
  4. Quick Check:

    Subscription keyword = subscription declaration [OK]
Hint: Use 'type Subscription' keyword to declare live data streams [OK]
Common Mistakes:
  • Using 'type Query' or 'type Mutation' instead of 'type Subscription'
  • Trying to use 'type Resolver' keyword
  • Misplacing subscription declaration outside schema
3. Given this subscription resolver code snippet, what will be the output when a new message is published?
const resolvers = {
  Subscription: {
    newMessage: {
      subscribe: () => pubsub.asyncIterator(['MESSAGE_ADDED'])
    }
  }
};
medium
A. The client receives updates only when a message is added
B. The client receives all past messages immediately
C. The client receives no updates until a query is made
D. The client receives an error because subscribe is missing

Solution

  1. Step 1: Understand the subscribe function

    The subscribe function returns an async iterator for the event 'MESSAGE_ADDED', so it listens for new messages.
  2. Step 2: Determine what triggers updates

    Updates are sent only when the 'MESSAGE_ADDED' event occurs, so clients get notified only on new messages.
  3. Final Answer:

    The client receives updates only when a message is added -> Option A
  4. Quick Check:

    Subscription triggers on event = client updates [OK]
Hint: Subscribe returns async iterator for live event updates [OK]
Common Mistakes:
  • Assuming all past data is sent immediately
  • Thinking updates require queries first
  • Missing that subscribe returns async iterator
4. Identify the error in this subscription resolver code:
const resolvers = {
  Subscription: {
    newNotification: {
      subscribe: pubsub.asyncIterator('NOTIFICATION_ADDED')
    }
  }
};
medium
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

  1. Step 1: Check the subscribe property type

    The subscribe property must be a function that returns the async iterator, not the async iterator itself.
  2. Step 2: Verify correct usage of asyncIterator

    It should be subscribe: () => pubsub.asyncIterator('NOTIFICATION_ADDED') to work properly.
  3. Final Answer:

    The subscribe function should be a function returning asyncIterator, not the asyncIterator itself -> Option D
  4. 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?
const resolvers = {
  Subscription: {
    commentAdded: {
      subscribe: (parent, args, { pubsub }) => {
        return pubsub.asyncIterator('COMMENT_ADDED');
      },
      resolve: (payload, args) => {
        if (payload.postId === args.postId) {
          return payload;
        }
        return null;
      }
    }
  }
};
hard
A. The filtering should be done inside the subscribe function, not resolve
B. This implementation correctly filters comments by postId in the resolve function
C. The asyncIterator should listen to an array of post IDs, not a single event
D. Subscriptions cannot filter events by arguments

Solution

  1. Step 1: Understand event filtering in subscriptions

    The resolve function can filter events by checking if the payload matches the subscription arguments.
  2. Step 2: Check the given code logic

    The code returns the payload only if payload.postId === args.postId, otherwise returns null to skip irrelevant events.
  3. Final Answer:

    This implementation correctly filters comments by postId in the resolve function -> Option B
  4. Quick Check:

    Filtering in resolve function = correct approach [OK]
Hint: Filter events in resolve by comparing payload and args [OK]
Common Mistakes:
  • Trying to filter inside subscribe instead of resolve
  • Expecting asyncIterator to filter by arguments automatically
  • Believing subscriptions cannot filter events
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard