Skip to content

Commit

Permalink
add postgraphile pro (#539)
Browse files Browse the repository at this point in the history
* Revert "Revert "Revert "Revert "pagination limit""" (#531)"

This reverts commit 331255d.

* log req body
boudra authored Apr 24, 2024
1 parent e161b3f commit 2a92e48
Showing 4 changed files with 108 additions and 20 deletions.
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
"types": "dist/src/indexer/index.d.ts",
"scripts": {
"start": "node dist/src/index.js",
"dev": "sh -c 'tsx watch src/index.ts --http --indexer --http-wait-for-sync=true $@ | pino-pretty' --",
"dev": "sh -c 'tsx watch src/index.ts --http --indexer --http-wait-for-sync=false $@ | pino-pretty' --",
"build": "tsc",
"lint": "eslint src && prettier --check src",
"format": "prettier --write src",
@@ -26,6 +26,7 @@
"license": "ISC",
"dependencies": {
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
"@graphile/pro": "^1.0.4",
"@isaacs/ttlcache": "^1.4.1",
"@sentry/node": "^7.51.0",
"@teamawesome/tiny-batch": "^1.0.7",
17 changes: 15 additions & 2 deletions src/http/app.ts
Original file line number Diff line number Diff line change
@@ -49,9 +49,22 @@ export const createHttpApi = (config: HttpApiConfig): HttpApi => {

app.set("trust proxy", true);
app.use(cors());
// @ts-expect-error Something wrong with pino-http typings
app.use(createHttpLogger({ logger: config.logger }));
app.use(express.json());
app.use(
// @ts-expect-error Something wrong with pino-http typings
createHttpLogger({
logger: config.logger,
serializers: {
// @ts-expect-error Something wrong with pino-http typings
req(req) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
req.body = req.raw.body;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return req;
},
},
})
);

const api = createApiHandler(config);

47 changes: 30 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -16,9 +16,10 @@ import { throttle } from "throttle-debounce";
import * as pg from "pg";
const { Pool, types } = pg.default;

import { postgraphile } from "postgraphile";
import { postgraphile, makePluginHook } from "postgraphile";
import ConnectionFilterPlugin from "postgraphile-plugin-connection-filter";
import PgSimplifyInflectorPlugin from "@graphile-contrib/pg-simplify-inflector";
import GraphilePro from "@graphile/pro";

import { createPassportProvider, PassportProvider } from "./passport/index.js";

@@ -45,6 +46,20 @@ import { IndexerEvents } from "chainsauce/dist/indexer.js";

const RESOURCE_MONITOR_INTERVAL_MS = 1 * 60 * 1000; // every minute

function createPgPool(url: string): pg.Pool {
return new Pool({
connectionString: url,
max: 15,

// Maximum number of milliseconds a client in the pool is allowed to be idle before it is closed
idleTimeoutMillis: 30_000,
keepAlive: true,

// Maximum number of milliseconds to wait for acquiring a client from the pool
connectionTimeoutMillis: 5_000,
});
}

async function main(): Promise<void> {
const config = getConfig();

@@ -85,17 +100,15 @@ async function main(): Promise<void> {
await fs.mkdir(config.cacheDir, { recursive: true });
}

const databaseConnectionPool = new Pool({
connectionString: config.databaseUrl,
// Maximum number of connections in the pool
max: 15,
const databaseConnectionPool = createPgPool(config.databaseUrl);

// Maximum number of milliseconds a client in the pool is allowed to be idle before it is closed
idleTimeoutMillis: 30_000,
const readOnlyDatabaseUrl = new URL(config.databaseUrl);
readOnlyDatabaseUrl.port = "5433";

// Maximum number of milliseconds to wait for acquiring a client from the pool
connectionTimeoutMillis: 5_000,
});
const readOnlyDatabaseConnectionPool =
process.env.FLY_PROCESS_GROUP === "web"
? createPgPool(readOnlyDatabaseUrl.toString())
: databaseConnectionPool;

const db = new Database({
logger: baseLogger.child({ subsystem: "Database" }),
@@ -262,9 +275,10 @@ async function main(): Promise<void> {
...(config.httpServerWaitForSync ? [indexChainsPromise] : []),
]);

// TODO: use read only connection, use separate pool?
const pluginHook = makePluginHook([GraphilePro.default]);

const graphqlHandler = postgraphile(
databaseConnectionPool,
readOnlyDatabaseConnectionPool,
config.databaseSchemaName,
{
watchPg: false,
@@ -275,6 +289,7 @@ async function main(): Promise<void> {
disableDefaultMutations: true,
dynamicJson: true,
bodySizeLimit: "100kb", // response body limit
pluginHook,
// disableQueryLog: false,
// allowExplain: (req) => {
// return true;
@@ -305,11 +320,9 @@ async function main(): Promise<void> {
"contains",
],
},

// TODO: buy pro version?
// defaultPaginationCap: 1000,
// readOnlyConnection: true,
// graphqlDepthLimit: 2
defaultPaginationCap: -1,
graphqlCostLimit: -1,
graphqlDepthLimit: 4,
}
);

0 comments on commit 2a92e48

Please sign in to comment.