@stonecrop/graphql-middleware 0.10.15 → 0.11.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/README.md +124 -107
- package/dist/graphql-middleware.d.ts +0 -23
- package/dist/index.cjs +86 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +150 -161
- package/dist/index.js.map +1 -1
- package/dist/plugin/postgraphile.js +1 -3
- package/dist/registry/actions.js +19 -12
- package/dist/registry/doctypes.js +0 -14
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +8 -0
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +381 -0
- package/dist/src/registry/actions.d.ts.map +1 -1
- package/dist/src/registry/actions.js +156 -0
- package/dist/src/registry/doctypes.d.ts.map +1 -1
- package/dist/src/registry/doctypes.js +167 -0
- package/dist/src/tsdoc-metadata.json +11 -0
- package/dist/src/types/index.js +3 -0
- package/package.json +2 -2
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { parseDoctype, validateDoctype } from '@stonecrop/schema';
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/graphql-middleware",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/graphql-middleware.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"graphql": "^16.12.0",
|
|
18
18
|
"pluralize": "^8.0.0",
|
|
19
19
|
"postgraphile": "^5.0.0-rc.7",
|
|
20
|
-
"@stonecrop/schema": "0.
|
|
20
|
+
"@stonecrop/schema": "0.11.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@rushstack/heft": "^1.2.0",
|