graphql-multiplex-subscriptions

use different PubSubEngines (e.g. RedisPubSub, PostgresPubSub) for different topics

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
graphql-multiplex-subscriptions
802.0.04 years ago4 years agoMinified + gzip package size for graphql-multiplex-subscriptions in KB

Readme

graphql-multiplex-subscriptions
Build Status Coverage Status semantic-release Commitizen friendly npm version
If you need to use multiple PubSubEngines in your Apollo server (e.g. graphql-redis-subscriptions and graphql-postgres-subscriptions), use this package to coordinate which PubSubEngine handles a given topic.
Usage
Imagine you're currently using graphql-redis-subscriptions, and you set up your pubsub like this:
// Before
import { RedisPubSub } from 'graphql-redis-subscriptions'

export const pubsub = new RedisPubSub()

But then you decide you want to use graphql-postgres-subscriptions for some topics. All you have to do is create a MultiplexPubSub with a selectEngine function that returns which engine you'd like to use for a given topic. For example, you could use postgres for all topics beginning with pg/:
import { RedisPubSub } from 'graphql-redis-subscriptions'
import { PostgresPubSub } from 'graphql-postgres-subscriptions'
import { MultiplexPubSub } from 'graphql-multiplex-subscriptions'

const redisPubSub = new RedisPubSub()
const postgresPubSub = new PostgresPubSub()

export const pubsub = new MultiplexPubSub({
  selectEngine: topic =>
    topic.startsWith('pg/') ? postgresPubSub : redisPubSub,
})

WARNING: selectEngine should always return a PubSubEngine. If it doesn't, you will get TypeErrors!
Since you use a function to select the engine you can use whatever logic you want. So you could just map specific topics to a specific engine:
import { RedisPubSub } from 'graphql-redis-subscriptions'
import { PostgresPubSub } from 'graphql-postgres-subscriptions'
import { MultiplexPubSub } from 'graphql-multiplex-subscriptions'

const redisPubSub = new RedisPubSub()
const postgresPubSub = new PostgresPubSub()

export const pubsub = new MultiplexPubSub({
  selectEngine: topic =>
    topic.matches(/^(user|organization)\//) ? postgresPubSub : redisPubSub,
})