@constructive-io/graphql-server 4.11.6 → 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.
@@ -3,7 +3,7 @@ import { getNodeEnv } from '@pgpmjs/env';
3
3
  import { Logger } from '@pgpmjs/logger';
4
4
  import { createGraphileInstance, graphileCache } from 'graphile-cache';
5
5
  import { ConstructivePreset, makePgService } from 'graphile-settings';
6
- import { buildConnectionString } from 'pg-cache';
6
+ import { getPgPool } from 'pg-cache';
7
7
  import { getPgEnvOptions } from 'pg-env';
8
8
  import './types'; // for Request type
9
9
  import { isGraphqlObservabilityEnabled } from '../diagnostics/observability';
@@ -107,62 +107,64 @@ export function clearInFlightMap() {
107
107
  const log = new Logger('graphile');
108
108
  const reqLabel = (req) => (req.requestId ? `[${req.requestId}]` : '[req]');
109
109
  /**
110
- * Build a PostGraphile v5 preset for a tenant
110
+ * Build a PostGraphile v5 preset for a tenant.
111
111
  */
112
- const buildPreset = (connectionString, schemas, anonRole, roleName) => ({
113
- extends: [ConstructivePreset],
114
- pgServices: [
115
- makePgService({
116
- connectionString,
117
- schemas,
118
- }),
119
- ],
120
- grafserv: {
121
- graphqlPath: '/graphql',
122
- graphiqlPath: '/graphiql',
123
- graphiql: true,
124
- graphiqlOnGraphQLGET: false,
125
- maskError,
126
- },
127
- grafast: {
128
- explain: process.env.NODE_ENV === 'development',
129
- context: (requestContext) => {
130
- // In grafserv/express/v4, the request is available at requestContext.expressv4.req
131
- const req = requestContext?.expressv4?.req;
132
- const context = {};
133
- if (req) {
134
- if (req.databaseId) {
135
- context['jwt.claims.database_id'] = req.databaseId;
136
- }
137
- if (req.clientIp) {
138
- context['jwt.claims.ip_address'] = req.clientIp;
139
- }
140
- if (req.get('origin')) {
141
- context['jwt.claims.origin'] = req.get('origin');
142
- }
143
- if (req.get('User-Agent')) {
144
- context['jwt.claims.user_agent'] = req.get('User-Agent');
145
- }
146
- if (req.token?.user_id) {
147
- return {
148
- pgSettings: {
149
- role: roleName,
150
- 'jwt.claims.token_id': req.token.id,
151
- 'jwt.claims.user_id': req.token.user_id,
152
- ...context,
153
- },
154
- };
112
+ const buildPreset = (pool, schemas, anonRole, roleName) => {
113
+ return {
114
+ extends: [ConstructivePreset],
115
+ pgServices: [
116
+ makePgService({
117
+ pool,
118
+ schemas,
119
+ }),
120
+ ],
121
+ grafserv: {
122
+ graphqlPath: '/graphql',
123
+ graphiqlPath: '/graphiql',
124
+ graphiql: true,
125
+ graphiqlOnGraphQLGET: false,
126
+ maskError,
127
+ },
128
+ grafast: {
129
+ explain: process.env.NODE_ENV === 'development',
130
+ context: (requestContext) => {
131
+ // In grafserv/express/v4, the request is available at requestContext.expressv4.req
132
+ const req = requestContext?.expressv4?.req;
133
+ const context = {};
134
+ if (req) {
135
+ if (req.databaseId) {
136
+ context['jwt.claims.database_id'] = req.databaseId;
137
+ }
138
+ if (req.clientIp) {
139
+ context['jwt.claims.ip_address'] = req.clientIp;
140
+ }
141
+ if (req.get('origin')) {
142
+ context['jwt.claims.origin'] = req.get('origin');
143
+ }
144
+ if (req.get('User-Agent')) {
145
+ context['jwt.claims.user_agent'] = req.get('User-Agent');
146
+ }
147
+ if (req.token?.user_id) {
148
+ return {
149
+ pgSettings: {
150
+ role: roleName,
151
+ 'jwt.claims.token_id': req.token.id,
152
+ 'jwt.claims.user_id': req.token.user_id,
153
+ ...context,
154
+ },
155
+ };
156
+ }
155
157
  }
156
- }
157
- return {
158
- pgSettings: {
159
- role: anonRole,
160
- ...context,
161
- },
162
- };
158
+ return {
159
+ pgSettings: {
160
+ role: anonRole,
161
+ ...context,
162
+ },
163
+ };
164
+ },
163
165
  },
164
- },
165
- });
166
+ };
167
+ };
166
168
  export const graphile = (opts) => {
167
169
  const observabilityEnabled = isGraphqlObservabilityEnabled(opts.server?.host);
168
170
  return async (req, res, next) => {
@@ -225,9 +227,11 @@ export const graphile = (opts) => {
225
227
  ...opts.pg,
226
228
  database: dbname,
227
229
  });
228
- const connectionString = buildConnectionString(pgConfig.user, pgConfig.password, pgConfig.host, pgConfig.port, pgConfig.database);
230
+ // Route through pg-cache so the pool is tracked and can be cleaned up
231
+ // properly, preventing leaked connections during database teardown.
232
+ const pool = getPgPool(pgConfig);
229
233
  // Create promise and store in in-flight map BEFORE try block
230
- const preset = buildPreset(connectionString, schema || [], anonRole, roleName);
234
+ const preset = buildPreset(pool, schema || [], anonRole, roleName);
231
235
  const creationPromise = observeGraphileBuild({
232
236
  cacheKey: key,
233
237
  serviceKey: key,
@@ -116,62 +116,64 @@ function clearInFlightMap() {
116
116
  const log = new logger_1.Logger('graphile');
117
117
  const reqLabel = (req) => (req.requestId ? `[${req.requestId}]` : '[req]');
118
118
  /**
119
- * Build a PostGraphile v5 preset for a tenant
119
+ * Build a PostGraphile v5 preset for a tenant.
120
120
  */
121
- const buildPreset = (connectionString, schemas, anonRole, roleName) => ({
122
- extends: [graphile_settings_1.ConstructivePreset],
123
- pgServices: [
124
- (0, graphile_settings_1.makePgService)({
125
- connectionString,
126
- schemas,
127
- }),
128
- ],
129
- grafserv: {
130
- graphqlPath: '/graphql',
131
- graphiqlPath: '/graphiql',
132
- graphiql: true,
133
- graphiqlOnGraphQLGET: false,
134
- maskError,
135
- },
136
- grafast: {
137
- explain: process.env.NODE_ENV === 'development',
138
- context: (requestContext) => {
139
- // In grafserv/express/v4, the request is available at requestContext.expressv4.req
140
- const req = requestContext?.expressv4?.req;
141
- const context = {};
142
- if (req) {
143
- if (req.databaseId) {
144
- context['jwt.claims.database_id'] = req.databaseId;
145
- }
146
- if (req.clientIp) {
147
- context['jwt.claims.ip_address'] = req.clientIp;
148
- }
149
- if (req.get('origin')) {
150
- context['jwt.claims.origin'] = req.get('origin');
151
- }
152
- if (req.get('User-Agent')) {
153
- context['jwt.claims.user_agent'] = req.get('User-Agent');
154
- }
155
- if (req.token?.user_id) {
156
- return {
157
- pgSettings: {
158
- role: roleName,
159
- 'jwt.claims.token_id': req.token.id,
160
- 'jwt.claims.user_id': req.token.user_id,
161
- ...context,
162
- },
163
- };
121
+ const buildPreset = (pool, schemas, anonRole, roleName) => {
122
+ return {
123
+ extends: [graphile_settings_1.ConstructivePreset],
124
+ pgServices: [
125
+ (0, graphile_settings_1.makePgService)({
126
+ pool,
127
+ schemas,
128
+ }),
129
+ ],
130
+ grafserv: {
131
+ graphqlPath: '/graphql',
132
+ graphiqlPath: '/graphiql',
133
+ graphiql: true,
134
+ graphiqlOnGraphQLGET: false,
135
+ maskError,
136
+ },
137
+ grafast: {
138
+ explain: process.env.NODE_ENV === 'development',
139
+ context: (requestContext) => {
140
+ // In grafserv/express/v4, the request is available at requestContext.expressv4.req
141
+ const req = requestContext?.expressv4?.req;
142
+ const context = {};
143
+ if (req) {
144
+ if (req.databaseId) {
145
+ context['jwt.claims.database_id'] = req.databaseId;
146
+ }
147
+ if (req.clientIp) {
148
+ context['jwt.claims.ip_address'] = req.clientIp;
149
+ }
150
+ if (req.get('origin')) {
151
+ context['jwt.claims.origin'] = req.get('origin');
152
+ }
153
+ if (req.get('User-Agent')) {
154
+ context['jwt.claims.user_agent'] = req.get('User-Agent');
155
+ }
156
+ if (req.token?.user_id) {
157
+ return {
158
+ pgSettings: {
159
+ role: roleName,
160
+ 'jwt.claims.token_id': req.token.id,
161
+ 'jwt.claims.user_id': req.token.user_id,
162
+ ...context,
163
+ },
164
+ };
165
+ }
164
166
  }
165
- }
166
- return {
167
- pgSettings: {
168
- role: anonRole,
169
- ...context,
170
- },
171
- };
167
+ return {
168
+ pgSettings: {
169
+ role: anonRole,
170
+ ...context,
171
+ },
172
+ };
173
+ },
172
174
  },
173
- },
174
- });
175
+ };
176
+ };
175
177
  const graphile = (opts) => {
176
178
  const observabilityEnabled = (0, observability_1.isGraphqlObservabilityEnabled)(opts.server?.host);
177
179
  return async (req, res, next) => {
@@ -234,9 +236,11 @@ const graphile = (opts) => {
234
236
  ...opts.pg,
235
237
  database: dbname,
236
238
  });
237
- const connectionString = (0, pg_cache_1.buildConnectionString)(pgConfig.user, pgConfig.password, pgConfig.host, pgConfig.port, pgConfig.database);
239
+ // Route through pg-cache so the pool is tracked and can be cleaned up
240
+ // properly, preventing leaked connections during database teardown.
241
+ const pool = (0, pg_cache_1.getPgPool)(pgConfig);
238
242
  // Create promise and store in in-flight map BEFORE try block
239
- const preset = buildPreset(connectionString, schema || [], anonRole, roleName);
243
+ const preset = buildPreset(pool, schema || [], anonRole, roleName);
240
244
  const creationPromise = (0, graphile_build_stats_1.observeGraphileBuild)({
241
245
  cacheKey: key,
242
246
  serviceKey: key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructive-io/graphql-server",
3
- "version": "4.11.6",
3
+ "version": "4.13.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "Constructive GraphQL Server",
6
6
  "main": "index.js",
@@ -63,7 +63,7 @@
63
63
  "graphile-build-pg": "5.0.0-rc.8",
64
64
  "graphile-cache": "^3.3.4",
65
65
  "graphile-config": "1.0.0-rc.6",
66
- "graphile-settings": "^4.10.3",
66
+ "graphile-settings": "^4.12.0",
67
67
  "graphile-utils": "5.0.0-rc.8",
68
68
  "graphql": "16.13.0",
69
69
  "graphql-upload": "^13.0.0",
@@ -91,5 +91,5 @@
91
91
  "nodemon": "^3.1.14",
92
92
  "ts-node": "^10.9.2"
93
93
  },
94
- "gitHead": "3b3735292589a49601f40645ea7880f584a23b77"
94
+ "gitHead": "b8d4ae2b36e37e7f3533f858f19ec000febaa04b"
95
95
  }