{"id":13515993,"url":"https://github.com/GraphQLCollege/graphql-postgres-subscriptions","last_synced_at":"2025-03-31T05:31:10.436Z","repository":{"id":46938696,"uuid":"127340843","full_name":"GraphQLCollege/graphql-postgres-subscriptions","owner":"GraphQLCollege","description":"A graphql subscriptions implementation using postgres and apollo's graphql-subscriptions","archived":false,"fork":false,"pushed_at":"2023-02-23T11:29:09.000Z","size":127,"stargazers_count":172,"open_issues_count":22,"forks_count":18,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-22T06:08:13.536Z","etag":null,"topics":["graphql","graphql-subscriptions","real-time"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GraphQLCollege.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-03-29T19:55:39.000Z","updated_at":"2024-06-28T11:34:35.000Z","dependencies_parsed_at":"2024-01-10T08:03:59.117Z","dependency_job_id":"455a2eb5-9d1e-4158-9b5a-2eabe8ae58e2","html_url":"https://github.com/GraphQLCollege/graphql-postgres-subscriptions","commit_stats":{"total_commits":18,"total_committers":5,"mean_commits":3.6,"dds":0.5,"last_synced_commit":"46113aa0a5254b1c865069b11be448b2a120094b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraphQLCollege%2Fgraphql-postgres-subscriptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraphQLCollege%2Fgraphql-postgres-subscriptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraphQLCollege%2Fgraphql-postgres-subscriptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraphQLCollege%2Fgraphql-postgres-subscriptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GraphQLCollege","download_url":"https://codeload.github.com/GraphQLCollege/graphql-postgres-subscriptions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246150437,"owners_count":20731419,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","graphql-subscriptions","real-time"],"created_at":"2024-08-01T05:01:18.130Z","updated_at":"2025-03-31T05:31:10.406Z","avatar_url":"https://github.com/GraphQLCollege.png","language":"JavaScript","readme":"# graphql-postgres-subscriptions\n\n[![Build Status](https://travis-ci.org/GraphQLCollege/graphql-postgres-subscriptions.svg?branch=master)](https://travis-ci.org/GraphQLCollege/graphql-postgres-subscriptions)\n\nA graphql subscriptions implementation using postgres and apollo's graphql-subscriptions.\n\nThis 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.\n\n## Installation\n\n`yarn add graphql-postgres-subscriptions` or `npm install graphql-postgres-subscriptions --save`\n\n## Usage\n\nExample app: https://github.com/GraphQLCollege/apollo-subscriptions-example\n\nFirst of all, follow the instructions in [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) to add subscriptions to your app.\n\nAfterwards replace `PubSub` with `PostgresPubSub`:\n\n```js\n// Before\nimport { PubSub } from \"graphql-subscriptions\";\n\nexport const pubsub = new PubSub();\n```\n\n```js\n// After\nimport { PostgresPubSub } from \"graphql-postgres-subscriptions\";\n\nexport const pubsub = new PostgresPubSub();\n```\n\nThis library uses [`node-postgres`](https://github.com/brianc/node-postgres) to connect to PostgreSQL. If you want to customize connection options, please refer to their [connection docs](https://node-postgres.com/features/connecting).\n\nYou have three options:\n\nIf you don's send any argument to `new PostgresPubSub()`, we'll create a `postgres` client with no arguments.\n\nYou can also pass [node-postgres connection options](https://node-postgres.com/features/connecting#programmatic) to `PostgresPubSub`.\n\nYou can instantiate your own `client` and pass it to `PostgresPubSub`. Like this:\n\n```js\nimport { PostgresPubSub } from \"graphql-postgres-subscriptions\";\nimport { Client } from \"pg\";\n\nconst client = new Client();\nawait client.connect();\nconst pubsub = new PostgresPubSub({ client });\n```\n\n**Important**: Don't pass clients from `pg`'s `Pool` to `PostgresPubSub`. As [node-postgres creator states in this StackOverflow answer](https://stackoverflow.com/questions/8484404/what-is-the-proper-way-to-use-the-node-js-postgresql-module), the client needs to be around and not shared so pg can properly handle `NOTIFY` messages (which this library uses under the hood)\n\n### commonMessageHandler\n\nThe second argument to `new PostgresPubSub()` is the `commonMessageHandler`. The common message handler gets called with the received message from PostgreSQL.\nYou can transform the message before it is passed to the individual filter/resolver methods of the subscribers.\nThis way it is for example possible to inject one instance of a [DataLoader](https://github.com/facebook/dataloader) which can be used in all filter/resolver methods.\n\n```javascript\nconst getDataLoader = () =\u003e new DataLoader(...)\nconst commonMessageHandler = ({attributes: {id}, data}) =\u003e ({id, dataLoader: getDataLoader()})\nconst pubsub = new PostgresPubSub({ client, commonMessageHandler });\n```\n\n```javascript\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      resolve: ({ id, dataLoader }) =\u003e dataLoader.load(id)\n    }\n  }\n};\n```\n\n## Error handling\n\n`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`.\n\n```js\nconst ps = new PostgresPubSub({ client });\n\nps.subscribe(\"error\", err =\u003e {\n  console.log(err.message); // -\u003e \"payload string too long\"\n}).then(() =\u003e ps.publish(\"a\", \"a\".repeat(9000)));\n```\n\nFor example you can log all error messages (including stack traces and friends) using something like this:\n\n```js\nps.subscribe(\"error\", console.error);\n```\n\n## Development\n\nThis project has an integration test suite that uses [`jest`](https://facebook.github.io/jest/) to make sure everything works correctly.\n\nWe use Docker to spin up a PostgreSQL instance before running the tests. To run them, type the following commands:\n\n- `docker-compose build`\n- `docker-compose run test`\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGraphQLCollege%2Fgraphql-postgres-subscriptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGraphQLCollege%2Fgraphql-postgres-subscriptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGraphQLCollege%2Fgraphql-postgres-subscriptions/lists"}