Skip to content

Commit

Permalink
feat(subscriptions): pg-pubsub module (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Feb 21, 2019
1 parent 86ef40f commit 7506da1
Show file tree
Hide file tree
Showing 20 changed files with 2,951 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/graphile-build/src/SchemaBuilder.d.ts
Expand Up @@ -18,7 +18,7 @@ import { EventEmitter } from "events";
type mixed = {} | string | number | boolean | undefined | null;

export interface Options {
[str: string]: mixed;
[str: string]: any;
}

export interface Plugin {
Expand Down
1 change: 1 addition & 0 deletions packages/lds/package.json
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"db:init": "dropdb --if-exists lds_test && createdb lds_test && psql -X1v ON_ERROR_STOP=1 -v VERBOSITY=verbose -f __tests__/schema.sql lds_test",
"test": "npm run db:init && jest -i",
"tslint": "prettier --list-different 'src/**/*' && tslint --config ../../tslint.json --project tsconfig.json",
"prepack": "tsc",
"watch": "tsc --watch"
},
Expand Down
1 change: 1 addition & 0 deletions packages/pg-pubsub/.gitignore
@@ -0,0 +1 @@
/dist/
24 changes: 24 additions & 0 deletions packages/pg-pubsub/LICENSE.md
@@ -0,0 +1,24 @@
# The MIT License (MIT)

Copyright © `2019` Benjie Gillam

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
59 changes: 59 additions & 0 deletions packages/pg-pubsub/README.md
@@ -0,0 +1,59 @@
# @graphile/pg-pubsub

This PostGraphile [server
plugin](https://www.graphile.org/postgraphile/plugins/) provides a `pubsub`
instance to [schema
plugins](https://www.graphile.org/postgraphile/extending/) that uses
PostgreSQL `LISTEN`/`NOTIFY` to provide realtime features.

Also adds support for `@pgSubscriptions` directive to easily define your own
subscriptions using LISTEN/NOTIFY with `makeExtendSchemaPlugin`; and adds the
`--simple-subscriptions` feature which, when enabled, adds a simple `listen`
subscription field to your GraphQL API.

It's intended that you use this plugin as a provider of realtime data to
other plugins which can use it to add subscription fields to your API.

For full documentation, see: https://www.graphile.org/postgraphile/subscriptions/

## Usage

CLI:

```
yarn add @graphile/pg-pubsub
postgraphile \
--plugins @graphile/pg-pubsub \
--subscriptions \
--simple-subscriptions \
-c postgres:///mydb
```

Library:

```js
const express = require("express");
const { postgraphile, makePluginHook } = require("postgraphile");
const { default: PgPubsub } = require("@graphile/pg-pubsub");

const pluginHook = makePluginHook([PgPubsub]);

const postgraphileOptions = {
pluginHook,
subscriptions: true, // Enable PostGraphile websocket capabilities
simpleSubscriptions: true, // Add the `listen` subscription field
websocketMiddlewares: [
// Add whatever middlewares you need here, note that they should only
// manipulate properties on req/res, they must not sent response data. e.g.:
//
// require('express-session')(),
// require('passport').initialize(),
// require('passport').session(),
],
};

const app = express();
app.use(postgraphile(databaseUrl, "app_public", postgraphileOptions));
app.listen(parseInt(process.env.PORT, 10) || 3000);
```

0 comments on commit 7506da1

Please sign in to comment.