Git Product home page Git Product logo

siamahnaf / graphql-pg-subscriptions Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 10 KB

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

Home Page: https://www.npmjs.com/package/graphql-pg-subscriptions

License: MIT License

JavaScript 100.00%
nextjs reactjs subscriptions

graphql-pg-subscriptions's Introduction

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

graphql-pg-subscriptions's People

Contributors

siamahnaf avatar

Watchers

 avatar

Forkers

jasonshere

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.