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 thecreateOneorcreateManymutation is called.updatedOne{ObjectName}- published when theupdateOnemutation is called.updatedMany{ObjectName}- published whenupdateManymutation is called.deletedOne{ObjectName}- published when thedeleteOnemutation is called.deletedMany{ObjectName}- published whendeleteManymutation is called.
Enabling Subscriptions
You can enable subscriptions through the auto-generated resolver using theNestjsQueryGraphQLModule 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 thecreateOneorcreateManymutation is called.updatedOneTodoItem- published when theupdateOnemutation is called.updatedManyTodoItems- published whenupdateManymutation is called.deletedOneTodoItem- published when thedeleteOnemutation is called.deletedManyTodoItems- published whendeleteManymutation is called.
- NestjsQueryGraphQLModule
- CRUDResolver
- Example
- Example
- Example
todo-item.module.ts
Enabling/Disabling Individual Subscriptions
You also have the option to selectively enable or disable individual subscriptions. In this example theupdatedMany and deletedMany subscriptions are disabled.
- NestjsQueryGraphQLModule
- CRUDResolver
- Example
- Example
todo-item.module.ts
Filtering Subscriptions
Thecreated, 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.
Custom PubSub Provider
You can override the defaultPubSub implementation by creating your own provider and providing it to nestjs-query.
Below is an example provider.
redis-pub-sub.provider.ts
nestjs-query know about the provider you can set the pubSub option.
todo-item/todo-item.module.ts