@pg-lq/postgraphile-plugin

Optimized GraphQL live-queries for Postgraphile, using json-patches.

Showing:

Popularity

Downloads/wk

14

GitHub Stars

7

Maintenance

Last Commit

1mo ago

Contributors

1

Package

Dependencies

3

License

MIT

Type Definitions

Built-In

Tree-Shakeable

Yes?

Categories

Readme

Postgraphile Live Query

Optimized GraphQL live-queries for Postgraphile, using json-patches.

Prerequisites

1) The subscription-lds plugin should already be set up for your Postgraphile server. 2) You must be using Postgraphile v4.12.1 or newer. (the Postgraphile plugin requires the existence of a new hook, which was added only recently)

Integration

Server-side

1) Install: yarn add @pg-lq/postgraphile-plugin (or npm i @pg-lq/postgraphile-plugin) 2) Integrate:

+import {GeneratePatchesPlugin} from "@pg-lq/postgraphile-plugin";

+const pluginHook = makePluginHook([new GeneratePatchesPlugin({...options})]);
const postgraphileMiddleware = postgraphile(
    "DATABASE_URL,
    "SCHEMA_NAME",
    {
        appendPlugins: [
            require("@graphile-contrib/pg-simplify-inflector"),
            require("@graphile/subscriptions-lds").default,
        ],
        live: true,
+       pluginHook,
        [...]
    },
)

Client-side (Apollo)

1) Install: yarn add @pg-lq/apollo-plugin (or npm i @pg-lq/apollo-plugin) 2) Integrate:

+import {ApplyPatchesLink} from "@pg-lq/apollo-plugin";
const apolloClient = new ApolloClient({
-   link: link,
+   link: new ApplyPatchesLink({baseLink: link, ...options}),
    [...]
});

Options

GeneratePatchesPlugin

generatePatchFunc: (previous, current)=>Patch

new GeneratePatchesPlugin({
    // uses the patcher lib that graphql-live-query-patch defaults to (currently fast-json-patch)
    generatePatchFunc: null,

    // uses json-diff-patch (recommended; baseline)
    generatePatchFunc: CreateGeneratePatchFunc_JSONDiffPatch({
        // see here for list of options: https://github.com/benjamine/jsondiffpatch#options
        // note that our wrapper defaults these to support efficient array-item reordering
    }),

    // uses fast-json-patch
    // PRO: json-patches are more readable (at expense of slightly longer length in some cases)
    // CON: no special handling of array-item reordering/deletion (so can be very inefficient for that)
    generatePatchFunc: CreateGeneratePatchFunc_FastJSONPatch(),
})

applyPatchFunc: (previous, patch)=>Object

See generatePatchFunc section for patch-func options, and their tradeoffs. (replacing CreateGeneratePatchFunc_ with CreateApplyPatchFunc_)

Development

1) Download/clone this repo. 2) Run: yarn 3) Run: npm start dev (or ctrl+shift+b in vscode)

Troubleshooting

1) If you're on NodeJS 14.13.0+, you may get an error (when using NodeJS to run server code that uses the @pg-lq/postgraphile-plugin package) about the named-import compare not being found in fast-json-patch. This is due to an issue in the package.json file of fast-json-patch.

See here for more details (and the fix): https://github.com/Starcounter-Jack/JSON-Patch/issues/277

2) You may get a Typescript error "Cannot find namespace 'JSX'." for types.d.ts in apollo-client. If hit, just replace the JSX.Element with any for now. (not sure when it started)

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100