Subscriptions
Subscription is just another GraphQL operation type like Query and Mutation. It allows creating real-time subscriptions over a bidirectional transport layer, mainly over websockets. Read more about the subscriptions here. Below is a commentAdded subscription example, copied and pasted directly from the official Apollo documentation:
Subscription: {
commentAdded: {
subscribe: () => pubSub.asyncIterator('commentAdded')
}
}Notice Thepubsubis an instance ofPubSubclass. Read more about it here.
In order to create an equivalent subscription in Nest, we'll make use of the @Subscription() decorator. Let's extend our AuthorResolver used in the resolvers section.
const pubSub = new PubSub();
@Resolver('Author')
export class AuthorResolver {
constructor(
private readonly authorsService: AuthorsService,
private readonly postsService: PostsService,
) {}
@Query('author')
async getAuthor(@Args('id') id: number) {
return await this.authorsService.findOneById(id);
}
@ResolveProperty('posts')
async getPosts(@Parent() author) {
const { id } = author;
return await this.postsService.findAll({ authorId: id });
}
@Subscription()
commentAdded() {
return {
subscribe: () => pubSub.asyncIterator('commentAdded'),
};
}
} We have used a local PubSub instance here. Instead, we should define PubSub as a provider, inject it through the constructor (using @Inject() decorator), and reuse it across the whole application. You can read more about Nest custom providers here.
Module
In order to enable subscriptions, we have to set installSubscriptionHandlers property to true.
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
installSubscriptionHandlers: true,
}) To customize the subscriptions server (e.g. change port), you can use subscriptions property (read more).
Type definitions
The last step is to update type definitions file.
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: Int!
title: String
votes: Int
}
type Query {
author(id: Int!): Author
}
type Comment {
id: String
content: String
}
type Subscription {
commentAdded(repoFullName: String!): Comment
} Well done. We created a single commentAdded(repoFullName: String!): Comment subscription. You can find a full sample implementation here.
