@constructive-io/graphql-server 5.22.0 → 5.23.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 +2 -0
- package/esm/middleware/mask-error.js +1 -1
- package/esm/plugins/error-events-plugin.js +76 -0
- package/middleware/graphile.js +2 -0
- package/middleware/mask-error.d.ts +14 -0
- package/middleware/mask-error.js +3 -2
- package/package.json +4 -4
- package/plugins/error-events-plugin.d.ts +19 -0
- package/plugins/error-events-plugin.js +81 -0
|
@@ -12,6 +12,7 @@ import { isGraphqlObservabilityEnabled } from '../diagnostics/observability';
|
|
|
12
12
|
import { HandlerCreationError } from '../errors/api-errors';
|
|
13
13
|
import { respondWithGraphQLError } from '../errors/graphql-response';
|
|
14
14
|
import { AuthCookiePlugin } from '../plugins/auth-cookie-plugin';
|
|
15
|
+
import { createErrorEventsPlugin } from '../plugins/error-events-plugin';
|
|
15
16
|
import { RequestProtectionPlugin } from '../plugins/request-protection-plugin';
|
|
16
17
|
import { maskError } from './mask-error';
|
|
17
18
|
import { observeGraphileBuild } from './observability/graphile-build-stats';
|
|
@@ -61,6 +62,7 @@ const buildPreset = (pool, schemas, anonRole, roleName, introspectionRole, datab
|
|
|
61
62
|
plugins: [
|
|
62
63
|
AuthCookiePlugin,
|
|
63
64
|
RequestProtectionPlugin,
|
|
65
|
+
createErrorEventsPlugin(pool),
|
|
64
66
|
// Only registered when the compute module is provisioned for this
|
|
65
67
|
// database — all schema/table names come from the constructive
|
|
66
68
|
// metaschema (express-context compute module loader); the plugin has
|
|
@@ -51,7 +51,7 @@ const BAD_USER_INPUT = 'BAD_USER_INPUT';
|
|
|
51
51
|
* underlying pg error at `originalError`). We parse `originalError` first so we
|
|
52
52
|
* can recover the structured code, then fall back to the GraphQL error itself.
|
|
53
53
|
*/
|
|
54
|
-
const normalizeError = (error) => {
|
|
54
|
+
export const normalizeError = (error) => {
|
|
55
55
|
const original = error.originalError;
|
|
56
56
|
const fromOriginal = original ? parse(original) : null;
|
|
57
57
|
const parsed = fromOriginal?.code ? fromOriginal : parse(error);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import '../middleware/types'; // for Request type
|
|
2
|
+
import { Logger } from '@pgpmjs/logger';
|
|
3
|
+
import { getOperationAST } from 'graphql';
|
|
4
|
+
import { escapeIdentifier } from 'pg';
|
|
5
|
+
import { withPgClient } from 'pg-query-context';
|
|
6
|
+
import { normalizeError } from '../middleware/mask-error';
|
|
7
|
+
const log = new Logger('error-events');
|
|
8
|
+
export const GRAPHQL_ERROR_EVENT = 'graphql.error';
|
|
9
|
+
const getExpressRequest = (requestContext) => requestContext?.expressv4?.req;
|
|
10
|
+
/**
|
|
11
|
+
* The first structured, public-classified registry code among the errors.
|
|
12
|
+
* Internal/unknown errors are bugs, not refusals: they are masked and logged
|
|
13
|
+
* by `maskError` and never recorded as tenant events.
|
|
14
|
+
*/
|
|
15
|
+
const refusalCode = (errors) => {
|
|
16
|
+
for (const error of errors ?? []) {
|
|
17
|
+
const { code, class: errorClass } = normalizeError(error);
|
|
18
|
+
if (code && errorClass === 'public')
|
|
19
|
+
return code;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
};
|
|
23
|
+
export const recordEventSql = (events) => `SELECT ${escapeIdentifier(events.privateSchemaName)}.${escapeIdentifier(events.recordEvent)}($1, $2::uuid, $3::jsonb)`;
|
|
24
|
+
/**
|
|
25
|
+
* Records `graphql.error` when an authenticated mutation is refused with a
|
|
26
|
+
* structured registry code. The refusal rolled back the mutation's own
|
|
27
|
+
* transaction, so the event is written afterwards in a fresh transaction under
|
|
28
|
+
* the same request claims, via the tenant's events module `record_event`.
|
|
29
|
+
*
|
|
30
|
+
* The server carries no policy about what a code means: the database reads
|
|
31
|
+
* `payload->>'code'` (e.g. PRINCIPAL_CHILD_WIDENS demoting a principal on the
|
|
32
|
+
* trust ladder). Endpoints without an events module record nothing.
|
|
33
|
+
* Unauthenticated requests are never recorded, so anonymous traffic cannot
|
|
34
|
+
* drive writes. The client response is never altered.
|
|
35
|
+
*/
|
|
36
|
+
export const createErrorEventsPlugin = (pool) => ({
|
|
37
|
+
name: 'ErrorEventsPlugin',
|
|
38
|
+
version: '0.0.0',
|
|
39
|
+
description: 'Records graphql.error through the tenant events module when an authenticated mutation is refused.',
|
|
40
|
+
grafast: {
|
|
41
|
+
middleware: {
|
|
42
|
+
async execute(next, event) {
|
|
43
|
+
const result = await next();
|
|
44
|
+
if (Symbol.asyncIterator in result)
|
|
45
|
+
return result;
|
|
46
|
+
const { args } = event;
|
|
47
|
+
const req = getExpressRequest(args.requestContext);
|
|
48
|
+
const actorId = req?.token?.principal_id ?? req?.token?.user_id;
|
|
49
|
+
if (!actorId)
|
|
50
|
+
return result;
|
|
51
|
+
if (getOperationAST(args.document, args.operationName)?.operation !== 'mutation')
|
|
52
|
+
return result;
|
|
53
|
+
const code = refusalCode(result.errors);
|
|
54
|
+
if (!code)
|
|
55
|
+
return result;
|
|
56
|
+
const pgSettings = args.contextValue?.pgSettings;
|
|
57
|
+
const label = req.requestId ? `[${req.requestId}]` : '[req]';
|
|
58
|
+
try {
|
|
59
|
+
const events = await req.constructive?.useModule('events');
|
|
60
|
+
if (!events || !pgSettings)
|
|
61
|
+
return result;
|
|
62
|
+
const operation = args.operationName ?? getOperationAST(args.document)?.name?.value ?? null;
|
|
63
|
+
await withPgClient(pool, pgSettings, (client) => client.query(recordEventSql(events), [
|
|
64
|
+
GRAPHQL_ERROR_EVENT,
|
|
65
|
+
actorId,
|
|
66
|
+
JSON.stringify({ code, operation })
|
|
67
|
+
]));
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
log.error(`${label} failed to record ${GRAPHQL_ERROR_EVENT} (${code}) for ${actorId}: ${err instanceof Error ? err.message : String(err)}`);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
package/middleware/graphile.js
CHANGED
|
@@ -18,6 +18,7 @@ const observability_1 = require("../diagnostics/observability");
|
|
|
18
18
|
const api_errors_1 = require("../errors/api-errors");
|
|
19
19
|
const graphql_response_1 = require("../errors/graphql-response");
|
|
20
20
|
const auth_cookie_plugin_1 = require("../plugins/auth-cookie-plugin");
|
|
21
|
+
const error_events_plugin_1 = require("../plugins/error-events-plugin");
|
|
21
22
|
const request_protection_plugin_1 = require("../plugins/request-protection-plugin");
|
|
22
23
|
const mask_error_1 = require("./mask-error");
|
|
23
24
|
const graphile_build_stats_1 = require("./observability/graphile-build-stats");
|
|
@@ -67,6 +68,7 @@ const buildPreset = (pool, schemas, anonRole, roleName, introspectionRole, datab
|
|
|
67
68
|
plugins: [
|
|
68
69
|
auth_cookie_plugin_1.AuthCookiePlugin,
|
|
69
70
|
request_protection_plugin_1.RequestProtectionPlugin,
|
|
71
|
+
(0, error_events_plugin_1.createErrorEventsPlugin)(pool),
|
|
70
72
|
// Only registered when the compute module is provisioned for this
|
|
71
73
|
// database — all schema/table names come from the constructive
|
|
72
74
|
// metaschema (express-context compute module loader); the plugin has
|
|
@@ -1,4 +1,18 @@
|
|
|
1
|
+
import { type ErrorContext } from '@constructive-io/errors';
|
|
1
2
|
import { type GraphQLError, type GraphQLFormattedError } from 'graphql';
|
|
3
|
+
/**
|
|
4
|
+
* Normalize any GraphQL/database error into a canonical Constructive shape.
|
|
5
|
+
*
|
|
6
|
+
* Database errors surface through Grafast without a populated `extensions.code`
|
|
7
|
+
* (the semantic code lives in the message, and any SQLSTATE/DETAIL lives on the
|
|
8
|
+
* underlying pg error at `originalError`). We parse `originalError` first so we
|
|
9
|
+
* can recover the structured code, then fall back to the GraphQL error itself.
|
|
10
|
+
*/
|
|
11
|
+
export declare const normalizeError: (error: GraphQLError) => {
|
|
12
|
+
code: string | null;
|
|
13
|
+
context: ErrorContext;
|
|
14
|
+
class: "public" | "internal";
|
|
15
|
+
};
|
|
2
16
|
/**
|
|
3
17
|
* Production-aware error handling backed by `@constructive-io/errors`.
|
|
4
18
|
*
|
package/middleware/mask-error.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.maskError = void 0;
|
|
6
|
+
exports.maskError = exports.normalizeError = void 0;
|
|
7
7
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
8
8
|
const errors_1 = require("@constructive-io/errors");
|
|
9
9
|
const env_1 = require("@pgpmjs/env");
|
|
@@ -63,6 +63,7 @@ const normalizeError = (error) => {
|
|
|
63
63
|
const parsed = fromOriginal?.code ? fromOriginal : (0, errors_1.parse)(error);
|
|
64
64
|
return { code: parsed.code, context: parsed.context, class: parsed.class };
|
|
65
65
|
};
|
|
66
|
+
exports.normalizeError = normalizeError;
|
|
66
67
|
/**
|
|
67
68
|
* Production-aware error handling backed by `@constructive-io/errors`.
|
|
68
69
|
*
|
|
@@ -75,7 +76,7 @@ const normalizeError = (error) => {
|
|
|
75
76
|
* the original.
|
|
76
77
|
*/
|
|
77
78
|
const maskError = (error) => {
|
|
78
|
-
const { code, context, class: errorClass } = normalizeError(error);
|
|
79
|
+
const { code, context, class: errorClass } = (0, exports.normalizeError)(error);
|
|
79
80
|
// Lift the structured code onto extensions for every recognized error so
|
|
80
81
|
// clients always receive a machine-readable code (`extensions` is read-only
|
|
81
82
|
// on GraphQLError, so we build a formatted error rather than mutating it).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/graphql-server",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.23.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Constructive GraphQL Server",
|
|
6
6
|
"main": "index.js",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@agentic-kit/ollama": "2.14.0",
|
|
45
45
|
"@constructive-io/csrf": "^0.29.1",
|
|
46
46
|
"@constructive-io/errors": "^0.13.0",
|
|
47
|
-
"@constructive-io/express-context": "^0.
|
|
47
|
+
"@constructive-io/express-context": "^0.29.0",
|
|
48
48
|
"@constructive-io/graphql-env": "^3.32.0",
|
|
49
49
|
"@constructive-io/graphql-types": "^3.31.0",
|
|
50
50
|
"@constructive-io/llm-env": "^0.14.1",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"graphile-cache": "^4.12.4",
|
|
68
68
|
"graphile-config": "1.1.0",
|
|
69
69
|
"graphile-function-bindings": "^1.14.5",
|
|
70
|
-
"graphile-settings": "^6.21.
|
|
70
|
+
"graphile-settings": "^6.21.6",
|
|
71
71
|
"graphile-utils": "5.0.3",
|
|
72
72
|
"graphql": "16.13.0",
|
|
73
73
|
"graphql-upload": "^13.0.0",
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
"supertest": "^7.2.2",
|
|
97
97
|
"ts-node": "^10.9.2"
|
|
98
98
|
},
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "86896399e80595be179738d459106cfbfbada659"
|
|
100
100
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import '../middleware/types';
|
|
2
|
+
import type { EventsConfig } from '@constructive-io/express-context';
|
|
3
|
+
import type { GraphileConfig } from 'graphile-config';
|
|
4
|
+
import { type Pool } from 'pg';
|
|
5
|
+
export declare const GRAPHQL_ERROR_EVENT = "graphql.error";
|
|
6
|
+
export declare const recordEventSql: (events: EventsConfig) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Records `graphql.error` when an authenticated mutation is refused with a
|
|
9
|
+
* structured registry code. The refusal rolled back the mutation's own
|
|
10
|
+
* transaction, so the event is written afterwards in a fresh transaction under
|
|
11
|
+
* the same request claims, via the tenant's events module `record_event`.
|
|
12
|
+
*
|
|
13
|
+
* The server carries no policy about what a code means: the database reads
|
|
14
|
+
* `payload->>'code'` (e.g. PRINCIPAL_CHILD_WIDENS demoting a principal on the
|
|
15
|
+
* trust ladder). Endpoints without an events module record nothing.
|
|
16
|
+
* Unauthenticated requests are never recorded, so anonymous traffic cannot
|
|
17
|
+
* drive writes. The client response is never altered.
|
|
18
|
+
*/
|
|
19
|
+
export declare const createErrorEventsPlugin: (pool: Pool) => GraphileConfig.Plugin;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createErrorEventsPlugin = exports.recordEventSql = exports.GRAPHQL_ERROR_EVENT = void 0;
|
|
4
|
+
require("../middleware/types"); // for Request type
|
|
5
|
+
const logger_1 = require("@pgpmjs/logger");
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
7
|
+
const pg_1 = require("pg");
|
|
8
|
+
const pg_query_context_1 = require("pg-query-context");
|
|
9
|
+
const mask_error_1 = require("../middleware/mask-error");
|
|
10
|
+
const log = new logger_1.Logger('error-events');
|
|
11
|
+
exports.GRAPHQL_ERROR_EVENT = 'graphql.error';
|
|
12
|
+
const getExpressRequest = (requestContext) => requestContext?.expressv4?.req;
|
|
13
|
+
/**
|
|
14
|
+
* The first structured, public-classified registry code among the errors.
|
|
15
|
+
* Internal/unknown errors are bugs, not refusals: they are masked and logged
|
|
16
|
+
* by `maskError` and never recorded as tenant events.
|
|
17
|
+
*/
|
|
18
|
+
const refusalCode = (errors) => {
|
|
19
|
+
for (const error of errors ?? []) {
|
|
20
|
+
const { code, class: errorClass } = (0, mask_error_1.normalizeError)(error);
|
|
21
|
+
if (code && errorClass === 'public')
|
|
22
|
+
return code;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
};
|
|
26
|
+
const recordEventSql = (events) => `SELECT ${(0, pg_1.escapeIdentifier)(events.privateSchemaName)}.${(0, pg_1.escapeIdentifier)(events.recordEvent)}($1, $2::uuid, $3::jsonb)`;
|
|
27
|
+
exports.recordEventSql = recordEventSql;
|
|
28
|
+
/**
|
|
29
|
+
* Records `graphql.error` when an authenticated mutation is refused with a
|
|
30
|
+
* structured registry code. The refusal rolled back the mutation's own
|
|
31
|
+
* transaction, so the event is written afterwards in a fresh transaction under
|
|
32
|
+
* the same request claims, via the tenant's events module `record_event`.
|
|
33
|
+
*
|
|
34
|
+
* The server carries no policy about what a code means: the database reads
|
|
35
|
+
* `payload->>'code'` (e.g. PRINCIPAL_CHILD_WIDENS demoting a principal on the
|
|
36
|
+
* trust ladder). Endpoints without an events module record nothing.
|
|
37
|
+
* Unauthenticated requests are never recorded, so anonymous traffic cannot
|
|
38
|
+
* drive writes. The client response is never altered.
|
|
39
|
+
*/
|
|
40
|
+
const createErrorEventsPlugin = (pool) => ({
|
|
41
|
+
name: 'ErrorEventsPlugin',
|
|
42
|
+
version: '0.0.0',
|
|
43
|
+
description: 'Records graphql.error through the tenant events module when an authenticated mutation is refused.',
|
|
44
|
+
grafast: {
|
|
45
|
+
middleware: {
|
|
46
|
+
async execute(next, event) {
|
|
47
|
+
const result = await next();
|
|
48
|
+
if (Symbol.asyncIterator in result)
|
|
49
|
+
return result;
|
|
50
|
+
const { args } = event;
|
|
51
|
+
const req = getExpressRequest(args.requestContext);
|
|
52
|
+
const actorId = req?.token?.principal_id ?? req?.token?.user_id;
|
|
53
|
+
if (!actorId)
|
|
54
|
+
return result;
|
|
55
|
+
if ((0, graphql_1.getOperationAST)(args.document, args.operationName)?.operation !== 'mutation')
|
|
56
|
+
return result;
|
|
57
|
+
const code = refusalCode(result.errors);
|
|
58
|
+
if (!code)
|
|
59
|
+
return result;
|
|
60
|
+
const pgSettings = args.contextValue?.pgSettings;
|
|
61
|
+
const label = req.requestId ? `[${req.requestId}]` : '[req]';
|
|
62
|
+
try {
|
|
63
|
+
const events = await req.constructive?.useModule('events');
|
|
64
|
+
if (!events || !pgSettings)
|
|
65
|
+
return result;
|
|
66
|
+
const operation = args.operationName ?? (0, graphql_1.getOperationAST)(args.document)?.name?.value ?? null;
|
|
67
|
+
await (0, pg_query_context_1.withPgClient)(pool, pgSettings, (client) => client.query((0, exports.recordEventSql)(events), [
|
|
68
|
+
exports.GRAPHQL_ERROR_EVENT,
|
|
69
|
+
actorId,
|
|
70
|
+
JSON.stringify({ code, operation })
|
|
71
|
+
]));
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
log.error(`${label} failed to record ${exports.GRAPHQL_ERROR_EVENT} (${code}) for ${actorId}: ${err instanceof Error ? err.message : String(err)}`);
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
exports.createErrorEventsPlugin = createErrorEventsPlugin;
|