0
0
NestJSframework~10 mins

Subscriptions in NestJS - Interactive Code Practice

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

Complete the code to import the required module for GraphQL subscriptions in NestJS.

NestJS
import { [1] } from '@nestjs/graphql';
Drag options to blanks, or click blank then click option'
ASubscription
BGraphQLModule
CResolver
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'Subscription' instead of 'GraphQLModule'.
Using 'Query' which is for queries, not subscriptions.
2fill in blank
medium

Complete the code to define a subscription method in a NestJS resolver.

NestJS
@Subscription()
[1]() {
  return pubSub.asyncIterator('messageAdded');
}
Drag options to blanks, or click blank then click option'
Asubscribe
Basync
Cresolve
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' keyword without 'function' keyword.
Using 'resolve' which is for queries or mutations.
3fill in blank
hard

Fix the error in the subscription setup by completing the missing import.

NestJS
import { PubSub } from '[1]';

const pubSub = new PubSub();
Drag options to blanks, or click blank then click option'
Aapollo-server-express
B@nestjs/graphql
Cgraphql-subscriptions
Drxjs
Attempts:
3 left
💡 Hint
Common Mistakes
Importing PubSub from '@nestjs/graphql' which does not export it.
Using 'rxjs' which is for reactive programming but not for GraphQL PubSub.
4fill in blank
hard

Fill both blanks to correctly configure the GraphQLModule for subscriptions in NestJS.

NestJS
GraphQLModule.forRoot({
  installSubscriptionHandlers: [1],
  subscriptions: {
    'graphql-ws': [2]
  }
})
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C{}
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Setting installSubscriptionHandlers to false disables subscriptions.
Using null instead of an object for subscriptions config.
5fill in blank
hard

Fill all three blanks to create a subscription resolver that publishes a message event.

NestJS
import { Resolver, [1] } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';

const pubSub = new PubSub();

@Resolver()
export class MessageResolver {
  @[2]()
  messageAdded() {
    return pubSub.asyncIterator('messageAdded');
  }

  async addMessage(content: string) {
    await pubSub.[3]('messageAdded', { messageAdded: content });
  }
}
Drag options to blanks, or click blank then click option'
ASubscription
Cpublish
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Query' decorator instead of 'Subscription'.
Calling 'subscribe' instead of 'publish' to send events.