> ## Documentation Index
> Fetch the complete documentation index at: https://nestjs-query.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Subscriptions

<Note>
  Before reading this it is recommended to read the [nestjs graphql subscriptions
  docs](https://docs.nestjs.com/graphql/subscriptions).
</Note>

`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[​](#enabling-subscriptions "Direct link to 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.

<Tabs>
  <Tab title="NestjsQueryGraphQLModule">
    todo-item.module.ts
  </Tab>

  <Tab title="CRUDResolver">
    ```ts theme={null}
    import { NestjsQueryGraphQLModule } from '@ptc-org/nestjs-query-graphql';
    import { NestjsQueryTypeOrmModule } from '@ptc-org/nestjs-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 {}
    ```
  </Tab>

  <Tab title="Example">
    When manually defining your resolver you must also provide a readonly `pubSub` property. In this the default `PubSub`
    implementation is injected.
  </Tab>

  <Tab title="Example">todo-item.resolver.ts</Tab>

  <Tab title="Example">
    ```ts theme={null}
    import { QueryService, InjectQueryService } from '@ptc-org/nestjs-query-core';
    import { CRUDResolver, InjectPubSub } from '@ptc-org/nestjs-query-graphql';
    import { Resolver } from '@nestjs/graphql';
    import { PubSub } from 'graphql-subscriptions';
    import { TodoItemInputDTO } from './todo-item.input';
    import { TodoItemDTO } from './todo-item.dto';
    import { TodoItemEntity } from './todo-item.entity';

    @Resolver()
    export class TodoItemResolver extends CRUDResolver(TodoItemDTO, {

      CreateDTOClass: TodoItemInputDTO,
      UpdateDTOClass: TodoItemInputDTO,
      enableSubscriptions: true,
    }) {
      constructor(

          @InjectQueryService(TodoItemEntity) readonly service: QueryService<TodoItemEntity>,
          @InjectPubSub() readonly pubSub: PubSub
      ) {
        super(service);
      }
    }
    ```
  </Tab>
</Tabs>

### Enabling/Disabling Individual Subscriptions[​](#enablingdisabling-individual-subscriptions "Direct link to 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.

<Tabs>
  <Tab title="NestjsQueryGraphQLModule">
    todo-item.module.ts
  </Tab>

  <Tab title="CRUDResolver">
    ```ts theme={null}
    import { NestjsQueryGraphQLModule } from '@ptc-org/nestjs-query-graphql';
    import { NestjsQueryTypeOrmModule } from '@ptc-org/nestjs-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 {}
    ```
  </Tab>

  <Tab title="Example">todo-item.resolver.ts</Tab>

  <Tab title="Example">
    ```ts theme={null}
    import { QueryService, InjectQueryService } from '@ptc-org/nestjs-query-core';
    import { CRUDResolver, InjectPubSub } from '@ptc-org/nestjs-query-graphql';
    import { Resolver } from '@nestjs/graphql';
    import { PubSub } from 'graphql-subscriptions';
    import { TodoItemInputDTO } from './todo-item.input';
    import { TodoItemDTO } from './todo-item.dto';
    import { TodoItemEntity } from './todo-item.entity';

    @Resolver()
    export class TodoItemResolver extends CRUDResolver(TodoItemDTO, {

      CreateDTOClass: TodoItemInputDTO,
      UpdateDTOClass: TodoItemInputDTO,
      enableSubscriptions: true,
      update: { many: { enableSubscriptions: false } },
      delete: { many: { enableSubscriptions: false } },
    }) {
      constructor(

          @InjectQueryService(TodoItemEntity) readonly service: QueryService<TodoItemEntity>,
          @InjectPubSub() readonly pubSub: PubSub
      ) {
        super(service);
      }
    }
    ```
  </Tab>
</Tabs>

## Filtering Subscriptions[​](#filtering-subscriptions "Direct link to 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.

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

## Custom PubSub Provider[​](#custom-pubsub-provider "Direct link to 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

```ts theme={null}
import { pubSubToken } from '@ptc-org/nestjs-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

```ts theme={null}
import { NestjsQueryGraphQLModule } from '@ptc-org/nestjs-query-graphql'
import { NestjsQueryTypeOrmModule } from '@ptc-org/nestjs-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 {}
```

[Edit this page](https://github.com/tripss/nestjs-query/edit/master/docs/graphql/subscriptions)
