chore: added message types to PubSub Service
This commit is contained in:
parent
60e2ef7cda
commit
a0006f73ac
3 changed files with 19 additions and 1 deletions
|
|
@ -4,6 +4,7 @@ import { default as Redis, RedisOptions } from 'ioredis';
|
|||
|
||||
import { RedisPubSub } from 'graphql-redis-subscriptions';
|
||||
import { PubSub as LocalPubSub } from 'graphql-subscriptions';
|
||||
import { MessageType } from './subscriptionTopicsDefs';
|
||||
|
||||
/**
|
||||
* RedisPubSub uses JSON parsing for back and forth conversion, which loses Date objects, hence this reviver brings them back
|
||||
|
|
@ -70,7 +71,10 @@ export class PubSubService implements OnModuleInit {
|
|||
return this.pubsub.asyncIterator(topic, options);
|
||||
}
|
||||
|
||||
async publish(topic: string, payload: any) {
|
||||
async publish<T extends keyof MessageType>(
|
||||
topic: T,
|
||||
payload: MessageType[T],
|
||||
) {
|
||||
await this.pubsub.publish(topic, payload);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
import { UserEnvironment } from '../user-environment/user-environments.model';
|
||||
import { PrimitiveTypes } from '../types/primitiveTypes';
|
||||
|
||||
// A custom message type that defines the topic and the corresponding payload.
|
||||
// For every module that publishes a subscription add its type def and the possible subscription type.
|
||||
export type MessageType = {
|
||||
[
|
||||
topic: `user_environment/${string}/${
|
||||
| 'created'
|
||||
| 'updated'
|
||||
| 'deleted'
|
||||
| 'deleted_many'}`
|
||||
]: UserEnvironment | PrimitiveTypes; // Returning a number hence having a union with `PrimitiveTypes`.
|
||||
};
|
||||
Loading…
Reference in a new issue