Skip to main content

Subscriptions

note

Before reading this it is recommended to read the nestjs graphql subscriptions docs.

nestjs-query includes out of the box subscription support. When enabling subscriptions the following subscriptions will be created

  • created{ObjectName} - published when the createOne or createMany mutation is called.
  • updatedOne{ObjectName} - published when the updateOne mutation is called.
  • updatedMany{ObjectName} - published when updateMany mutation is called.
  • deletedOne{ObjectName} - published when the deleteOne mutation is called.
  • deletedMany{ObjectName} - published when deleteMany mutation is called.

Enabling Subscriptions#

You can enable subscriptions through the auto-generated resolver using the NestjsQueryGraphQLModule or by manually defining your resolver.

In both cases you need to set the enableSubscriptions option to true.

In the below example the following subscriptions will be exposed.

  • createdTodoItem - published when the createOne or createMany mutation is called.
  • updatedOneTodoItem - published when the updateOne mutation is called.
  • updatedManyTodoItems - published when updateMany mutation is called.
  • deletedOneTodoItem - published when the deleteOne mutation is called.
  • deletedManyTodoItems - published when deleteMany mutation is called.
todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemInputDTO } from './todo-item.input';import { TodoItemDTO } from './todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      resolvers: [{        DTOClass: TodoItemDTO,        EntityClass: TodoItemEntity,        CreateDTOClass: TodoItemInputDTO,        UpdateDTOClass: TodoItemInputDTO,        enableSubscriptions: true,      }],    }),  ],})export class TodoItemModule {}

Enabling/Disabling Individual Subscriptions#

You also have the option to selectively enable or disable individual subscriptions.

In this example the updatedMany and deletedMany subscriptions are disabled.

todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemInputDTO } from './dto/todo-item-input.dto';import { TodoItemUpdateDTO } from './dto/todo-item-update.dto';import { TodoItemDTO } from './dto/todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      resolvers: [        {          DTOClass: TodoItemDTO,          EntityClass: TodoItemEntity,          CreateDTOClass: TodoItemInputDTO,          UpdateDTOClass: TodoItemUpdateDTO,          enableSubscriptions: true,          update: { many: { enableSubscriptions: false } },          delete: { many: { enableSubscriptions: false } },        },      ],    }),  ],})export class TodoItemModule {}

Filtering Subscriptions#

The created, updatedOne and deletedOne subscriptions all for a Filter to be passed in filter for events that match the criteria

The filter your provide is the same type that is used when querying for records.

For example to filter for all created TodoItems where the like starts with Foo and is completed, you could do the following.

subscription {  createdTodoItem(    input: { filter: { title: { like: "Foo%" }, completed: { is: true } } }  ) {    id    title    description    completed    created    updated  }}

Custom PubSub Provider#

You can override the default PubSub implementation by creating your own provider and providing it to nestjs-query.

Below is an example provider.

redis-pub-sub.provider.ts
import { pubSubToken } from '@nestjs-query/query-graphql';import { RedisPubSub } from 'graphql-redis-subscriptions';import Redis from 'ioredis';import { Provider } from '@nestjs/common';
export class RedisPubSubProvider {  static provider(): Provider {    return {      provide: pubSubToken(),      useValue: new RedisPubSub({        publisher: new Redis(),        subscriber: new Redis(),      }),    };  }}

In order to let nestjs-query know about the provider you can set the pubSub option.

todo-item/todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemInputDTO } from './dto/todo-item-input.dto';import { TodoItemUpdateDTO } from './dto/todo-item-update.dto';import { TodoItemDTO } from './dto/todo-item.dto';import { TodoItemEntity } from './todo-item.entity';import { RedisPubSubProvider } from '../redis-pub-sub.provider';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      pubSub: RedisPubSubProvider.provider(),      resolvers: [        {          DTOClass: TodoItemDTO,          EntityClass: TodoItemEntity,          CreateDTOClass: TodoItemInputDTO,          UpdateDTOClass: TodoItemUpdateDTO,          enableSubscriptions: true,        },      ],    }),  ],})export class TodoItemModule {}