@stonecrop/graphql-middleware 0.8.7 → 0.8.9
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/graphql-middleware.d.ts +1168 -0
- package/dist/graphql_client.tsbuildinfo +1 -0
- package/dist/index.cjs +41 -36
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +336 -3331
- package/dist/index.js.map +1 -0
- package/dist/src/client/index.d.ts +1 -1
- package/dist/src/client/index.js +191 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +22 -0
- package/dist/src/plugin/postgraphile.d.ts +32 -1
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +281 -0
- package/dist/src/registry/actions.d.ts +1 -1
- package/dist/src/registry/actions.js +148 -0
- package/dist/src/registry/doctypes.d.ts +1 -1
- package/dist/src/registry/doctypes.js +167 -0
- package/dist/src/tsdoc-metadata.json +11 -0
- package/dist/src/types/index.d.ts +1 -1
- package/dist/src/types/index.js +2 -0
- package/dist/tests/registry.test.js +4 -0
- package/package.json +5 -6
- package/dist/index.d.ts +0 -2
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { extendSchema, gql } from 'postgraphile/utils';
|
|
2
|
+
import { constant, lambda, object, loadOne } from 'postgraphile/grafast';
|
|
3
|
+
import { getHandler } from '../registry/actions';
|
|
4
|
+
import { getMeta, getAllMeta } from '../registry/doctypes';
|
|
5
|
+
/**
|
|
6
|
+
* Create a PostGraphile plugin that extends the GraphQL schema with Stonecrop functionality
|
|
7
|
+
* @param options - Plugin configuration options
|
|
8
|
+
* @returns A PostGraphile plugin
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export const createStonecropPlugin = (options) => {
|
|
12
|
+
// Resolve inflection callbacks with Amber defaults
|
|
13
|
+
const recordFieldName = options.inflection?.recordFieldName ?? defaultRecordFieldName;
|
|
14
|
+
const connectionFieldName = options.inflection?.connectionFieldName ?? defaultConnectionFieldName;
|
|
15
|
+
const orderByTypeName = options.inflection?.orderByTypeName ?? defaultOrderByTypeName;
|
|
16
|
+
return extendSchema(() => {
|
|
17
|
+
return {
|
|
18
|
+
typeDefs: gql `
|
|
19
|
+
type StonecropFieldMeta {
|
|
20
|
+
fieldname: String!
|
|
21
|
+
fieldtype: String!
|
|
22
|
+
label: String
|
|
23
|
+
required: Boolean
|
|
24
|
+
options: JSON
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type StonecropActionDefinition {
|
|
28
|
+
label: String!
|
|
29
|
+
handler: String!
|
|
30
|
+
requiredFields: [String!]
|
|
31
|
+
allowedStates: [String!]
|
|
32
|
+
confirm: Boolean
|
|
33
|
+
args: JSON
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type StonecropWorkflowMeta {
|
|
37
|
+
states: [String!]
|
|
38
|
+
actions: JSON
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type StonecropDoctypeMeta {
|
|
42
|
+
name: String!
|
|
43
|
+
tableName: String
|
|
44
|
+
fields: [StonecropFieldMeta!]!
|
|
45
|
+
workflow: StonecropWorkflowMeta
|
|
46
|
+
listDoctype: String
|
|
47
|
+
parentDoctype: String
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type StonecropRecordResult {
|
|
51
|
+
data: JSON
|
|
52
|
+
doctype: String!
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type StonecropRecordsResult {
|
|
56
|
+
data: [JSON!]!
|
|
57
|
+
doctype: String!
|
|
58
|
+
count: Int!
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type StonecropActionResult {
|
|
62
|
+
success: Boolean!
|
|
63
|
+
data: JSON
|
|
64
|
+
error: String
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
extend type Query {
|
|
68
|
+
stonecropMeta(doctype: String!): StonecropDoctypeMeta
|
|
69
|
+
stonecropAllMeta: [StonecropDoctypeMeta!]!
|
|
70
|
+
stonecropRecord(doctype: String!, id: String!): StonecropRecordResult
|
|
71
|
+
stonecropRecords(
|
|
72
|
+
doctype: String!
|
|
73
|
+
filters: JSON
|
|
74
|
+
orderBy: String
|
|
75
|
+
limit: Int
|
|
76
|
+
offset: Int
|
|
77
|
+
): StonecropRecordsResult
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
extend type Mutation {
|
|
81
|
+
stonecropAction(doctype: String!, action: String!, args: JSON): StonecropActionResult!
|
|
82
|
+
}
|
|
83
|
+
`,
|
|
84
|
+
objects: {
|
|
85
|
+
Query: {
|
|
86
|
+
plans: {
|
|
87
|
+
stonecropMeta(_, { $doctype }) {
|
|
88
|
+
return lambda($doctype, (doctype) => {
|
|
89
|
+
const meta = getMeta(doctype);
|
|
90
|
+
return meta ?? null;
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
stonecropAllMeta() {
|
|
94
|
+
return constant(getAllMeta());
|
|
95
|
+
},
|
|
96
|
+
stonecropRecord(_, { $doctype, $id }) {
|
|
97
|
+
return loadOne(object({ doctype: $doctype, id: $id }), async (specs) => {
|
|
98
|
+
return await Promise.all(specs.map(async (spec) => {
|
|
99
|
+
const meta = getMeta(spec.doctype);
|
|
100
|
+
if (!meta) {
|
|
101
|
+
throw new Error(`Unknown doctype: ${spec.doctype}`);
|
|
102
|
+
}
|
|
103
|
+
if (!meta.tableName) {
|
|
104
|
+
throw new Error(`Doctype ${spec.doctype} has no table mapping`);
|
|
105
|
+
}
|
|
106
|
+
const query = buildRecordQuery(meta, recordFieldName);
|
|
107
|
+
const result = await options.executor.query(query, { id: spec.id });
|
|
108
|
+
return {
|
|
109
|
+
data: extractSingleResult(result, meta, recordFieldName),
|
|
110
|
+
doctype: spec.doctype,
|
|
111
|
+
};
|
|
112
|
+
}));
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
stonecropRecords(_, { $doctype, $filters, $orderBy, $limit, $offset }) {
|
|
116
|
+
return loadOne(object({
|
|
117
|
+
doctype: $doctype,
|
|
118
|
+
filters: $filters,
|
|
119
|
+
orderBy: $orderBy,
|
|
120
|
+
limit: $limit,
|
|
121
|
+
offset: $offset,
|
|
122
|
+
}), async (specs) => {
|
|
123
|
+
return await Promise.all(specs.map(async (spec) => {
|
|
124
|
+
const meta = getMeta(spec.doctype);
|
|
125
|
+
if (!meta) {
|
|
126
|
+
throw new Error(`Unknown doctype: ${spec.doctype}`);
|
|
127
|
+
}
|
|
128
|
+
if (!meta.tableName) {
|
|
129
|
+
throw new Error(`Doctype ${spec.doctype} has no table mapping`);
|
|
130
|
+
}
|
|
131
|
+
const query = buildListQuery(meta, {
|
|
132
|
+
limit: spec.limit,
|
|
133
|
+
offset: spec.offset,
|
|
134
|
+
orderBy: spec.orderBy,
|
|
135
|
+
}, connectionFieldName, orderByTypeName);
|
|
136
|
+
const result = await options.executor.query(query, {
|
|
137
|
+
limit: spec.limit,
|
|
138
|
+
offset: spec.offset,
|
|
139
|
+
orderBy: spec.orderBy,
|
|
140
|
+
});
|
|
141
|
+
const data = extractListResult(result, meta, connectionFieldName);
|
|
142
|
+
return {
|
|
143
|
+
data,
|
|
144
|
+
doctype: spec.doctype,
|
|
145
|
+
count: data.length,
|
|
146
|
+
};
|
|
147
|
+
}));
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
Mutation: {
|
|
153
|
+
plans: {
|
|
154
|
+
stonecropAction(_, { $doctype, $action, $args: $actionArgs }) {
|
|
155
|
+
return loadOne(object({
|
|
156
|
+
doctype: $doctype,
|
|
157
|
+
action: $action,
|
|
158
|
+
actionArgs: $actionArgs,
|
|
159
|
+
}), async (specs) => {
|
|
160
|
+
return await Promise.all(specs.map(async (spec) => {
|
|
161
|
+
const meta = getMeta(spec.doctype);
|
|
162
|
+
if (!meta) {
|
|
163
|
+
return {
|
|
164
|
+
success: false,
|
|
165
|
+
data: null,
|
|
166
|
+
error: `Unknown doctype: ${spec.doctype}`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const actionDef = meta.workflow?.actions?.[spec.action];
|
|
170
|
+
if (!actionDef) {
|
|
171
|
+
return {
|
|
172
|
+
success: false,
|
|
173
|
+
data: null,
|
|
174
|
+
error: `Unknown action: ${spec.action} on ${spec.doctype}`,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
const handler = getHandler(actionDef.handler);
|
|
178
|
+
if (!handler) {
|
|
179
|
+
return {
|
|
180
|
+
success: false,
|
|
181
|
+
data: null,
|
|
182
|
+
error: `Handler not registered: ${actionDef.handler}`,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const actionContext = {
|
|
186
|
+
doctype: meta,
|
|
187
|
+
executor: options.executor,
|
|
188
|
+
};
|
|
189
|
+
try {
|
|
190
|
+
const result = await handler(spec.actionArgs ?? [], actionContext);
|
|
191
|
+
return {
|
|
192
|
+
success: true,
|
|
193
|
+
data: result,
|
|
194
|
+
error: null,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
return {
|
|
199
|
+
success: false,
|
|
200
|
+
data: null,
|
|
201
|
+
error: err instanceof Error ? err.message : String(err),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}));
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
// =============================================================================
|
|
214
|
+
// Inflection helpers — default to PostGraphile Amber preset conventions
|
|
215
|
+
// =============================================================================
|
|
216
|
+
function toCamelCase(str) {
|
|
217
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
218
|
+
}
|
|
219
|
+
function toPascalCase(str) {
|
|
220
|
+
const camel = toCamelCase(str);
|
|
221
|
+
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
222
|
+
}
|
|
223
|
+
/** Amber default: sales_orders → salesOrderById */
|
|
224
|
+
function defaultRecordFieldName(tableName) {
|
|
225
|
+
// sales_orders -> salesOrder (remove trailing 's' and camelCase) + ById
|
|
226
|
+
const singular = tableName.replace(/s$/, '');
|
|
227
|
+
return `${toCamelCase(singular)}ById`;
|
|
228
|
+
}
|
|
229
|
+
/** Amber default: sales_orders → allSalesOrders */
|
|
230
|
+
function defaultConnectionFieldName(tableName) {
|
|
231
|
+
return `all${toPascalCase(tableName)}`;
|
|
232
|
+
}
|
|
233
|
+
/** Amber default: sales_orders → SalesOrdersOrderBy */
|
|
234
|
+
function defaultOrderByTypeName(tableName) {
|
|
235
|
+
return `${toPascalCase(tableName)}OrderBy`;
|
|
236
|
+
}
|
|
237
|
+
// =============================================================================
|
|
238
|
+
// Query builders — generate GraphQL queries to send to the underlying schema
|
|
239
|
+
// =============================================================================
|
|
240
|
+
function buildRecordQuery(meta, recordFieldName) {
|
|
241
|
+
const fieldNames = meta.fields.map(f => f.fieldname).join('\n ');
|
|
242
|
+
const queryName = recordFieldName(meta.tableName);
|
|
243
|
+
return `
|
|
244
|
+
query GetRecord($id: UUID!) {
|
|
245
|
+
${queryName}(id: $id) {
|
|
246
|
+
${fieldNames}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
`;
|
|
250
|
+
}
|
|
251
|
+
function buildListQuery(meta, args, connectionFieldName, orderByTypeName) {
|
|
252
|
+
const fieldNames = meta.fields.map(f => f.fieldname).join('\n ');
|
|
253
|
+
const connectionName = connectionFieldName(meta.tableName);
|
|
254
|
+
const orderByType = orderByTypeName(meta.tableName);
|
|
255
|
+
const queryArgs = [];
|
|
256
|
+
if (args.limit)
|
|
257
|
+
queryArgs.push(`first: $limit`);
|
|
258
|
+
if (args.offset)
|
|
259
|
+
queryArgs.push(`offset: $offset`);
|
|
260
|
+
if (args.orderBy)
|
|
261
|
+
queryArgs.push(`orderBy: $orderBy`);
|
|
262
|
+
const argsStr = queryArgs.length > 0 ? `(${queryArgs.join(', ')})` : '';
|
|
263
|
+
return `
|
|
264
|
+
query GetRecords($limit: Int, $offset: Int, $orderBy: [${orderByType}!]) {
|
|
265
|
+
${connectionName}${argsStr} {
|
|
266
|
+
nodes {
|
|
267
|
+
${fieldNames}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
`;
|
|
272
|
+
}
|
|
273
|
+
function extractSingleResult(result, meta, recordFieldName) {
|
|
274
|
+
const queryName = recordFieldName(meta.tableName);
|
|
275
|
+
return result[queryName];
|
|
276
|
+
}
|
|
277
|
+
function extractListResult(result, meta, connectionFieldName) {
|
|
278
|
+
const connectionName = connectionFieldName(meta.tableName);
|
|
279
|
+
const connection = result[connectionName];
|
|
280
|
+
return connection?.nodes ?? [];
|
|
281
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
const handlerRegistry = new Map();
|
|
2
|
+
/**
|
|
3
|
+
* Register an action handler
|
|
4
|
+
* @param name - Unique name for the action handler
|
|
5
|
+
* @param handler - Action handler function to register
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export function registerHandler(name, handler) {
|
|
9
|
+
handlerRegistry.set(name, handler);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get a registered handler by name
|
|
13
|
+
* @param name - Name of the action handler to retrieve
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export function getHandler(name) {
|
|
17
|
+
return handlerRegistry.get(name);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Check if a handler is registered
|
|
21
|
+
* @param name - Name of the action handler to check
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export function hasHandler(name) {
|
|
25
|
+
return handlerRegistry.has(name);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Clear all registered handlers
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export function clearHandlers() {
|
|
32
|
+
handlerRegistry.clear();
|
|
33
|
+
}
|
|
34
|
+
// =============================================================================
|
|
35
|
+
// Built-in Handlers
|
|
36
|
+
// =============================================================================
|
|
37
|
+
/**
|
|
38
|
+
* Validate that all required fields are present in a record
|
|
39
|
+
*/
|
|
40
|
+
const validateRequiredFields = async (args, context) => {
|
|
41
|
+
const [record] = args;
|
|
42
|
+
const { doctype } = context;
|
|
43
|
+
const missing = [];
|
|
44
|
+
for (const field of doctype.fields) {
|
|
45
|
+
if (field.required && (record[field.fieldname] === undefined || record[field.fieldname] === null)) {
|
|
46
|
+
missing.push(field.label ?? field.fieldname);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (missing.length > 0) {
|
|
50
|
+
throw new Error(`Missing required fields: ${missing.join(', ')}`);
|
|
51
|
+
}
|
|
52
|
+
return { valid: true };
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Validate field types match expected types
|
|
56
|
+
*/
|
|
57
|
+
const validateFieldTypes = async (args, context) => {
|
|
58
|
+
const [record] = args;
|
|
59
|
+
const { doctype } = context;
|
|
60
|
+
const errors = [];
|
|
61
|
+
for (const field of doctype.fields) {
|
|
62
|
+
const value = record[field.fieldname];
|
|
63
|
+
if (value === undefined || value === null)
|
|
64
|
+
continue;
|
|
65
|
+
const error = validateFieldValue(field.fieldname, value, field.fieldtype);
|
|
66
|
+
if (error)
|
|
67
|
+
errors.push(error);
|
|
68
|
+
}
|
|
69
|
+
if (errors.length > 0) {
|
|
70
|
+
throw new Error(`Field type validation failed:\n${errors.join('\n')}`);
|
|
71
|
+
}
|
|
72
|
+
return { valid: true };
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Validate a single field value against its expected type
|
|
76
|
+
*/
|
|
77
|
+
function validateFieldValue(fieldname, value, fieldtype) {
|
|
78
|
+
switch (fieldtype) {
|
|
79
|
+
case 'Int':
|
|
80
|
+
if (typeof value !== 'number' || !Number.isInteger(value)) {
|
|
81
|
+
return `${fieldname}: expected integer, got ${typeof value}`;
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
case 'Float':
|
|
85
|
+
case 'Decimal':
|
|
86
|
+
case 'Currency':
|
|
87
|
+
case 'Quantity':
|
|
88
|
+
if (typeof value !== 'number' && typeof value !== 'string') {
|
|
89
|
+
return `${fieldname}: expected number, got ${typeof value}`;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
case 'Check':
|
|
93
|
+
if (typeof value !== 'boolean') {
|
|
94
|
+
return `${fieldname}: expected boolean, got ${typeof value}`;
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
case 'Data':
|
|
98
|
+
case 'Text':
|
|
99
|
+
case 'Code':
|
|
100
|
+
case 'Link':
|
|
101
|
+
if (typeof value !== 'string') {
|
|
102
|
+
return `${fieldname}: expected string, got ${typeof value}`;
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
case 'Date':
|
|
106
|
+
case 'Time':
|
|
107
|
+
case 'Datetime':
|
|
108
|
+
if (typeof value !== 'string' && !(value instanceof Date)) {
|
|
109
|
+
return `${fieldname}: expected date string or Date, got ${typeof value}`;
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
case 'JSON':
|
|
113
|
+
if (typeof value !== 'object') {
|
|
114
|
+
return `${fieldname}: expected object, got ${typeof value}`;
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case 'Table':
|
|
118
|
+
if (!Array.isArray(value)) {
|
|
119
|
+
return `${fieldname}: expected array, got ${typeof value}`;
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* No-op handler for testing or placeholder actions
|
|
127
|
+
*/
|
|
128
|
+
const noop = async () => {
|
|
129
|
+
return { ok: true };
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Built-in handlers available for registration
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
export const builtinHandlers = {
|
|
136
|
+
validateRequiredFields,
|
|
137
|
+
validateFieldTypes,
|
|
138
|
+
noop,
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Register all built-in handlers
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
export function registerBuiltinHandlers() {
|
|
145
|
+
for (const [name, handler] of Object.entries(builtinHandlers)) {
|
|
146
|
+
registerHandler(name, handler);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { parseDoctype, validateDoctype } from '../types';
|
|
4
|
+
const doctypeRegistry = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown when a doctype definition fails validation
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export class DoctypeValidationError extends Error {
|
|
10
|
+
file;
|
|
11
|
+
errors;
|
|
12
|
+
constructor(
|
|
13
|
+
/** File path or name where the validation error occurred */
|
|
14
|
+
file,
|
|
15
|
+
/** List of validation errors found */
|
|
16
|
+
errors) {
|
|
17
|
+
const errorMessages = errors.map(e => ` ${e.path.join('.')}: ${e.message}`).join('\n');
|
|
18
|
+
super(`Invalid doctype definition in ${file}:\n${errorMessages}`);
|
|
19
|
+
this.file = file;
|
|
20
|
+
this.errors = errors;
|
|
21
|
+
this.name = 'DoctypeValidationError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Load doctype definitions from a directory of JSON files
|
|
26
|
+
* @param dir - Directory path containing doctype JSON files
|
|
27
|
+
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export function loadDoctypes(dir, options = {}) {
|
|
31
|
+
const entries = readdirSync(dir);
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
const fullPath = join(dir, entry);
|
|
34
|
+
const stat = statSync(fullPath);
|
|
35
|
+
if (stat.isDirectory()) {
|
|
36
|
+
loadDoctypes(fullPath, options);
|
|
37
|
+
}
|
|
38
|
+
else if (entry.endsWith('.json')) {
|
|
39
|
+
loadDoctypeFile(fullPath, options);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Load and validate a single doctype file
|
|
45
|
+
*/
|
|
46
|
+
function loadDoctypeFile(filePath, options) {
|
|
47
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
48
|
+
let data;
|
|
49
|
+
try {
|
|
50
|
+
data = JSON.parse(content);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
const parseError = {
|
|
54
|
+
path: [],
|
|
55
|
+
message: `Invalid JSON: ${err instanceof Error ? err.message : String(err)}`,
|
|
56
|
+
};
|
|
57
|
+
if (options.continueOnError) {
|
|
58
|
+
options.onError?.(filePath, [parseError]);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
throw new DoctypeValidationError(filePath, [parseError]);
|
|
62
|
+
}
|
|
63
|
+
const result = validateDoctype(data);
|
|
64
|
+
if (!result.success) {
|
|
65
|
+
if (options.continueOnError) {
|
|
66
|
+
options.onError?.(filePath, result.errors);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
throw new DoctypeValidationError(filePath, result.errors);
|
|
70
|
+
}
|
|
71
|
+
const doctype = parseDoctype(data);
|
|
72
|
+
doctypeRegistry.set(doctype.name, doctype);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Load doctypes from an object (for programmatic use)
|
|
76
|
+
* @param doctypes - Object mapping doctype names to doctype definitions
|
|
77
|
+
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export function loadDoctypesFromObject(doctypes, options = {}) {
|
|
81
|
+
for (const [name, data] of Object.entries(doctypes)) {
|
|
82
|
+
const withName = { ...data, name };
|
|
83
|
+
const result = validateDoctype(withName);
|
|
84
|
+
if (!result.success) {
|
|
85
|
+
if (options.continueOnError) {
|
|
86
|
+
options.onError?.(name, result.errors);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
throw new DoctypeValidationError(name, result.errors);
|
|
90
|
+
}
|
|
91
|
+
const doctype = parseDoctype(withName);
|
|
92
|
+
doctypeRegistry.set(doctype.name, doctype);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get a doctype by name
|
|
97
|
+
* @param name - Name of the doctype to retrieve
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
export function getMeta(name) {
|
|
101
|
+
return doctypeRegistry.get(name);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get all loaded doctypes
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
export function getAllMeta() {
|
|
108
|
+
return Array.from(doctypeRegistry.values());
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Check if a doctype is registered
|
|
112
|
+
* @param name - Name of the doctype to check
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
export function hasMeta(name) {
|
|
116
|
+
return doctypeRegistry.has(name);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Clear all registered doctypes
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export function clearRegistry() {
|
|
123
|
+
doctypeRegistry.clear();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Validate cross-doctype references (Link fields, inherits, etc.)
|
|
127
|
+
* Call after all doctypes are loaded.
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
export function validateReferences() {
|
|
131
|
+
const errors = [];
|
|
132
|
+
for (const doctype of doctypeRegistry.values()) {
|
|
133
|
+
// Check inherits reference
|
|
134
|
+
if (doctype.inherits && !doctypeRegistry.has(doctype.inherits)) {
|
|
135
|
+
errors.push({
|
|
136
|
+
path: [doctype.name, 'inherits'],
|
|
137
|
+
message: `References unknown doctype: ${doctype.inherits}`,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
// Check listDoctype reference
|
|
141
|
+
if (doctype.listDoctype && !doctypeRegistry.has(doctype.listDoctype)) {
|
|
142
|
+
errors.push({
|
|
143
|
+
path: [doctype.name, 'listDoctype'],
|
|
144
|
+
message: `References unknown doctype: ${doctype.listDoctype}`,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
// Check parentDoctype reference
|
|
148
|
+
if (doctype.parentDoctype && !doctypeRegistry.has(doctype.parentDoctype)) {
|
|
149
|
+
errors.push({
|
|
150
|
+
path: [doctype.name, 'parentDoctype'],
|
|
151
|
+
message: `References unknown doctype: ${doctype.parentDoctype}`,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// Check Link field targets
|
|
155
|
+
for (const field of doctype.fields) {
|
|
156
|
+
if (field.fieldtype === 'Link' && typeof field.options === 'string') {
|
|
157
|
+
if (!doctypeRegistry.has(field.options)) {
|
|
158
|
+
errors.push({
|
|
159
|
+
path: [doctype.name, 'fields', field.fieldname, 'options'],
|
|
160
|
+
message: `Link references unknown doctype: ${field.options}`,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return errors;
|
|
167
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.57.0"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DoctypeMeta } from '@stonecrop/schema';
|
|
2
1
|
export { StonecropFieldType, FieldOptions, FieldMeta, ActionDefinition, WorkflowMeta, DoctypeMeta, validateDoctype, validateField, parseDoctype, parseField, } from '@stonecrop/schema';
|
|
3
2
|
export type { ValidationResult, ValidationError, IntrospectionSource, GraphQLConversionOptions, GraphQLConversionFieldMeta, ConvertedGraphQLDoctype, } from '@stonecrop/schema';
|
|
3
|
+
import type { DoctypeMeta } from '@stonecrop/schema';
|
|
4
4
|
/**
|
|
5
5
|
* Route context for identifying what doctype/record we're working with
|
|
6
6
|
* @public
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/graphql-middleware",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
7
|
+
"types": "./dist/src/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
10
|
+
"types": "./dist/src/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js",
|
|
12
12
|
"require": "./dist/index.cjs"
|
|
13
13
|
}
|
|
@@ -18,14 +18,13 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"graphql": "^16.12.0",
|
|
20
20
|
"postgraphile": "^5.0.0-rc.7",
|
|
21
|
-
"@stonecrop/schema": "0.8.
|
|
21
|
+
"@stonecrop/schema": "0.8.9"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@rushstack/heft": "^1.
|
|
24
|
+
"@rushstack/heft": "^1.2.0",
|
|
25
25
|
"@types/node": "^22.19.5",
|
|
26
26
|
"typescript": "^5.9.3",
|
|
27
27
|
"vite": "^7.3.1",
|
|
28
|
-
"vite-plugin-dts": "^4.5.4",
|
|
29
28
|
"vitest": "^4.0.18",
|
|
30
29
|
"stonecrop-rig": "0.7.0"
|
|
31
30
|
},
|
package/dist/index.d.ts
DELETED