@m1212e/rumble 0.23.0 → 0.23.4

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/out/index.cjs CHANGED
@@ -27,7 +27,6 @@ let graphql = require("graphql");
27
27
  let _escape_tech_graphql_armor = require("@escape.tech/graphql-armor");
28
28
  let _graphql_yoga_plugin_disable_introspection = require("@graphql-yoga/plugin-disable-introspection");
29
29
  let _opentelemetry_api = require("@opentelemetry/api");
30
- let _pothos_tracing_opentelemetry = require("@pothos/tracing-opentelemetry");
31
30
  let es_toolkit = require("es-toolkit");
32
31
  let graphql_yoga = require("graphql-yoga");
33
32
  let drizzle_orm = require("drizzle-orm");
@@ -36,6 +35,7 @@ let drizzle_orm_pg_core = require("drizzle-orm/pg-core");
36
35
  let drizzle_orm_mysql_core = require("drizzle-orm/mysql-core");
37
36
  let pluralize = require("pluralize");
38
37
  pluralize = __toESM(pluralize, 1);
38
+ let _pothos_tracing_opentelemetry = require("@pothos/tracing-opentelemetry");
39
39
  let drizzle_orm_sqlite_core = require("drizzle-orm/sqlite-core");
40
40
  let _pothos_core = require("@pothos/core");
41
41
  _pothos_core = __toESM(_pothos_core, 1);
@@ -208,7 +208,7 @@ function mapNullFieldsToUndefined(obj) {
208
208
  }
209
209
  //#endregion
210
210
  //#region package.json
211
- var version = "0.23.0";
211
+ var version = "0.23.4";
212
212
  //#endregion
213
213
  //#region lib/helpers/mergeFilters.ts
214
214
  function mergeFilters(filterA, filterB, mode = "AND") {
@@ -704,7 +704,8 @@ by: (explicitFilter) => {
704
704
  * Merges the current query filters with the provided filters for this call only
705
705
  */
706
706
  function merge(p) {
707
- return internalTransformer(mergeFilters(ret.query.many, p), p.limit);
707
+ const merged = mergeFilters(ret.query.many, p);
708
+ return internalTransformer(merged, p.limit);
708
709
  }
709
710
  ret.merge = merge;
710
711
  return ret;
@@ -754,13 +755,14 @@ by: (explicitFilter) => {
754
755
  "rumble.table": tableName.toString(),
755
756
  "rumble.action": action
756
757
  };
757
- const result = assembleAbilities(new Proxy(span, { get(target, prop) {
758
+ const proxy = new Proxy(span, { get(target, prop) {
758
759
  if (prop === "setAttribute") return (k, v) => {
759
760
  attrs[k] = v;
760
761
  return target.setAttribute(k, v);
761
762
  };
762
763
  return target[prop];
763
- } }));
764
+ } });
765
+ const result = assembleAbilities(proxy);
764
766
  log?.debug(attrs, "abilities prepared");
765
767
  return result;
766
768
  } finally {
@@ -1096,10 +1098,11 @@ const createWhereArgImplementer = ({ db, schemaBuilder, enumImplementer }) => {
1096
1098
  }, {});
1097
1099
  const relations = Object.entries(tableSchema.relations ?? {}).reduce((acc, [key, value]) => {
1098
1100
  const targetTsName = value.targetTableName;
1099
- const referenceModel = whereArgImplementer({ dbName: tableHelper({
1101
+ const relationSchema = tableHelper({
1100
1102
  db,
1101
1103
  table: targetTsName
1102
- }).dbName });
1104
+ });
1105
+ const referenceModel = whereArgImplementer({ dbName: relationSchema.dbName });
1103
1106
  acc[key] = t.field({
1104
1107
  type: referenceModel,
1105
1108
  required: false
@@ -1297,6 +1300,165 @@ const sofaOpenAPIWebhookDocs = {
1297
1300
  } }
1298
1301
  };
1299
1302
  //#endregion
1303
+ //#region lib/helpers/tracing.ts
1304
+ function isAsyncIterable(value) {
1305
+ return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
1306
+ }
1307
+ async function* wrapSubscriptionIterator(iterator, log, operationName) {
1308
+ let eventCount = 0;
1309
+ try {
1310
+ for await (const event of iterator) {
1311
+ eventCount++;
1312
+ if (event.errors?.length) log.error({
1313
+ "graphql.operation.name": operationName,
1314
+ eventCount,
1315
+ errors: event.errors.map((e) => e.message)
1316
+ }, "graphql subscription event error");
1317
+ yield event;
1318
+ }
1319
+ log.info({
1320
+ "graphql.operation.name": operationName,
1321
+ eventCount
1322
+ }, "graphql subscription completed");
1323
+ } catch (error) {
1324
+ log.error({
1325
+ "graphql.operation.name": operationName,
1326
+ eventCount,
1327
+ err: error
1328
+ }, "graphql subscription threw");
1329
+ throw error;
1330
+ }
1331
+ }
1332
+ function buildTracedExecute(executeFn, rumbleInput) {
1333
+ return async (options) => {
1334
+ let log = rumbleInput.logger?.enabled ? rumbleInput.logger.logger : void 0;
1335
+ const operationName = options.operationName ?? "anonymous";
1336
+ const start = Date.now();
1337
+ log?.info({ "graphql.operation.name": operationName }, "graphql execute start");
1338
+ const run = async () => {
1339
+ const result = await executeFn(options);
1340
+ if (result && "errors" in result && result.errors?.length) log?.error({
1341
+ "graphql.operation.name": operationName,
1342
+ durationMs: Date.now() - start,
1343
+ errors: result.errors.map((e) => e.message)
1344
+ }, "graphql execute completed with errors");
1345
+ else log?.info({
1346
+ "graphql.operation.name": operationName,
1347
+ durationMs: Date.now() - start
1348
+ }, "graphql execute completed");
1349
+ return result;
1350
+ };
1351
+ if (rumbleInput.otel?.enabled) return rumbleInput.otel.tracer.startActiveSpan(_pothos_tracing_opentelemetry.SpanNames.EXECUTE, { attributes: {
1352
+ [_pothos_tracing_opentelemetry.AttributeNames.OPERATION_NAME]: operationName,
1353
+ [_pothos_tracing_opentelemetry.AttributeNames.SOURCE]: options.document
1354
+ } }, async (span) => {
1355
+ if (log && rumbleInput.logger?.injectTraceId !== false) {
1356
+ const { traceId, spanId } = span.spanContext();
1357
+ log = log.child({
1358
+ traceId,
1359
+ spanId
1360
+ });
1361
+ }
1362
+ try {
1363
+ const result = await run();
1364
+ if (result && "errors" in result && result.errors?.length) {
1365
+ for (const error of result.errors) span.recordException(error);
1366
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1367
+ }
1368
+ return result;
1369
+ } catch (error) {
1370
+ if (error instanceof Error) span.recordException(error);
1371
+ log?.error({
1372
+ "graphql.operation.name": operationName,
1373
+ durationMs: Date.now() - start,
1374
+ err: error
1375
+ }, "graphql execute threw");
1376
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1377
+ throw error;
1378
+ } finally {
1379
+ span.end();
1380
+ }
1381
+ });
1382
+ try {
1383
+ return await run();
1384
+ } catch (error) {
1385
+ log?.error({
1386
+ "graphql.operation.name": operationName,
1387
+ durationMs: Date.now() - start,
1388
+ err: error
1389
+ }, "graphql execute threw");
1390
+ throw error;
1391
+ }
1392
+ };
1393
+ }
1394
+ function buildTracedSubscribe(subscribeFn, rumbleInput) {
1395
+ return async (options) => {
1396
+ let log = rumbleInput.logger?.enabled ? rumbleInput.logger.logger : void 0;
1397
+ const operationName = options.operationName ?? "anonymous";
1398
+ const start = Date.now();
1399
+ const doSubscribe = async () => {
1400
+ log?.info({ "graphql.operation.name": operationName }, "graphql subscribe start");
1401
+ const result = await subscribeFn(options);
1402
+ if (!isAsyncIterable(result)) {
1403
+ const execResult = result;
1404
+ if (execResult.errors?.length) log?.error({
1405
+ "graphql.operation.name": operationName,
1406
+ durationMs: Date.now() - start,
1407
+ errors: execResult.errors.map((e) => e.message)
1408
+ }, "graphql subscribe completed with errors");
1409
+ return execResult;
1410
+ }
1411
+ log?.info({
1412
+ "graphql.operation.name": operationName,
1413
+ durationMs: Date.now() - start
1414
+ }, "graphql subscription established");
1415
+ if (log) return wrapSubscriptionIterator(result, log, operationName);
1416
+ return result;
1417
+ };
1418
+ if (rumbleInput.otel?.enabled) return rumbleInput.otel.tracer.startActiveSpan("graphql.subscribe", async (span) => {
1419
+ if (log && rumbleInput.logger?.injectTraceId !== false) {
1420
+ const { traceId, spanId } = span.spanContext();
1421
+ log = log.child({
1422
+ traceId,
1423
+ spanId
1424
+ });
1425
+ }
1426
+ try {
1427
+ const result = await doSubscribe();
1428
+ if (!isAsyncIterable(result)) {
1429
+ const execResult = result;
1430
+ if (execResult.errors?.length) {
1431
+ for (const error of execResult.errors) span.recordException(error);
1432
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1433
+ }
1434
+ }
1435
+ return result;
1436
+ } catch (error) {
1437
+ if (error instanceof Error) span.recordException(error);
1438
+ log?.error({
1439
+ "graphql.operation.name": operationName,
1440
+ durationMs: Date.now() - start,
1441
+ err: error
1442
+ }, "graphql subscribe threw");
1443
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1444
+ throw error;
1445
+ } finally {
1446
+ span.end();
1447
+ }
1448
+ });
1449
+ try {
1450
+ return await doSubscribe();
1451
+ } catch (error) {
1452
+ log?.error({
1453
+ "graphql.operation.name": operationName,
1454
+ durationMs: Date.now() - start,
1455
+ err: error
1456
+ }, "graphql subscribe threw");
1457
+ throw error;
1458
+ }
1459
+ };
1460
+ }
1461
+ //#endregion
1300
1462
  //#region lib/helpers/sqlTypes/mapDrizzleTypeToGraphQlType.ts
1301
1463
  function buildPothosResponseTypeFromGraphQLType({ builder, sqlType, fieldName, nullable, isArray }) {
1302
1464
  const gqlType = mapSQLTypeToGraphQLType({
@@ -2334,59 +2496,7 @@ export const r = rumble({
2334
2496
  ...args?.plugins ?? [],
2335
2497
  ...enableApiDocs ? [] : [(0, _graphql_yoga_plugin_disable_introspection.useDisableIntrospection)(), (0, _escape_tech_graphql_armor.EnvelopArmorPlugin)(args?.armorConfig)],
2336
2498
  rumbleInput.otel?.enabled || rumbleInput.logger?.enabled ? { onExecute: ({ setExecuteFn, executeFn }) => {
2337
- setExecuteFn(async (options) => {
2338
- const log = rumbleInput.logger?.enabled ? rumbleInput.logger.logger : void 0;
2339
- const operationName = options.operationName ?? "anonymous";
2340
- const start = Date.now();
2341
- log?.info({ "graphql.operation.name": operationName }, "graphql execute start");
2342
- const run = async () => {
2343
- const result = await executeFn(options);
2344
- if (result && "errors" in result && result.errors?.length) log?.error({
2345
- "graphql.operation.name": operationName,
2346
- durationMs: Date.now() - start,
2347
- errors: result.errors.map((e) => e.message)
2348
- }, "graphql execute completed with errors");
2349
- else log?.info({
2350
- "graphql.operation.name": operationName,
2351
- durationMs: Date.now() - start
2352
- }, "graphql execute completed");
2353
- return result;
2354
- };
2355
- if (rumbleInput.otel?.enabled) return rumbleInput.otel.tracer.startActiveSpan(_pothos_tracing_opentelemetry.SpanNames.EXECUTE, { attributes: {
2356
- [_pothos_tracing_opentelemetry.AttributeNames.OPERATION_NAME]: operationName,
2357
- [_pothos_tracing_opentelemetry.AttributeNames.SOURCE]: options.document
2358
- } }, async (span) => {
2359
- try {
2360
- const result = await run();
2361
- if (result && "errors" in result && result.errors?.length) {
2362
- for (const error of result.errors) span.recordException(error);
2363
- span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
2364
- }
2365
- return result;
2366
- } catch (error) {
2367
- if (error instanceof Error) span.recordException(error);
2368
- log?.error({
2369
- "graphql.operation.name": operationName,
2370
- durationMs: Date.now() - start,
2371
- err: error
2372
- }, "graphql execute threw");
2373
- span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
2374
- throw error;
2375
- } finally {
2376
- span.end();
2377
- }
2378
- });
2379
- try {
2380
- return await run();
2381
- } catch (error) {
2382
- log?.error({
2383
- "graphql.operation.name": operationName,
2384
- durationMs: Date.now() - start,
2385
- err: error
2386
- }, "graphql execute threw");
2387
- throw error;
2388
- }
2389
- });
2499
+ setExecuteFn(buildTracedExecute(executeFn, rumbleInput));
2390
2500
  } } : false
2391
2501
  ].filter(Boolean)
2392
2502
  });
@@ -2407,6 +2517,8 @@ export const r = rumble({
2407
2517
  ...args,
2408
2518
  schema: builtSchema(),
2409
2519
  context,
2520
+ execute: buildTracedExecute(args.execute ?? graphql.execute, rumbleInput),
2521
+ subscribe: buildTracedSubscribe(args.subscribe ?? graphql.subscribe, rumbleInput),
2410
2522
  errorHandler(errors) {
2411
2523
  const span = _opentelemetry_api.trace.getActiveSpan();
2412
2524
  for (const error of errors) span?.recordException(error);
@@ -2419,7 +2531,9 @@ export const r = rumble({
2419
2531
  return implementation({
2420
2532
  ...args,
2421
2533
  schema: builtSchema(),
2422
- context
2534
+ context,
2535
+ execute: buildTracedExecute(args.execute ?? graphql.execute, rumbleInput),
2536
+ subscribe: buildTracedSubscribe(args.subscribe ?? graphql.subscribe, rumbleInput)
2423
2537
  }, ...rest);
2424
2538
  };
2425
2539
  return {