0
0
GraphQLquery~10 mins

Subscription resolvers in GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a subscription resolver that listens for new messages.

GraphQL
const resolvers = {
  Subscription: {
    newMessage: {
      subscribe: [1]
    }
  }
};
Drag options to blanks, or click blank then click option'
Apubsub.listen('NEW_MESSAGE')
Bpubsub.publish('NEW_MESSAGE')
Cpubsub.subscribe('NEW_MESSAGE')
Dpubsub.asyncIterator('NEW_MESSAGE')
Attempts:
3 left
💡 Hint
Common Mistakes
Using pubsub.publish instead of asyncIterator
Calling a non-existent pubsub.listen method
2fill in blank
medium

Complete the code to publish a new message event after saving the message.

GraphQL
async function sendMessage(message) {
  await saveMessageToDB(message);
  pubsub.[1]('NEW_MESSAGE', { newMessage: message });
}
Drag options to blanks, or click blank then click option'
Asubscribe
BasyncIterator
Cpublish
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using subscribe instead of publish
Trying to call asyncIterator to send events
3fill in blank
hard

Fix the error in the subscription resolver to correctly return the async iterator.

GraphQL
Subscription: {
  messageAdded: {
    subscribe() {
      return pubsub.[1]('MESSAGE_ADDED');
    }
  }
}
Drag options to blanks, or click blank then click option'
AasyncIterator
Bpublish
Csubscribe
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Returning pubsub.publish instead of asyncIterator
Using a non-existent listen method
4fill in blank
hard

Fill both blanks to create a subscription resolver that filters messages by channel.

GraphQL
Subscription: {
  messageByChannel: {
    subscribe: withFilter(
      () => pubsub.[1]('MESSAGE_SENT'),
      (payload, variables) => payload.channelId [2] variables.channelId
    )
  }
}
Drag options to blanks, or click blank then click option'
AasyncIterator
B===
C!==
Dpublish
Attempts:
3 left
💡 Hint
Common Mistakes
Using publish instead of asyncIterator
Using !== instead of === in the filter
5fill in blank
hard

Fill all three blanks to define a subscription resolver with a filter and a custom resolve function.

GraphQL
Subscription: {
  commentAdded: {
    subscribe: withFilter(
      () => pubsub.[1]('COMMENT_ADDED'),
      (payload, variables) => payload.postId [2] variables.postId
    ),
    resolve: (payload) => payload.[3]
  }
}
Drag options to blanks, or click blank then click option'
AasyncIterator
B===
Ccomment
Dpublish
Attempts:
3 left
💡 Hint
Common Mistakes
Using publish instead of asyncIterator
Using !== instead of === in filter
Returning wrong property in resolve