Skip to content

Commit 620fc9b

Browse files
committed
Make it so you can pass pgClient into execution
1 parent ab963aa commit 620fc9b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

__tests__/helpers.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// @ts-check
2-
const { makePgService } = require("postgraphile/adaptors/pg");
2+
const {
3+
makePgService,
4+
makeWithPgClientViaPgClientAlreadyInTransaction,
5+
} = require("postgraphile/adaptors/pg");
36
const pg = require("pg");
47
const { readFile } = require("fs/promises");
58
const { default: PgOrderByRelatedPlugin } = require("../dist/index.js");
@@ -19,10 +22,28 @@ afterAll(() => {
1922
pool?.end();
2023
});
2124

25+
/** @type {GraphileConfig.Plugin} */
26+
const ShoveClientIntoContextPlugin = {
27+
name: "ShoveClientIntoContextPlugin",
28+
29+
grafast: {
30+
middleware: {
31+
prepareArgs(next, event) {
32+
const pgClient = event.args.contextValue.pgClient;
33+
if (pgClient) {
34+
event.args.contextValue.withPgClient =
35+
makeWithPgClientViaPgClientAlreadyInTransaction(pgClient, true);
36+
}
37+
return next();
38+
},
39+
},
40+
},
41+
};
42+
2243
/** @type {(schemas: string[], options: import("postgraphile/presets/v4").V4Options) => GraphileConfig.Preset} */
2344
const makePreset = (schemas, options) => ({
2445
extends: [makeV4Preset(options)],
25-
plugins: [PgOrderByRelatedPlugin],
46+
plugins: [PgOrderByRelatedPlugin, ShoveClientIntoContextPlugin],
2647
pgServices: [
2748
makePgService({
2849
pool,

__tests__/integration/queries.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ beforeAll(() => {
6969
const result = await grafast({
7070
schema,
7171
source,
72+
variableValues: {},
73+
contextValue: {
74+
pgClient,
75+
},
7276
resolvedPreset,
7377
requestContext: {},
7478
});

__tests__/types.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare global {
2+
namespace Grafast {
3+
interface Context {
4+
pgClient?: import("pg").PoolClient;
5+
}
6+
}
7+
}
8+
9+
// TypeScript hack since this has to be a module
10+
export const hack = 1;

0 commit comments

Comments
 (0)