2.0.5 • Published 12 months ago

graphql-pg-subscriptions v2.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

graphql-pg-subscriptions

A graphql subscriptions implementation using postgres and apollo's graphql-subscriptions.

This package implements the PubSubEngine Interface from the graphql-subscriptions package and also the new AsyncIterator interface. It allows you to connect your subscriptions manger to a postgres based Pub Sub mechanism to support multiple subscription manager instances.

Installation

npm i graphql-pg-subscriptions

Usage

First of all, follow the instructions in graphql-subscriptions to add subscriptions to your app.

Afterwards replace PubSub with PostgresPubSub:

// Before
import { PubSub } from "graphql-subscriptions";

export const pubsub = new PubSub();
// After
import { PostgresPubSub } from "graphql-pg-subscriptions";

export const pubsub = new PostgresPubSub();

This library uses node-postgres to connect to PostgreSQL. If you want to customize connection options, please refer to their connection docs.

import { PostgresPubSub } from "graphql-postgres-subscriptions";
import { Client } from "pg";

const client = new Client({
    user: 'dbuser',
    host: 'database.server.com',
    database: 'mydb',
    password: 'secretpassword',
    port: 3211,
});
client.connect();
const pubsub = new PostgresPubSub({ client });

Important: Don't pass clients from pg's Pool to PostgresPubSub. As node-postgres creator states in this StackOverflow answer, the client needs to be around and not shared so pg can properly handle NOTIFY messages (which this library uses under the hood)

commonMessageHandler

The second argument to new PostgresPubSub() is the commonMessageHandler. The common message handler gets called with the received message from PostgreSQL. You can transform the message before it is passed to the individual filter/resolver methods of the subscribers. This way it is for example possible to inject one instance of a DataLoader which can be used in all filter/resolver methods.

const getDataLoader = () => new DataLoader(...)
const commonMessageHandler = ({attributes: {id}, data}) => ({id, dataLoader: getDataLoader()})
const pubsub = new PostgresPubSub({ client, commonMessageHandler });
export const resolvers = {
  Subscription: {
    somethingChanged: {
      resolve: ({ id, dataLoader }) => dataLoader.load(id)
    }
  }
};

Error handling

PostgresPubSub instances emit a special event called "error". This event's payload is an instance of Javascript's Error. You can get the error's text using error.message.

const ps = new PostgresPubSub({ client });

ps.subscribe("error", err => {
  console.log(err.message); // -> "payload string too long"
}).then(() => ps.publish("a", "a".repeat(9000)));

For example you can log all error messages (including stack traces and friends) using something like this:

ps.subscribe("error", console.error);

This is a forked version of GraphQLCollege/graphql-postgres-subscriptions, where I add typescript and dependency error.

Stay in touch