moleculerjs / moleculer-apollo-server

:rocket: Apollo GraphQL server for Moleculer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't override PubSub implementation

jasondarwin opened this issue · comments

The PubSub implementation used by moleculer-apollo-server is not production-grade, as the comment at https://github.com/apollographql/graphql-subscriptions makes clear:

Note that the default PubSub implementation is intended for demo purposes. It only works if you have a single instance of your server and doesn't scale beyond a couple of connections. For production usage you'll want to use one of the PubSub implementations backed by an external store. (e.g. Redis).

In my project, I have to use patch-package to patch moleculer-apollo-server/src/service.js:

diff --git a/node_modules/moleculer-apollo-server/src/service.js b/node_modules/moleculer-apollo-server/src/service.js
index 947c25b..8550c95 100644
--- a/node_modules/moleculer-apollo-server/src/service.js
+++ b/node_modules/moleculer-apollo-server/src/service.js
@@ -13,6 +13,9 @@ const DataLoader = require("dataloader");
 const { makeExecutableSchema } = require("graphql-tools");
 const GraphQL = require("graphql");
 const { PubSub, withFilter } = require("graphql-subscriptions");
+// PubSub is not intended for production use -- see the comment just above:
+// https://github.com/apollographql/graphql-subscriptions#filters
+const { NatsPubSub } = require("@moonwalker/graphql-nats-subscriptions");
 const hash = require("object-hash");
 
 module.exports = function(mixinOptions) {
@@ -596,7 +599,12 @@ module.exports = function(mixinOptions) {
 				);
 
 				try {
-					this.pubsub = new PubSub();
+					// this.pubsub = new PubSub();
+					// PubSub is not intended for production use -- see the comment just above:
+					// https://github.com/apollographql/graphql-subscriptions#filters
+					this.logger.debug("Using NatsPubSub as the PubSub implementation", process.env.TRANSPORTER);
+					this.pubsub = new NatsPubSub({ servers: [process.env.TRANSPORTER] });
+
 					const services = this.broker.registry.getServiceList({ withActions: true });
 					const schema = this.generateGraphQLSchema(services);

It would be much better if this could be done in configuration somehow.

There is a createPubSub method what you can override in your service. Released in 0.3.2