Use our own hapi-postgraphile plugin
1. Much much less code 2. The third-party plugin only supported JWT which we no longer use
This commit is contained in:
parent
ffee8f42e4
commit
9b2f08ab7e
4 changed files with 69 additions and 36 deletions
66
apps/metamigo-api/src/app/plugins/hapi-postgraphile.ts
Normal file
66
apps/metamigo-api/src/app/plugins/hapi-postgraphile.ts
Normal file
|
|
@ -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<HapiPostgraphileOptions> = {
|
||||||
|
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<void> => {
|
||||||
|
|
||||||
|
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
@ -11,6 +11,8 @@ import { registerCloudflareAccessJwt } from "./cloudflare-jwt.js";
|
||||||
import { registerAuthBearer } from "./auth-bearer.js";
|
import { registerAuthBearer } from "./auth-bearer.js";
|
||||||
import pg from "pg-promise/typescript/pg-subset";
|
import pg from "pg-promise/typescript/pg-subset";
|
||||||
|
|
||||||
|
import { registerPostgraphile } from "./hapi-postgraphile.js";
|
||||||
|
|
||||||
export const register = async (
|
export const register = async (
|
||||||
server: Hapi.Server,
|
server: Hapi.Server,
|
||||||
config: IAppConfig
|
config: IAppConfig
|
||||||
|
|
@ -36,4 +38,5 @@ export const register = async (
|
||||||
await registerSwagger(server);
|
await registerSwagger(server);
|
||||||
await registerCloudflareAccessJwt(server, config);
|
await registerCloudflareAccessJwt(server, config);
|
||||||
await registerAuthBearer(server, config);
|
await registerAuthBearer(server, config);
|
||||||
|
await registerPostgraphile(server, config)
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,6 @@ import * as Glue from "@hapi/glue";
|
||||||
import * as Metamigo from "@digiresilience/metamigo-common";
|
import * as Metamigo from "@digiresilience/metamigo-common";
|
||||||
import * as Blipp from "blipp";
|
import * as Blipp from "blipp";
|
||||||
import HapiBasic from "@hapi/basic";
|
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 AppPlugin from "../app/index.js";
|
||||||
import type { IAppConfig } from "../config.js";
|
import type { IAppConfig } from "../config.js";
|
||||||
|
|
||||||
|
|
@ -24,9 +21,6 @@ const build = async (config: IAppConfig): Promise<Glue.Manifest> => {
|
||||||
},
|
},
|
||||||
register: {
|
register: {
|
||||||
plugins: [
|
plugins: [
|
||||||
// jwt plugin, required for our jwt auth plugin
|
|
||||||
//{ plugin: HapiJwt },
|
|
||||||
|
|
||||||
// Blipp prints the nicely formatted list of endpoints at app boot
|
// Blipp prints the nicely formatted list of endpoints at app boot
|
||||||
{ plugin: Blipp },
|
{ plugin: Blipp },
|
||||||
|
|
||||||
|
|
@ -43,33 +37,6 @@ const build = async (config: IAppConfig): Promise<Glue.Manifest> => {
|
||||||
config,
|
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",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,6 @@
|
||||||
"typeorm": {
|
"typeorm": {
|
||||||
"pg": "^8.11.0"
|
"pg": "^8.11.0"
|
||||||
},
|
},
|
||||||
"hapi-postgraphile": {
|
|
||||||
"pg": "^8.11.0"
|
|
||||||
},
|
|
||||||
"graphql": "15.8.0"
|
"graphql": "15.8.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue