Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions graphile/graphile-test/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,28 @@ export const runGraphQLInContext = async <T = ExecutionResult>({
_pgSettings: Record<string, string> | null,
callback: (client: Client) => T | Promise<T>
): Promise<T> => {
// Simply use the test client - it's already in a transaction
// The pgSettings have already been applied above via setContextOnClient
// Augment the client with withTransaction if it doesn't already have it.
// In production, @dataplan/pg provides this method. In tests the raw pg
// Client lacks it, so we implement it via savepoints (which nest cleanly
// inside the test harness's outer transaction).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = pgClient as any;
if (typeof client.withTransaction !== 'function') {
client.withTransaction = async (
cb: (txClient: Client) => unknown | Promise<unknown>
) => {
const sp = `wt_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
await client.query(`SAVEPOINT ${sp}`);
try {
const result = await cb(client);
await client.query(`RELEASE SAVEPOINT ${sp}`);
return result;
} catch (err) {
await client.query(`ROLLBACK TO SAVEPOINT ${sp}`);
throw err;
}
};
}
return callback(pgClient);
};

Expand Down
Loading