0.4.1 • Published 2 years ago

pg-gql-pubsub v0.4.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

pg-gql-pubsub

Full type-safe PostgreSQL PubSub (using NOTIFY and LISTEN) with smart reconnection and specially designed for GraphQL API Usage, powered mainly by the great library: @imqueue/pg-pubsub

https://npm.im/pg-gql-pubsub

Check graphql-ez 💯

pnpm add pg-gql-pubsub
yarn add pg-gql-pubsub
npm install pg-gql-pubsub

Usage

import { CreatePubSub } from "pg-gql-pubsub";

declare module "pg-gql-pubsub" {
  interface Channels {
    notification: string;
  }
}

export const pubSub = CreatePubSub({
  connectionString: process.env.DATABASE_URL,
});

// ...

pubSub.subscribe("notification").then(async (iterator) => {
  for await (const data of iterator) {
    // data <=> string
    console.log(data);
  }
});

// ...

pubSub.publish("notification", "Hello World");

With GraphQL Code Generator

// Assumming `type Subscription { notification: String! }`

import type { Subscription } from "../generated/graphql.ts";
import { CreatePubSub } from "pg-gql-pubsub";

type PubSubData = { [k in keyof Subscription]: Pick<Subscription, k> };

declare module "pg-gql-pubsub" {
  interface Channels extends PubSubData {}
}

export const pubSub = CreatePubSub({
  connectionString: process.env.DATABASE_URL,
});

// ...

const resolvers = {
  Subscription: {
    notification: {
      subscribe(_root, _args, { pubSub }) {
        // Assumming you already added the pubSub to your context
        return pubSub.subscribe("notification");
      },
    },
  },
};

for await (const data of pubSub.subscribe("notification")) {
  // data <=> { notification: "Hello World" }
  console.log(data);
}

// ...

pubSub.publish("notification", { notification: "Hello World" });
0.4.1

2 years ago

0.4.0

2 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago