• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

GraphQLCollege/graphql-postgres-subscriptions: A graphql subscriptions implement ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

GraphQLCollege/graphql-postgres-subscriptions

开源软件地址(OpenSource Url):

https://github.com/GraphQLCollege/graphql-postgres-subscriptions

开源编程语言(OpenSource Language):

JavaScript 99.4%

开源软件介绍(OpenSource Introduction):

graphql-postgres-subscriptions

Build Status

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

yarn add graphql-postgres-subscriptions or npm install graphql-postgres-subscriptions --save

Usage

Example app: https://github.com/GraphQLCollege/apollo-subscriptions-example

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-postgres-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.

You have three options:

If you don's send any argument to new PostgresPubSub(), we'll create a postgres client with no arguments.

You can also pass node-postgres connection options to PostgresPubSub.

You can instantiate your own client and pass it to PostgresPubSub. Like this:

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

const client = new Client();
await 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);

Development

This project has an integration test suite that uses jest to make sure everything works correctly.

We use Docker to spin up a PostgreSQL instance before running the tests. To run them, type the following commands:

  • docker-compose build
  • docker-compose run test



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap