@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.
- package/esm/middleware/graphile.js +60 -56
- package/middleware/graphile.js +59 -55
- package/package.json +3 -3
|
@@ -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 {
|
|
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 = (
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (req
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
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(
|
|
234
|
+
const preset = buildPreset(pool, schema || [], anonRole, roleName);
|
|
231
235
|
const creationPromise = observeGraphileBuild({
|
|
232
236
|
cacheKey: key,
|
|
233
237
|
serviceKey: key,
|
package/middleware/graphile.js
CHANGED
|
@@ -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 = (
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (req
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
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(
|
|
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.
|
|
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.
|
|
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": "
|
|
94
|
+
"gitHead": "b8d4ae2b36e37e7f3533f858f19ec000febaa04b"
|
|
95
95
|
}
|