Register PubSub as a provider using an injection token. In the mutation, publish the event after the write. In the subscription resolver method, return pubSub.asyncIterator() with the event name. The @Subscription() return type arrow function and the event payload key must match the asyncIterator event name.
Register PubSub with an injection token — inject it into resolvers via @Inject(PUB_SUB).
The object key in publish() must match the subscription field name: { postCreated: post }.
asyncIterator() argument must match the event name used in publish().
In-memory PubSub works only in single-process deployments — use Redis PubSub for multiple instances.
Publish after the database write completes — never publish on a failed or rolled-back write.