graphql-amqp-subscriptions
Version:
GraphQL AMQP Subscriptions
42 lines • 1.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AMQPPublisher = void 0;
class AMQPPublisher {
logger;
connection;
exchange;
channelPromise = null;
constructor(config, logger) {
this.logger = logger;
this.connection = config.connection;
this.exchange = {
name: 'graphql_subscriptions',
type: 'topic',
options: {
durable: false,
autoDelete: false
},
...config.exchange
};
}
async publish(routingKey, data, options) {
const channel = await this.getOrCreateChannel();
await channel.assertExchange(this.exchange.name, this.exchange.type, this.exchange.options);
channel.publish(this.exchange.name, routingKey, Buffer.from(JSON.stringify(data)), options);
this.logger('Message sent to Exchange "%s" with Routing Key "%s" (%j)', this.exchange.name, routingKey, data);
}
async getOrCreateChannel() {
if (!this.channelPromise) {
this.channelPromise = this.connection.createChannel()
.then(channel => {
channel.on('error', (error) => {
this.logger('Publisher channel error: "%j"', error);
});
return channel;
});
}
return this.channelPromise;
}
}
exports.AMQPPublisher = AMQPPublisher;
//# sourceMappingURL=publisher.js.map