Created
April 17, 2023 11:24
-
-
Save hos/2c4b08165165f48bb50d582d472a3808 to your computer and use it in GitHub Desktop.
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
import { jsonParse } from "@dataplan/json"; | |
import { access, context, lambda, listen } from "grafast"; | |
import { gql, makeExtendSchemaPlugin } from "graphile-utils"; | |
const PackagePlugin = makeExtendSchemaPlugin((build) => { | |
const { sql } = build; | |
if (!sql) { | |
throw new Error("sql not found"); | |
} | |
const packageSource = build.input.pgSources.find( | |
(s) => !s.parameters && s.extensions?.pg?.schemaName === 'public' && s.extensions.pg.name === "packages" | |
); | |
if (!packageSource) { | |
throw new Error("Couldn't find source for public.packages"); | |
} | |
return { | |
typeDefs: gql` | |
type PackageSubscriptionPayload { | |
package: Package! | |
event: String | |
} | |
input PackageSubscriptionInput { | |
packageId: String! | |
} | |
extend type Subscription { | |
packageUpdate(input: PackageSubscriptionInput!): PackageSubscriptionPayload | |
} | |
`, | |
plans: { | |
Subscription: { | |
packageUpdate: { | |
subscribePlan(_root, $args) { | |
const $packageId = $args.get(["input", "packageId"]); | |
const $pgSubscriber = context().get("pgSubscriber"); | |
const $topic = lambda( | |
[$packageId], | |
([packageId]) => `graphql:postgraphile:${packageId}`, | |
false | |
); | |
return listen($pgSubscriber, $topic, (e) => e); | |
}, | |
plan($e) { | |
return jsonParse($e); | |
}, | |
}, | |
}, | |
PackageSubscriptionPayload: { | |
package($obj) { | |
const $id = access($obj, "packageId"); | |
return packageSource.get({ id: $id }); | |
}, | |
}, | |
}, | |
}; | |
}); | |
export default PackagePlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment