diff --git a/apps/metamigo-api/src/app/plugins/hapi-postgraphile.ts b/apps/metamigo-api/src/app/plugins/hapi-postgraphile.ts new file mode 100644 index 0000000..7f62fd7 --- /dev/null +++ b/apps/metamigo-api/src/app/plugins/hapi-postgraphile.ts @@ -0,0 +1,66 @@ +import type * as Hapi from "@hapi/hapi"; +import { IAppConfig } from "@digiresilience/metamigo-config"; +import { postgraphile } from 'postgraphile'; +import { getPostGraphileOptions } from "@digiresilience/metamigo-db"; + + +export interface HapiPostgraphileOptions { + pgConfig: any; + schemaOptions: any; + schemaName: string; +} + +const PostgraphilePlugin: Hapi.Plugin = { + name: 'postgraphilePlugin', + version: '1.0.0', + register: async function (server, options: HapiPostgraphileOptions) { + + const postgraphileMiddleware = postgraphile(options.pgConfig, options.schemaName, options.schemaOptions); + + server.route({ + method: ['POST'], + path: '/graphql', + options: { + payload: { + parse: false, // this disables payload parsing + output: 'stream' // ensures the payload is a readable stream + }, + }, + handler: (request, h) => { + return new Promise((resolve, reject) => { + postgraphileMiddleware(request.raw.req, request.raw.res, (error) => { + if (error) { + reject(error); + } else { + // PostGraphile responds directly to the request + resolve(h.abandon); + } + }); + }); + }, + }); + }, +}; + +export const registerPostgraphile = async ( + server: Hapi.Server, + config: IAppConfig +): Promise => { + + + await server.register({ + plugin: PostgraphilePlugin, + options: { + pgConfig: config.postgraphile.authConnection, + schemaName: "app_public", + schemaOptions: { + ...getPostGraphileOptions(), + jwtAudiences: [config.nextAuth.audience], + jwtSecret: "", + // unauthenticated users will hit the database with this role + pgDefaultRole: "app_anonymous", + }, + }, + }); +}; + diff --git a/apps/metamigo-api/src/app/plugins/index.ts b/apps/metamigo-api/src/app/plugins/index.ts index ee53098..c687c8f 100644 --- a/apps/metamigo-api/src/app/plugins/index.ts +++ b/apps/metamigo-api/src/app/plugins/index.ts @@ -11,6 +11,8 @@ import { registerCloudflareAccessJwt } from "./cloudflare-jwt.js"; import { registerAuthBearer } from "./auth-bearer.js"; import pg from "pg-promise/typescript/pg-subset"; +import { registerPostgraphile } from "./hapi-postgraphile.js"; + export const register = async ( server: Hapi.Server, config: IAppConfig @@ -36,4 +38,5 @@ export const register = async ( await registerSwagger(server); await registerCloudflareAccessJwt(server, config); await registerAuthBearer(server, config); + await registerPostgraphile(server, config) }; diff --git a/apps/metamigo-api/src/server/manifest.ts b/apps/metamigo-api/src/server/manifest.ts index 14a0a8e..347e002 100644 --- a/apps/metamigo-api/src/server/manifest.ts +++ b/apps/metamigo-api/src/server/manifest.ts @@ -2,9 +2,6 @@ import * as Glue from "@hapi/glue"; import * as Metamigo from "@digiresilience/metamigo-common"; import * as Blipp from "blipp"; import HapiBasic from "@hapi/basic"; -//import HapiJwt from "hapi-auth-jwt2"; -//import HapiPostgraphile from "hapi-postgraphile"; -import { getPostGraphileOptions } from "@digiresilience/metamigo-db"; import AppPlugin from "../app/index.js"; import type { IAppConfig } from "../config.js"; @@ -24,9 +21,6 @@ const build = async (config: IAppConfig): Promise => { }, register: { plugins: [ - // jwt plugin, required for our jwt auth plugin - //{ plugin: HapiJwt }, - // Blipp prints the nicely formatted list of endpoints at app boot { plugin: Blipp }, @@ -43,33 +37,6 @@ const build = async (config: IAppConfig): Promise => { config, }, }, - // load Postgraphile - - /* - { - plugin: HapiPostgraphile, - options: { - route: { - path: "/graphql", - options: { - auth: { - strategies: ["session-id-bearer-token"], - mode: "optional", - }, - }, - }, - pgConfig: config.postgraphile.authConnection, - schemaName: "app_public", - schemaOptions: { - ...getPostGraphileOptions(), - jwtAudiences: [config.nextAuth.audience], - jwtSecret: "", - // unauthenticated users will hit the database with this role - pgDefaultRole: "app_anonymous", - }, - }, - }, - */ ], }, }; diff --git a/package.json b/package.json index 4b1d6bb..362580c 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,6 @@ "typeorm": { "pg": "^8.11.0" }, - "hapi-postgraphile": { - "pg": "^8.11.0" - }, "graphql": "15.8.0" }, "devDependencies": {