graphile-test 5.14.4 → 5.14.6

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.
Files changed (3) hide show
  1. package/context.js +10 -10
  2. package/esm/context.js +10 -10
  3. package/package.json +6 -6
package/context.js CHANGED
@@ -165,38 +165,38 @@ const runGraphQLInContext = async ({ input, conn, schema, resolvedPreset, pgServ
165
165
  // Include additional request options (excluding pgSettings to avoid duplication)
166
166
  ...Object.fromEntries(Object.entries(reqOptions).filter(([key]) => key !== 'pgSettings')),
167
167
  };
168
+ // Check if we're in a transaction by looking at the test client's transaction state
169
+ // When useRoot is true, we might not be in a transaction
170
+ // pgsql-test's `db` client is in a transaction, but `pg` (root) client may not be
171
+ const isInTransaction = !input.useRoot;
168
172
  // Provide a custom withPgClient function that uses the test client
169
173
  // This ensures GraphQL operations run within the test transaction
170
174
  // instead of getting a new connection from the pool
171
175
  const withPgClientKey = pgService.withPgClientKey ?? 'withPgClient';
172
176
  contextValue[withPgClientKey] = async (_pgSettings, callback) => {
173
177
  // 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).
178
+ // In production @dataplan/pg provides this method; a raw pg Client lacks
179
+ // it. Like @dataplan/pg's client, nest with a savepoint when a transaction
180
+ // is already open and open a real one otherwise.
177
181
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
178
182
  const client = pgClient;
179
183
  if (typeof client.withTransaction !== 'function') {
180
184
  client.withTransaction = async (cb) => {
181
185
  const sp = `wt_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
182
- await client.query(`SAVEPOINT ${sp}`);
186
+ await client.query(isInTransaction ? `SAVEPOINT ${sp}` : 'BEGIN');
183
187
  try {
184
188
  const result = await cb(client);
185
- await client.query(`RELEASE SAVEPOINT ${sp}`);
189
+ await client.query(isInTransaction ? `RELEASE SAVEPOINT ${sp}` : 'COMMIT');
186
190
  return result;
187
191
  }
188
192
  catch (err) {
189
- await client.query(`ROLLBACK TO SAVEPOINT ${sp}`);
193
+ await client.query(isInTransaction ? `ROLLBACK TO SAVEPOINT ${sp}` : 'ROLLBACK');
190
194
  throw err;
191
195
  }
192
196
  };
193
197
  }
194
198
  return callback(pgClient);
195
199
  };
196
- // Check if we're in a transaction by looking at the test client's transaction state
197
- // When useRoot is true, we might not be in a transaction
198
- // pgsql-test's `db` client is in a transaction, but `pg` (root) client may not be
199
- const isInTransaction = !input.useRoot;
200
200
  // Wrap the entire query execution in a savepoint if we're in a transaction
201
201
  // This matches v4 PostGraphile behavior where each mutation is wrapped in a savepoint
202
202
  // allowing the transaction to continue after a database error
package/esm/context.js CHANGED
@@ -161,38 +161,38 @@ export const runGraphQLInContext = async ({ input, conn, schema, resolvedPreset,
161
161
  // Include additional request options (excluding pgSettings to avoid duplication)
162
162
  ...Object.fromEntries(Object.entries(reqOptions).filter(([key]) => key !== 'pgSettings')),
163
163
  };
164
+ // Check if we're in a transaction by looking at the test client's transaction state
165
+ // When useRoot is true, we might not be in a transaction
166
+ // pgsql-test's `db` client is in a transaction, but `pg` (root) client may not be
167
+ const isInTransaction = !input.useRoot;
164
168
  // Provide a custom withPgClient function that uses the test client
165
169
  // This ensures GraphQL operations run within the test transaction
166
170
  // instead of getting a new connection from the pool
167
171
  const withPgClientKey = pgService.withPgClientKey ?? 'withPgClient';
168
172
  contextValue[withPgClientKey] = async (_pgSettings, callback) => {
169
173
  // 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).
174
+ // In production @dataplan/pg provides this method; a raw pg Client lacks
175
+ // it. Like @dataplan/pg's client, nest with a savepoint when a transaction
176
+ // is already open and open a real one otherwise.
173
177
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
174
178
  const client = pgClient;
175
179
  if (typeof client.withTransaction !== 'function') {
176
180
  client.withTransaction = async (cb) => {
177
181
  const sp = `wt_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
178
- await client.query(`SAVEPOINT ${sp}`);
182
+ await client.query(isInTransaction ? `SAVEPOINT ${sp}` : 'BEGIN');
179
183
  try {
180
184
  const result = await cb(client);
181
- await client.query(`RELEASE SAVEPOINT ${sp}`);
185
+ await client.query(isInTransaction ? `RELEASE SAVEPOINT ${sp}` : 'COMMIT');
182
186
  return result;
183
187
  }
184
188
  catch (err) {
185
- await client.query(`ROLLBACK TO SAVEPOINT ${sp}`);
189
+ await client.query(isInTransaction ? `ROLLBACK TO SAVEPOINT ${sp}` : 'ROLLBACK');
186
190
  throw err;
187
191
  }
188
192
  };
189
193
  }
190
194
  return callback(pgClient);
191
195
  };
192
- // Check if we're in a transaction by looking at the test client's transaction state
193
- // When useRoot is true, we might not be in a transaction
194
- // pgsql-test's `db` client is in a transaction, but `pg` (root) client may not be
195
- const isInTransaction = !input.useRoot;
196
196
  // Wrap the entire query execution in a savepoint if we're in a transaction
197
197
  // This matches v4 PostGraphile behavior where each mutation is wrapped in a savepoint
198
198
  // allowing the transaction to continue after a database error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-test",
3
- "version": "5.14.4",
3
+ "version": "5.14.6",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PostGraphile Testing",
6
6
  "main": "index.js",
@@ -34,9 +34,9 @@
34
34
  "makage": "^0.8.0"
35
35
  },
36
36
  "dependencies": {
37
- "@constructive-io/graphql-env": "^3.31.3",
38
- "@constructive-io/graphql-types": "^3.30.3",
39
- "@pgpmjs/types": "^2.53.1",
37
+ "@constructive-io/graphql-env": "^3.32.1",
38
+ "@constructive-io/graphql-types": "^3.31.1",
39
+ "@pgpmjs/types": "^2.53.3",
40
40
  "grafast": "1.1.2",
41
41
  "graphile-build": "5.1.1",
42
42
  "graphile-build-pg": "5.1.3",
@@ -44,7 +44,7 @@
44
44
  "graphql": "16.13.0",
45
45
  "mock-req": "^0.2.0",
46
46
  "pg": "^8.21.0",
47
- "pgsql-test": "^5.13.4",
47
+ "pgsql-test": "^5.13.6",
48
48
  "postgraphile": "5.1.4"
49
49
  },
50
50
  "keywords": [
@@ -55,5 +55,5 @@
55
55
  "pgpm",
56
56
  "test"
57
57
  ],
58
- "gitHead": "6e2d71a9eb4e1f49f0de0b2b594b2f811de97442"
58
+ "gitHead": "27be33dc1ba8351e2a2c2bbb99b9c0798c0813b9"
59
59
  }