UNPKG

postgraphile

Version:

Automatic, high performance, and highly customizable GraphQL API for PostgreSQL

87 lines (86 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PgV4SimpleSubscriptionsPlugin = void 0; const json_1 = require("@dataplan/json"); const grafast_1 = require("grafast"); const graphile_utils_1 = require("graphile-utils"); exports.PgV4SimpleSubscriptionsPlugin = (0, graphile_utils_1.extendSchema)((build) => { const { grafast: { get }, } = build; const nodeIdHandlerByTypeName = build.getNodeIdHandlerByTypeName?.(); return { typeDefs: [ (0, graphile_utils_1.gql) ` extend type Subscription { listen( """ If true, this subscription will trigger an event as soon as it initiates. """ initialEvent: Boolean! = false topic: String! ): ListenPayload } type ListenPayload { event: String } `, ... // Only add the relatedNode if supported (nodeIdHandlerByTypeName ? [ (0, graphile_utils_1.gql) ` extend type ListenPayload { relatedNode: Node relatedNodeId: ID } `, ] : []), ], objects: { Subscription: { plans: { listen: { subscribePlan: (0, graphile_utils_1.EXPORTABLE)((context, initialEvent, jsonParse, lambda, listen) => function subscribePlan(_$root, { $topic, $initialEvent: $includeInitialEvent }) { const $pgSubscriber = context().get("pgSubscriber"); const $derivedTopic = lambda($topic, (topic) => `postgraphile:${topic}`); // Create an initial event if enabled const $initialEvent = lambda($includeInitialEvent, initialEvent, true); return listen($pgSubscriber, $derivedTopic, jsonParse, $initialEvent); }, [grafast_1.context, initialEvent, json_1.jsonParse, grafast_1.lambda, grafast_1.listen]), plan: (0, graphile_utils_1.EXPORTABLE)(() => function plan($event) { return $event; }, []), }, }, }, ListenPayload: { plans: { event: (0, graphile_utils_1.EXPORTABLE)((get) => ($event) => { return get($event, "event"); }, [get]), ...(nodeIdHandlerByTypeName ? { relatedNodeId: nodeIdFromEvent, relatedNode: nodeIdFromEvent, } : null), }, }, }, }; }, "PgV4SimpleSubscriptionsPlugin"); // base64JSON codec copy const nodeObjToNodeId = (0, graphile_utils_1.EXPORTABLE)(() => function nodeObjToNodeId(obj) { if (!obj) return null; return Buffer.from(JSON.stringify(obj), "utf8").toString("base64"); }, [], "nodeObjToNodeId"); // WARNING: this function assumes that you're using the `base64JSON` NodeID codec, which was the case for PostGraphile V4. If you are not doing so, YMMV. const nodeIdFromEvent = (0, graphile_utils_1.EXPORTABLE)((get, lambda, nodeObjToNodeId) => function nodeIdFromEvent($event) { const $nodeObj = get($event, "__node__"); const $nodeId = lambda($nodeObj, nodeObjToNodeId); return $nodeId; }, [grafast_1.get, grafast_1.lambda, nodeObjToNodeId], "nodeIdFromEvent"); function initialEvent(obj) { return obj ? `{"event":"initial"}` : undefined; } //# sourceMappingURL=PgV4SimpleSubscriptionsPlugin.js.map