Skip to content

Commit

Permalink
Use PgContextPlugin in example schema runner
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Dec 4, 2023
1 parent b0c4170 commit 0f3168d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
39 changes: 23 additions & 16 deletions grafast/dataplan-pg/scripts/runExampleSchema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@ import { grafast } from "grafast";
import { isAsyncIterable } from "iterall";
import JSON5 from "json5";
import { strict as assert } from "node:assert";
import pg from "pg";

import { createWithPgClient } from "../dist/adaptors/pg.js";
import { schema } from "./exampleSchemaExport.mjs";
import { resolvePresets } from "graphile-config";
import { PgContextPlugin } from "@dataplan/pg";
import { makePgService } from "@dataplan/pg/adaptors/pg";

const pool = new pg.Pool({
connectionString: process.env.TEST_DATABASE_URL || "graphile_crystal",
});
const connectionString =
process.env.TEST_DATABASE_URL || "postgres:///graphile_crystal";
const pgService = makePgService({ connectionString });
const preset = {
plugins: [PgContextPlugin],
pgServices: [pgService],
grafast: {
context() {
return {
pgSettings: {
timezone: "UTC",
},
};
},
},
};
const resolvedPreset = resolvePresets([preset]);

async function runTestQuery(basePath) {
const source = await readFile(`${basePath}.test.graphql`, "utf8");
const expectedData = JSON5.parse(await readFile(`${basePath}.json5`, "utf8"));

const withPgClient = createWithPgClient({
pool,
});

const result = await grafast({
schema,
source,
contextValue: {
pgSettings: {
timezone: "UTC",
},
withPgClient,
},
resolvedPreset,
requestContext: {},
});
const operationType = "query";

Expand Down Expand Up @@ -158,5 +165,5 @@ try {
await runTestQuery(basePath);
}
} finally {
pool.end();
await pgService.release?.();
}
24 changes: 17 additions & 7 deletions grafast/dataplan-pg/src/adaptors/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,12 @@ export function makePgService(
);
}
const Pool = pg.Pool ?? (pg as any).default?.Pool;
const pool =
options.pool ??
new Pool({
connectionString,
});
const releasers: (() => void | PromiseLike<void>)[] = [];
let pool = options.pool;
if (!pool) {
pool = new Pool({ connectionString });
releasers.push(() => pool!.end());
}
if (!options.pool) {
// If you pass your own pool, you're responsible for doing this yourself
pool.on("connect", (client) => {
Expand All @@ -746,8 +747,11 @@ export function makePgService(
console.error("Client error (in pool)", e);
});
}
const pgSubscriber =
options.pgSubscriber ?? (pubsub ? new PgSubscriber(pool) : null);
let pgSubscriber = options.pgSubscriber ?? null;
if (!pgSubscriber && pubsub) {
pgSubscriber = new PgSubscriber(pool);
releasers.push(() => pgSubscriber!.release?.());
}
const service: GraphileConfig.PgServiceConfiguration = {
name,
schemas: Array.isArray(schemas) ? schemas : [schemas ?? "public"],
Expand All @@ -762,6 +766,12 @@ export function makePgService(
pool,
superuserConnectionString,
},
async release() {
// Release in reverse order
for (const releaser of [...releasers].reverse()) {
await releaser();
}
},
};
return service;
}
6 changes: 6 additions & 0 deletions grafast/dataplan-pg/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ declare global {
Grafast.Context & object,
GrafastSubscriber<any> | null | undefined
>;

/**
* Call this when you no longer need this service configuration any more;
* releases any created resources (e.g. connection pools).
*/
release?: () => void | PromiseLike<void>;
}

interface Preset {
Expand Down
1 change: 1 addition & 0 deletions grafast/grafast/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ export type GrafastSubscriber<
subscribe<TTopic extends keyof TTopics = keyof TTopics>(
topic: TTopic,
): PromiseOrDirect<AsyncIterableIterator<TTopics[TTopic]>>;
release?(): PromiseOrDirect<void>;
};

/**
Expand Down

0 comments on commit 0f3168d

Please sign in to comment.