graphile-test 4.12.1 → 4.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/context.js CHANGED
@@ -170,8 +170,27 @@ const runGraphQLInContext = async ({ input, conn, schema, resolvedPreset, pgServ
170
170
  // instead of getting a new connection from the pool
171
171
  const withPgClientKey = pgService.withPgClientKey ?? 'withPgClient';
172
172
  contextValue[withPgClientKey] = async (_pgSettings, callback) => {
173
- // Simply use the test client - it's already in a transaction
174
- // The pgSettings have already been applied above via setContextOnClient
173
+ // Augment the client with withTransaction if it doesn't already have it.
174
+ // In production, @dataplan/pg provides this method. In tests the raw pg
175
+ // Client lacks it, so we implement it via savepoints (which nest cleanly
176
+ // inside the test harness's outer transaction).
177
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
178
+ const client = pgClient;
179
+ if (typeof client.withTransaction !== 'function') {
180
+ client.withTransaction = async (cb) => {
181
+ const sp = `wt_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
182
+ await client.query(`SAVEPOINT ${sp}`);
183
+ try {
184
+ const result = await cb(client);
185
+ await client.query(`RELEASE SAVEPOINT ${sp}`);
186
+ return result;
187
+ }
188
+ catch (err) {
189
+ await client.query(`ROLLBACK TO SAVEPOINT ${sp}`);
190
+ throw err;
191
+ }
192
+ };
193
+ }
175
194
  return callback(pgClient);
176
195
  };
177
196
  // Check if we're in a transaction by looking at the test client's transaction state
package/esm/context.js CHANGED
@@ -166,8 +166,27 @@ export const runGraphQLInContext = async ({ input, conn, schema, resolvedPreset,
166
166
  // instead of getting a new connection from the pool
167
167
  const withPgClientKey = pgService.withPgClientKey ?? 'withPgClient';
168
168
  contextValue[withPgClientKey] = async (_pgSettings, callback) => {
169
- // Simply use the test client - it's already in a transaction
170
- // The pgSettings have already been applied above via setContextOnClient
169
+ // Augment the client with withTransaction if it doesn't already have it.
170
+ // In production, @dataplan/pg provides this method. In tests the raw pg
171
+ // Client lacks it, so we implement it via savepoints (which nest cleanly
172
+ // inside the test harness's outer transaction).
173
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
174
+ const client = pgClient;
175
+ if (typeof client.withTransaction !== 'function') {
176
+ client.withTransaction = async (cb) => {
177
+ const sp = `wt_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
178
+ await client.query(`SAVEPOINT ${sp}`);
179
+ try {
180
+ const result = await cb(client);
181
+ await client.query(`RELEASE SAVEPOINT ${sp}`);
182
+ return result;
183
+ }
184
+ catch (err) {
185
+ await client.query(`ROLLBACK TO SAVEPOINT ${sp}`);
186
+ throw err;
187
+ }
188
+ };
189
+ }
171
190
  return callback(pgClient);
172
191
  };
173
192
  // Check if we're in a transaction by looking at the test client's transaction state
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-test",
3
- "version": "4.12.1",
3
+ "version": "4.13.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PostGraphile Testing",
6
6
  "main": "index.js",
@@ -55,5 +55,5 @@
55
55
  "pgpm",
56
56
  "test"
57
57
  ],
58
- "gitHead": "0fcb26c8f3379de5c2bf486e7ad78bb61a76e497"
58
+ "gitHead": "90016935b53d6fb84e0b83879377f0c2eb9abce6"
59
59
  }