Skip to content

Instantly share code, notes, and snippets.

@benjie
Last active November 8, 2024 11:22
Show Gist options
  • Save benjie/78979963a26947672da77f331a37c6d8 to your computer and use it in GitHub Desktop.
Save benjie/78979963a26947672da77f331a37c6d8 to your computer and use it in GitHub Desktop.
const MyPgContextPlugin: GraphileConfig.Plugin = {
name: "MyPgContextPlugin",
description:
"Extends the runtime GraphQL context with details needed to support your configured pgServices",
version: "0.0.0",
grafast: {
middleware: {
prepareArgs(next, { args }) {
if (!args.contextValue) args.contextValue = Object.create(null);
const { resolvedPreset: { pgServices: [pgService] }, requestContext: ctx } = args;
const contextValue = args.contextValue as Record<string, any>;
const { pgSettings } = pgService;
contextValue.pgSettings = pgSettings(ctx ?? EMPTY_OBJECT);
const { pgSubscriber, withPgClient } = getContextValuesForTenant(ctx.expressv4.req.tenant_id);
contextValue.pgSubscriber = pgSubscriber;
contextValue.withPgClient = withPgClient;
return next();
},
},
},
};
// Replaces PgContextPlugin with MyPgContextPlugin
export const MultitenantPreset: GraphileConfig.Preset = {
disablePlugins: ['PgContextPlugin'],
plugins: [MyPgContextPlugin],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment