While basically modules should be a standalone unit of code, it's not possible to create completely standalone modules. Modules need to interact with each other, execute functions, send messages and more. GraphQL Modules provides multiple ways of doing it, and you should pick the right one according to your needs.
Using Direct Dependency Injection
If your modules are coupled, you can use direct dependency injection.
Using Injection Tokens
If you wish to make a module communicate with other modules without direct imports, you can use injection tokens:
Then, your app or other modules can use the following to implement it:
And provide it using
Using a Communication Bridge: PubSub
GraphQL Modules can work with
PubSub mechanism for dealing with messages between modules.
It's useful when you want to notify other modules of something, without knowing them directly.
PubSub is implemented as a simple Pub/Sub mechanism for publishing/subscribing to messages.
First, you need to tell
GraphQLModule how to transmit your messages. graphql-subscriptions provides a simple
PubSub implementation based on
To use it, pass the
PubSub class as a provider in a shared
And import this common module when you want to use
PubSub, you can do the following:
This kind of communication between modules is useful for implementing notifications, auditing, logging, etc.
It's also useful for implementing communication between GraphQL Modules servers. There are various PubSub implementations based on EventEmitter, Redis and RabbitMQ.
Existing Implementations for PubSub
PubSub can be replaced by another implementations. The following are existing ready-to-use implementations: