@stonecrop/graphql-middleware 0.19.0 → 0.21.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/dist/index.js +330 -383
- package/dist/index.js.map +1 -1
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +0 -6
- package/package.json +2 -2
- package/dist/src/link-display.d.ts +0 -19
- package/dist/src/link-display.d.ts.map +0 -1
- package/dist/src/link-display.js +0 -80
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgraphile.d.ts","sourceRoot":"","sources":["../../../src/plugin/postgraphile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,WAAW,EAAiD,MAAM,mBAAmB,CAAA;AAGjH,OAAO,KAAK,EAAE,QAAQ,EAAc,MAAM,cAAc,CAAA;AAExD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"postgraphile.d.ts","sourceRoot":"","sources":["../../../src/plugin/postgraphile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,WAAW,EAAiD,MAAM,mBAAmB,CAAA;AAGjH,OAAO,KAAK,EAAE,QAAQ,EAAc,MAAM,cAAc,CAAA;AAExD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAU5D;;;;;;GAMG;AACH,MAAM,WAAW,oBAAoB;IACpC;;;;;;;;;;;;;;;;;;OAkBG;IACH,QAAQ,EAAE,QAAQ,CAAA;IAClB,4EAA4E;IAC5E,OAAO,EAAE,MAAM,CAAA;IACf,iGAAiG;IACjG,MAAM,EAAE,MAAM,CAAA;IACd,yFAAyF;IACzF,IAAI,EAAE,WAAW,CAAA;IACjB,8GAA8G;IAC9G,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IAC1B;;;;;;OAMG;IACH,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,wFAAwF;IACxF,IAAI,EAAE,OAAO,EAAE,CAAA;IACf,kGAAkG;IAClG,YAAY,CAAC,EAAE,MAAM,CAAA;CACrB;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,oBAAoB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;AAE/E;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACtC;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAA;IAC9D;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,kBAAkB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAClC;AAmCD;;;;;;;;;GASG;AACH,eAAO,MAAM,qBAAqB,GAAI,UAAS,sBAA2B,KAAG,cAAc,CAAC,MAuqB3F,CAAA;AA0BD;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAQvD;AA6HD;;;;;GAKG"}
|
|
@@ -3,7 +3,6 @@ import { loadOneWithPgClient, sideEffectWithPgClient } from '@dataplan/pg';
|
|
|
3
3
|
import { constant, lambda, object } from 'postgraphile/grafast';
|
|
4
4
|
import { extendSchema } from 'postgraphile/utils';
|
|
5
5
|
import { columnBackedFields, flattenFields } from '../fields';
|
|
6
|
-
import { enrichLinkDisplayFields } from '../link-display';
|
|
7
6
|
import { resolveTableName } from '../tables';
|
|
8
7
|
import { getFetchHandler } from '../registry/fetchHandlers';
|
|
9
8
|
import { getMeta, getAllMeta, validateReferences } from '../registry/doctypes';
|
|
@@ -315,10 +314,6 @@ export const createStonecropPlugin = (options = {}) => {
|
|
|
315
314
|
truncatedLinks: truncatedLinks.length ? truncatedLinks : undefined,
|
|
316
315
|
};
|
|
317
316
|
}
|
|
318
|
-
// After the expansion loop, not before it: a link that expanded has replaced its
|
|
319
|
-
// scalar FK with an object, and enrichment skips those. Running first stamped a
|
|
320
|
-
// display value beside a relation that no longer had an id to display.
|
|
321
|
-
await enrichLinkDisplayFields(pgClient, meta, enrichedRows, options.tables, debugSql);
|
|
322
317
|
}
|
|
323
318
|
return results;
|
|
324
319
|
});
|
|
@@ -409,7 +404,6 @@ export const createStonecropPlugin = (options = {}) => {
|
|
|
409
404
|
// downstream can mistake it for a record.
|
|
410
405
|
const hasMore = effectiveLimit != null && rows.length > effectiveLimit;
|
|
411
406
|
const data = [...(hasMore ? rows.slice(0, effectiveLimit ?? rows.length) : rows)];
|
|
412
|
-
await enrichLinkDisplayFields(pgClient, meta, data, options.tables, debugSql);
|
|
413
407
|
// Total matching the filters, independent of LIMIT/OFFSET, and opt-in.
|
|
414
408
|
//
|
|
415
409
|
// Explicitly requested rather than inferred from whether `count` was selected:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/graphql-middleware",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "GraphQL middleware for PostGraphile and Stonecrop schema",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"graphql": "^16.14.0",
|
|
24
24
|
"pluralize": "^8.0.0",
|
|
25
25
|
"postgraphile": "^5.0.3",
|
|
26
|
-
"@stonecrop/schema": "0.
|
|
26
|
+
"@stonecrop/schema": "0.21.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@electric-sql/pglite": "^0.4.5",
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { DoctypeMeta } from '@stonecrop/schema';
|
|
2
|
-
import type { PgClient } from '@dataplan/pg';
|
|
3
|
-
type SqlQueryFn = (pgClient: PgClient, query: {
|
|
4
|
-
text: string;
|
|
5
|
-
values?: unknown[];
|
|
6
|
-
}) => Promise<{
|
|
7
|
-
rows: readonly Record<string, unknown>[];
|
|
8
|
-
}>;
|
|
9
|
-
/**
|
|
10
|
-
* Add `fieldname__display` alongside each inline foreign-key field when the target doctype
|
|
11
|
-
* declares a `displayField`. Scalar FK values only — expanded relations are skipped.
|
|
12
|
-
*
|
|
13
|
-
* Takes every row at once and issues one lookup per FK field, not per row. Callers holding a batch
|
|
14
|
-
* must pass the whole batch: called per row inside a loader's own loop it reinstates the N+1 the
|
|
15
|
-
* loader exists to prevent, and does so invisibly, since the result is identical either way.
|
|
16
|
-
*/
|
|
17
|
-
export declare function enrichLinkDisplayFields(pgClient: PgClient, meta: DoctypeMeta, rows: Record<string, unknown>[], tables: Record<string, string> | undefined, debugSql: SqlQueryFn): Promise<void>;
|
|
18
|
-
export {};
|
|
19
|
-
//# sourceMappingURL=link-display.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"link-display.d.ts","sourceRoot":"","sources":["../../src/link-display.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAK5C,KAAK,UAAU,GAAG,CACjB,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAA;CAAE,KACvC,OAAO,CAAC;IAAE,IAAI,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CAAE,CAAC,CAAA;AA6C1D;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAC5C,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,WAAW,EACjB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EAC/B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAC1C,QAAQ,EAAE,UAAU,GAClB,OAAO,CAAC,IAAI,CAAC,CAqCf"}
|
package/dist/src/link-display.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { camelToSnake, flattenFields, getDisplayField, getRecordIdField, linkDisplayFieldname } from '@stonecrop/schema';
|
|
2
|
-
import { getMeta } from './registry/doctypes';
|
|
3
|
-
import { resolveTableName } from './tables';
|
|
4
|
-
/** Normalize a scalar FK cell value to a non-empty string id, or skip objects/null. */
|
|
5
|
-
function fkIdKey(value) {
|
|
6
|
-
if (value == null || typeof value === 'object')
|
|
7
|
-
return undefined;
|
|
8
|
-
if (typeof value === 'string')
|
|
9
|
-
return value === '' ? undefined : value;
|
|
10
|
-
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {
|
|
11
|
-
return String(value);
|
|
12
|
-
}
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
function collectLinkDisplaySpecs(meta, tables) {
|
|
16
|
-
const specs = [];
|
|
17
|
-
for (const field of flattenFields(meta.fields)) {
|
|
18
|
-
if (field.kind !== 'field' || !field.doctype)
|
|
19
|
-
continue;
|
|
20
|
-
const targetMeta = getMeta(field.doctype);
|
|
21
|
-
if (!targetMeta)
|
|
22
|
-
continue;
|
|
23
|
-
// `getDisplayField` rather than a lookup written here: it is the same call `DoctypeMeta`
|
|
24
|
-
// makes at the load gate, so a nomination that parsed is one this can build a SELECT from.
|
|
25
|
-
// A hand-rolled scan here admitted `computed` fields, whose column does not exist.
|
|
26
|
-
const displayField = getDisplayField(targetMeta.fields, targetMeta.displayField);
|
|
27
|
-
if (!displayField)
|
|
28
|
-
continue;
|
|
29
|
-
specs.push({
|
|
30
|
-
fieldname: field.fieldname,
|
|
31
|
-
displayField: displayField.fieldname,
|
|
32
|
-
targetPkFieldname: getRecordIdField(targetMeta.fields),
|
|
33
|
-
targetTable: resolveTableName(targetMeta.name, tables),
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return specs;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Add `fieldname__display` alongside each inline foreign-key field when the target doctype
|
|
40
|
-
* declares a `displayField`. Scalar FK values only — expanded relations are skipped.
|
|
41
|
-
*
|
|
42
|
-
* Takes every row at once and issues one lookup per FK field, not per row. Callers holding a batch
|
|
43
|
-
* must pass the whole batch: called per row inside a loader's own loop it reinstates the N+1 the
|
|
44
|
-
* loader exists to prevent, and does so invisibly, since the result is identical either way.
|
|
45
|
-
*/
|
|
46
|
-
export async function enrichLinkDisplayFields(pgClient, meta, rows, tables, debugSql) {
|
|
47
|
-
const specs = collectLinkDisplaySpecs(meta, tables);
|
|
48
|
-
if (specs.length === 0 || rows.length === 0)
|
|
49
|
-
return;
|
|
50
|
-
await Promise.all(specs.map(async (spec) => {
|
|
51
|
-
const ids = new Set();
|
|
52
|
-
for (const row of rows) {
|
|
53
|
-
const id = fkIdKey(row[spec.fieldname]);
|
|
54
|
-
if (id !== undefined)
|
|
55
|
-
ids.add(id);
|
|
56
|
-
}
|
|
57
|
-
if (ids.size === 0)
|
|
58
|
-
return;
|
|
59
|
-
const pkColumn = camelToSnake(spec.targetPkFieldname);
|
|
60
|
-
const displayColumn = camelToSnake(spec.displayField);
|
|
61
|
-
const displayAlias = displayColumn !== spec.displayField ? `"${displayColumn}" AS "${spec.displayField}"` : `"${spec.displayField}"`;
|
|
62
|
-
const { rows: displayRows } = await debugSql(pgClient, {
|
|
63
|
-
text: `SELECT "${pkColumn}" AS "${spec.targetPkFieldname}", ${displayAlias} FROM ${spec.targetTable} WHERE "${pkColumn}"::text = ANY($1::text[])`,
|
|
64
|
-
values: [Array.from(ids)],
|
|
65
|
-
});
|
|
66
|
-
const displayById = new Map();
|
|
67
|
-
for (const displayRow of displayRows) {
|
|
68
|
-
displayById.set(String(displayRow[spec.targetPkFieldname]), displayRow[spec.displayField]);
|
|
69
|
-
}
|
|
70
|
-
for (const row of rows) {
|
|
71
|
-
const id = fkIdKey(row[spec.fieldname]);
|
|
72
|
-
if (id === undefined)
|
|
73
|
-
continue;
|
|
74
|
-
const displayValue = displayById.get(id);
|
|
75
|
-
if (displayValue == null || displayValue === '')
|
|
76
|
-
continue;
|
|
77
|
-
row[linkDisplayFieldname(spec.fieldname)] = displayValue;
|
|
78
|
-
}
|
|
79
|
-
}));
|
|
80
|
-
}
|