-
-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add emulation of
simpleSubscriptions: true
to V4 preset (#1878)
- Loading branch information
Showing
11 changed files
with
129 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"postgraphile": patch | ||
--- | ||
|
||
Add emulation for `--simple-subscriptions` to V4 preset. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"graphile-build": patch | ||
--- | ||
|
||
Export getNodeIdHandlerByTypeName to make writing plugins easier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
postgraphile/postgraphile/src/plugins/PgV4SimpleSubscriptionsPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { JSONParseStep } from "@dataplan/json"; | ||
import { jsonParse } from "@dataplan/json"; | ||
import { context, lambda, listen, node } from "grafast"; | ||
import { EXPORTABLE, gql, makeExtendSchemaPlugin } from "graphile-utils"; | ||
|
||
export const PgV4SimpleSubscriptionsPlugin = makeExtendSchemaPlugin((build) => { | ||
const nodeIdHandlerByTypeName = build.getNodeIdHandlerByTypeName?.(); | ||
return { | ||
typeDefs: [ | ||
gql` | ||
extend type Subscription { | ||
listen(topic: String!): ListenPayload | ||
} | ||
type ListenPayload { | ||
event: String | ||
} | ||
`, | ||
...// Only add the relatedNode if supported | ||
(nodeIdHandlerByTypeName | ||
? [ | ||
gql` | ||
extend type ListenPayload { | ||
relatedNode: Node | ||
relatedNodeId: ID | ||
} | ||
`, | ||
] | ||
: []), | ||
], | ||
plans: { | ||
Subscription: { | ||
listen: { | ||
subscribePlan: EXPORTABLE( | ||
(context, jsonParse, lambda, listen) => | ||
function subscribePlan(_$root, { $topic }) { | ||
const $pgSubscriber = context().get("pgSubscriber"); | ||
const $derivedTopic = lambda( | ||
$topic, | ||
(topic) => `postgraphile:${topic}`, | ||
); | ||
return listen($pgSubscriber, $derivedTopic, jsonParse); | ||
}, | ||
[context, jsonParse, lambda, listen], | ||
), | ||
plan: EXPORTABLE( | ||
() => | ||
function plan($event) { | ||
return $event; | ||
}, | ||
[], | ||
), | ||
}, | ||
}, | ||
ListenPayload: { | ||
event($event) { | ||
return $event.get("event"); | ||
}, | ||
...(nodeIdHandlerByTypeName | ||
? { | ||
relatedNodeId($event) { | ||
return nodeIdFromEvent($event); | ||
}, | ||
relatedNode($event) { | ||
const $nodeId = nodeIdFromEvent($event); | ||
return node(nodeIdHandlerByTypeName, $nodeId); | ||
}, | ||
} | ||
: null), | ||
}, | ||
}, | ||
}; | ||
}, "PgV4SimpleSubscriptionsPlugin"); | ||
|
||
// 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. | ||
function nodeIdFromEvent($event: JSONParseStep<{ __node__?: any[] }>) { | ||
const $nodeObj = $event.get("__node__"); | ||
const $nodeId = lambda($nodeObj, nodeObjToNodeId); | ||
return $nodeId; | ||
} | ||
|
||
// base64JSON codec copy | ||
function nodeObjToNodeId(obj: any[] | undefined): string | null { | ||
if (!obj) return null; | ||
return Buffer.from(JSON.stringify(obj), "utf8").toString("base64"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters