graphql-multiplex-subscriptions

2.0.0 • Public • Published

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,
})

Package Sidebar

Install

npm i graphql-multiplex-subscriptions

Weekly Downloads

21

Version

2.0.0

License

MIT

Unpacked Size

354 kB

Total Files

11

Last publish

Collaborators

  • jedwards1211