@stonecrop/graphql-middleware 0.12.8 → 0.13.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 +103 -63
- package/dist/graphql-middleware.d.ts +111 -246
- package/dist/index.js +352 -408
- package/dist/index.js.map +1 -1
- package/dist/plugin/postgraphile.js +583 -0
- package/dist/preset.js +45 -0
- package/dist/registry/actions.js +163 -0
- package/dist/registry/doctypes.js +161 -0
- package/dist/src/debug.d.ts +31 -0
- package/dist/src/debug.d.ts.map +1 -0
- package/dist/src/debug.js +49 -0
- package/dist/src/index.d.ts +9 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +7 -2
- package/dist/src/plugin/postgraphile.d.ts +15 -171
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +253 -455
- package/dist/src/preset.d.ts +53 -0
- package/dist/src/preset.d.ts.map +1 -0
- package/dist/src/preset.js +54 -0
- package/dist/src/registry/fetchHandlers.d.ts +31 -0
- package/dist/src/registry/fetchHandlers.d.ts.map +1 -0
- package/dist/src/registry/fetchHandlers.js +24 -0
- package/dist/src/types/context.d.ts +20 -0
- package/dist/src/types/context.d.ts.map +1 -0
- package/dist/src/types/index.d.ts +4 -14
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/preset.d.ts +18 -0
- package/dist/src/types/preset.d.ts.map +1 -0
- package/dist/typeDefs.js +79 -0
- package/dist/types/context.js +0 -0
- package/dist/types/index.js +3 -0
- package/dist/types/preset.js +0 -0
- package/dist/types/query.js +3 -0
- package/package.json +7 -2
|
@@ -0,0 +1,163 @@
|
|
|
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, value);
|
|
66
|
+
if (error)
|
|
67
|
+
errors.push(error);
|
|
68
|
+
}
|
|
69
|
+
// Validate link fields
|
|
70
|
+
if (doctype.links) {
|
|
71
|
+
for (const [fieldname, link] of Object.entries(doctype.links)) {
|
|
72
|
+
const value = record[fieldname];
|
|
73
|
+
if (value === undefined || value === null)
|
|
74
|
+
continue;
|
|
75
|
+
const isMany = link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne';
|
|
76
|
+
if (isMany) {
|
|
77
|
+
if (!Array.isArray(value)) {
|
|
78
|
+
errors.push(`${fieldname}: expected array, got ${typeof value}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
83
|
+
errors.push(`${fieldname}: expected object, got ${typeof value}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (errors.length > 0) {
|
|
89
|
+
throw new Error(`Field type validation failed:\n${errors.join('\n')}`);
|
|
90
|
+
}
|
|
91
|
+
return { valid: true };
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Validate a single field value against its expected type
|
|
95
|
+
*/
|
|
96
|
+
function validateFieldValue(field, value) {
|
|
97
|
+
const { fieldname, fieldtype, cardinality } = field;
|
|
98
|
+
switch (fieldtype) {
|
|
99
|
+
case 'Int':
|
|
100
|
+
if (typeof value !== 'number' || !Number.isInteger(value)) {
|
|
101
|
+
return `${fieldname}: expected integer, got ${typeof value}`;
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
104
|
+
case 'Float':
|
|
105
|
+
case 'Decimal':
|
|
106
|
+
case 'Currency':
|
|
107
|
+
case 'Quantity':
|
|
108
|
+
if (typeof value !== 'number' && typeof value !== 'string') {
|
|
109
|
+
return `${fieldname}: expected number, got ${typeof value}`;
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
case 'Check':
|
|
113
|
+
if (typeof value !== 'boolean') {
|
|
114
|
+
return `${fieldname}: expected boolean, got ${typeof value}`;
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case 'Data':
|
|
118
|
+
case 'Text':
|
|
119
|
+
case 'Code':
|
|
120
|
+
case 'Link':
|
|
121
|
+
if (typeof value !== 'string') {
|
|
122
|
+
return `${fieldname}: expected string, got ${typeof value}`;
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
case 'Date':
|
|
126
|
+
case 'Time':
|
|
127
|
+
case 'Datetime':
|
|
128
|
+
if (typeof value !== 'string' && !(value instanceof Date)) {
|
|
129
|
+
return `${fieldname}: expected date string or Date, got ${typeof value}`;
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
case 'JSON':
|
|
133
|
+
if (typeof value !== 'object') {
|
|
134
|
+
return `${fieldname}: expected object, got ${typeof value}`;
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* No-op handler for testing or placeholder actions
|
|
142
|
+
*/
|
|
143
|
+
const noop = async () => {
|
|
144
|
+
return { ok: true };
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Built-in handlers available for registration
|
|
148
|
+
* @public
|
|
149
|
+
*/
|
|
150
|
+
export const builtinHandlers = {
|
|
151
|
+
validateRequiredFields,
|
|
152
|
+
validateFieldTypes,
|
|
153
|
+
noop,
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Register all built-in handlers
|
|
157
|
+
* @public
|
|
158
|
+
*/
|
|
159
|
+
export function registerBuiltinHandlers() {
|
|
160
|
+
for (const [name, handler] of Object.entries(builtinHandlers)) {
|
|
161
|
+
registerHandler(name, handler);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
const direct = doctypeRegistry.get(name);
|
|
102
|
+
if (direct)
|
|
103
|
+
return direct;
|
|
104
|
+
// Fallback: find by slug (links reference doctypes by slug, not name)
|
|
105
|
+
for (const doctype of doctypeRegistry.values()) {
|
|
106
|
+
if (doctype.slug === name)
|
|
107
|
+
return doctype;
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get all loaded doctypes
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
export function getAllMeta() {
|
|
116
|
+
return Array.from(doctypeRegistry.values());
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Check if a doctype is registered
|
|
120
|
+
* @param name - Name of the doctype to check
|
|
121
|
+
* @public
|
|
122
|
+
*/
|
|
123
|
+
export function hasMeta(name) {
|
|
124
|
+
return doctypeRegistry.has(name);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Clear all registered doctypes
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
export function clearRegistry() {
|
|
131
|
+
doctypeRegistry.clear();
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Validate cross-doctype references (Link fields, inherits, etc.)
|
|
135
|
+
* Call after all doctypes are loaded.
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
export function validateReferences() {
|
|
139
|
+
const errors = [];
|
|
140
|
+
for (const doctype of doctypeRegistry.values()) {
|
|
141
|
+
// Check inherits reference
|
|
142
|
+
if (doctype.inherits && !doctypeRegistry.has(doctype.inherits)) {
|
|
143
|
+
errors.push({
|
|
144
|
+
path: [doctype.name, 'inherits'],
|
|
145
|
+
message: `References unknown doctype: ${doctype.inherits}`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Check Link field targets
|
|
149
|
+
for (const field of doctype.fields) {
|
|
150
|
+
if (field.fieldtype === 'Link' && typeof field.options === 'string') {
|
|
151
|
+
if (!doctypeRegistry.has(field.options)) {
|
|
152
|
+
errors.push({
|
|
153
|
+
path: [doctype.name, 'fields', field.fieldname, 'options'],
|
|
154
|
+
message: `Link references unknown doctype: ${field.options}`,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return errors;
|
|
161
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for the Stonecrop debug plugin.
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export interface DebugPluginOptions {
|
|
6
|
+
/** Log a message when a plan is built for a Stonecrop field. Default: `true` */
|
|
7
|
+
logPlans?: boolean;
|
|
8
|
+
/** Log timing for plan construction. Default: `false` */
|
|
9
|
+
logTiming?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a PostGraphile plugin that wraps Stonecrop resolver plans with
|
|
13
|
+
* debug logging. Use it in your preset file:
|
|
14
|
+
*
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { createStonecropPlugin, createDebugPlugin } from '@stonecrop/graphql-middleware'
|
|
17
|
+
*
|
|
18
|
+
* export default {
|
|
19
|
+
* plugins: [createStonecropPlugin(), createDebugPlugin()],
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Place `createDebugPlugin()` **after** `createStonecropPlugin()` in the
|
|
24
|
+
* plugins array so the wrapper sees the Stonecrop fields.
|
|
25
|
+
*
|
|
26
|
+
* @param options - Optional logging configuration
|
|
27
|
+
* @returns A PostGraphile plugin
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export declare const createDebugPlugin: (options?: DebugPluginOptions) => GraphileConfig.Plugin;
|
|
31
|
+
//# sourceMappingURL=debug.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.d.ts","sourceRoot":"","sources":["../../src/debug.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IAClC,gFAAgF;IAChF,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,yDAAyD;IACzD,SAAS,CAAC,EAAE,OAAO,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,iBAAiB,GAAI,UAAS,kBAAuB,KAAG,cAAc,CAAC,MAmCnF,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { wrapPlans } from 'postgraphile/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a PostGraphile plugin that wraps Stonecrop resolver plans with
|
|
4
|
+
* debug logging. Use it in your preset file:
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { createStonecropPlugin, createDebugPlugin } from '@stonecrop/graphql-middleware'
|
|
8
|
+
*
|
|
9
|
+
* export default {
|
|
10
|
+
* plugins: [createStonecropPlugin(), createDebugPlugin()],
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Place `createDebugPlugin()` **after** `createStonecropPlugin()` in the
|
|
15
|
+
* plugins array so the wrapper sees the Stonecrop fields.
|
|
16
|
+
*
|
|
17
|
+
* @param options - Optional logging configuration
|
|
18
|
+
* @returns A PostGraphile plugin
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export const createDebugPlugin = (options = {}) => {
|
|
22
|
+
const { logPlans = true, logTiming = false } = options;
|
|
23
|
+
const wrapField = (typeName, fieldName) => ({
|
|
24
|
+
plan: (plan) => {
|
|
25
|
+
if (logPlans) {
|
|
26
|
+
// eslint-disable-next-line no-console
|
|
27
|
+
console.log(`[@stonecrop/graphql-middleware] Building plan for ${typeName}.${fieldName}`);
|
|
28
|
+
}
|
|
29
|
+
const start = logTiming ? Date.now() : 0;
|
|
30
|
+
const $result = plan();
|
|
31
|
+
if (logTiming) {
|
|
32
|
+
// eslint-disable-next-line no-console
|
|
33
|
+
console.log(`[@stonecrop/graphql-middleware] Plan built for ${typeName}.${fieldName} in ${Date.now() - start}ms`);
|
|
34
|
+
}
|
|
35
|
+
return $result;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
return wrapPlans({
|
|
39
|
+
Query: {
|
|
40
|
+
stonecropRecord: wrapField('Query', 'stonecropRecord'),
|
|
41
|
+
stonecropRecords: wrapField('Query', 'stonecropRecords'),
|
|
42
|
+
stonecropMeta: wrapField('Query', 'stonecropMeta'),
|
|
43
|
+
stonecropAllMeta: wrapField('Query', 'stonecropAllMeta'),
|
|
44
|
+
},
|
|
45
|
+
Mutation: {
|
|
46
|
+
stonecropAction: wrapField('Mutation', 'stonecropAction'),
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
};
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
export type { DoctypeMeta, ValidationError } from '@stonecrop/schema';
|
|
2
|
-
export type { ActionContext, ActionHandler
|
|
2
|
+
export type { ActionContext, ActionHandler } from './types';
|
|
3
3
|
export { loadDoctypes, loadDoctypesFromObject, getMeta, getAllMeta, hasMeta, clearRegistry, validateReferences, DoctypeValidationError, } from './registry/doctypes';
|
|
4
4
|
export type { LoadDoctypesOptions } from './registry/doctypes';
|
|
5
5
|
export { registerHandler, getHandler, hasHandler, clearHandlers, registerBuiltinHandlers, builtinHandlers, } from './registry/actions';
|
|
6
|
+
export { registerFetchHandler, getFetchHandler, clearFetchHandlers } from './registry/fetchHandlers';
|
|
7
|
+
export type { FetchHandler } from './registry/fetchHandlers';
|
|
6
8
|
export { createStonecropPlugin } from './plugin/postgraphile';
|
|
7
|
-
export type { StonecropPluginOptions
|
|
8
|
-
export {
|
|
9
|
-
export type {
|
|
9
|
+
export type { StonecropPluginOptions } from './plugin/postgraphile';
|
|
10
|
+
export { createDebugPlugin } from './debug';
|
|
11
|
+
export type { DebugPluginOptions } from './debug';
|
|
10
12
|
export { typeDefs } from './typeDefs';
|
|
13
|
+
export { createStonecropPreset, StonecropPreset } from './preset';
|
|
14
|
+
export type { FieldCasing } from './preset';
|
|
15
|
+
export { makePgService } from 'postgraphile/adaptors/pg';
|
|
11
16
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAGrE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAGrE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAG3D,OAAO,EACN,YAAY,EACZ,sBAAsB,EACtB,OAAO,EACP,UAAU,EACV,OAAO,EACP,aAAa,EACb,kBAAkB,EAClB,sBAAsB,GACtB,MAAM,qBAAqB,CAAA;AAE5B,YAAY,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAG9D,OAAO,EACN,eAAe,EACf,UAAU,EACV,UAAU,EACV,aAAa,EACb,uBAAuB,EACvB,eAAe,GACf,MAAM,oBAAoB,CAAA;AAG3B,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AACpG,YAAY,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAG5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAA;AAC7D,YAAY,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AAGnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAC3C,YAAY,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAGrC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AACjE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA"}
|
package/dist/src/index.js
CHANGED
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
export { loadDoctypes, loadDoctypesFromObject, getMeta, getAllMeta, hasMeta, clearRegistry, validateReferences, DoctypeValidationError, } from './registry/doctypes';
|
|
3
3
|
// Action handlers
|
|
4
4
|
export { registerHandler, getHandler, hasHandler, clearHandlers, registerBuiltinHandlers, builtinHandlers, } from './registry/actions';
|
|
5
|
+
// Fetch handlers
|
|
6
|
+
export { registerFetchHandler, getFetchHandler, clearFetchHandlers } from './registry/fetchHandlers';
|
|
5
7
|
// PostGraphile plugin
|
|
6
8
|
export { createStonecropPlugin } from './plugin/postgraphile';
|
|
7
|
-
//
|
|
8
|
-
export {
|
|
9
|
+
// Debug plugin
|
|
10
|
+
export { createDebugPlugin } from './debug';
|
|
9
11
|
export { typeDefs } from './typeDefs';
|
|
12
|
+
// Preset
|
|
13
|
+
export { createStonecropPreset, StonecropPreset } from './preset';
|
|
14
|
+
export { makePgService } from 'postgraphile/adaptors/pg';
|
|
@@ -1,185 +1,29 @@
|
|
|
1
|
-
import type { DoctypeMeta } from '@stonecrop/schema';
|
|
2
1
|
import { GraphileConfig } from 'postgraphile/graphile-build';
|
|
3
|
-
import type { GraphQLExecutor, ReverseConnectionParams, BuildRecordQueryOptions, BuildListQueryArgs, MergeNestedResultsParams, ExtractSingleResultParams, ExtractListResultParams } from '../types';
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Override these when using a non-Amber inflection preset (e.g., V4, SimplifyInflection).
|
|
7
|
-
*
|
|
8
|
-
* Defaults match the PostGraphile Amber preset conventions.
|
|
3
|
+
* Options for creating a Stonecrop PostGraphile plugin.
|
|
9
4
|
* @public
|
|
10
5
|
*/
|
|
11
|
-
export interface
|
|
12
|
-
/**
|
|
13
|
-
* Given a table name, return the GraphQL field name for fetching a single record by ID.
|
|
14
|
-
* @example Amber default: "sales_orders" → "salesOrderById"
|
|
15
|
-
* @example V4 preset: "sales_orders" → "salesOrderByRowId"
|
|
16
|
-
*/
|
|
17
|
-
recordFieldName?: (tableName: string) => string;
|
|
18
|
-
/**
|
|
19
|
-
* Given a table name, return the GraphQL field name for fetching a list/connection.
|
|
20
|
-
* @example Amber default: "sales_orders" → "allSalesOrders"
|
|
21
|
-
* @example SimplifyInflection: "sales_orders" → "salesOrders"
|
|
22
|
-
*/
|
|
23
|
-
connectionFieldName?: (tableName: string) => string;
|
|
24
|
-
/**
|
|
25
|
-
* Given a table name, return the GraphQL OrderBy enum type name.
|
|
26
|
-
* @example Amber default: "sales_orders" → "SalesOrdersOrderBy"
|
|
27
|
-
*/
|
|
28
|
-
orderByTypeName?: (tableName: string) => string;
|
|
29
|
-
/**
|
|
30
|
-
* Given a table name, return the GraphQL argument name used to look up a record by PK.
|
|
31
|
-
* @example Amber default (Relay Global ID): "sales_orders" → "id"
|
|
32
|
-
* @example row_id PK: "sales_orders" → "rowId"
|
|
33
|
-
*/
|
|
34
|
-
recordArgName?: (tableName: string) => string;
|
|
35
|
-
/**
|
|
36
|
-
* Given a table name, return the GraphQL variable type for the PK argument.
|
|
37
|
-
* @example Default UUID PK: "sales_orders" → "UUID!"
|
|
38
|
-
* @example Integer PK: "sales_orders" → "Int!"
|
|
39
|
-
*/
|
|
40
|
-
recordArgType?: (tableName: string) => string;
|
|
6
|
+
export interface StonecropPluginOptions {
|
|
41
7
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* - When backlink is provided: FK column is derived from the backlink field
|
|
45
|
-
* - When backlink is absent: FK column is derived from the parent doctype
|
|
46
|
-
* @example Recipe → RecipeTasksByRecipeId
|
|
8
|
+
* Primary key column name used in all `stonecropRecord` lookups and linked-record fetches.
|
|
9
|
+
* Defaults to `'id'`.
|
|
47
10
|
*/
|
|
48
|
-
|
|
49
|
-
/** Parent doctype slug */
|
|
50
|
-
doctype: string;
|
|
51
|
-
/** Link key on the parent */
|
|
52
|
-
linkName: string;
|
|
53
|
-
/** Link field on the target that points back to the parent (optional) */
|
|
54
|
-
backlink?: string;
|
|
55
|
-
/** Target doctype slug */
|
|
56
|
-
target: string;
|
|
57
|
-
}) => string;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Options for stonecropRecord queries
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
export interface StonecropRecordOptions {
|
|
64
|
-
/** Include nested/related records */
|
|
65
|
-
includeNested?: boolean | string[];
|
|
66
|
-
/** Maximum nesting depth */
|
|
67
|
-
maxDepth?: number;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Options for creating a Stonecrop PostGraphile plugin
|
|
71
|
-
* @public
|
|
72
|
-
*/
|
|
73
|
-
export interface StonecropPluginOptions {
|
|
74
|
-
/** GraphQL executor for running queries/mutations */
|
|
75
|
-
executor: GraphQLExecutor;
|
|
11
|
+
pkField?: string;
|
|
76
12
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
13
|
+
* When `true`, SQL queries executed inside `loadOneWithPgClient` callbacks
|
|
14
|
+
* are logged to `console.log` with `[@stonecrop/graphql-middleware]` prefix. Defaults to `false`.
|
|
79
15
|
*/
|
|
80
|
-
|
|
16
|
+
debug?: boolean;
|
|
81
17
|
}
|
|
82
18
|
/**
|
|
83
|
-
* Create a PostGraphile plugin that extends the GraphQL schema with Stonecrop functionality
|
|
84
|
-
* @param options - Plugin configuration options
|
|
85
|
-
* @returns A PostGraphile plugin
|
|
86
|
-
* @public
|
|
87
|
-
*/
|
|
88
|
-
export declare const createStonecropPlugin: (options: StonecropPluginOptions) => GraphileConfig.Plugin;
|
|
89
|
-
/**
|
|
90
|
-
* Amber default: sales_orders → salesOrderById
|
|
91
|
-
* Uses `pluralize` for proper singularization of irregular plurals.
|
|
92
|
-
* Override via `StonecropInflectionConfig.recordFieldName` for non-standard PK columns.
|
|
93
|
-
* @public
|
|
94
|
-
*/
|
|
95
|
-
declare function defaultRecordFieldName(tableName: string): string;
|
|
96
|
-
/**
|
|
97
|
-
* Amber default: sales_orders → allSalesOrders
|
|
98
|
-
* @public
|
|
99
|
-
*/
|
|
100
|
-
declare function defaultConnectionFieldName(tableName: string): string;
|
|
101
|
-
/**
|
|
102
|
-
* Amber default: sales_orders → SalesOrdersOrderBy
|
|
103
|
-
* @public
|
|
104
|
-
*/
|
|
105
|
-
declare function defaultOrderByTypeName(tableName: string): string;
|
|
106
|
-
/**
|
|
107
|
-
* Default PK argument name: 'id' (standard Relay Global ID pattern).
|
|
108
|
-
* Override via `StonecropInflectionConfig.recordArgName` when using row_id columns;
|
|
109
|
-
* PostGraphile Amber generates `rowId: UUID!` for those fields.
|
|
110
|
-
* @public
|
|
111
|
-
*/
|
|
112
|
-
declare function defaultRecordArgName(_tableName: string): string;
|
|
113
|
-
/**
|
|
114
|
-
* Default PK argument type: 'UUID!' (PostGraphile Amber default for UUID PKs).
|
|
115
|
-
* Override via `StonecropInflectionConfig.recordArgType` when using non-UUID PKs
|
|
116
|
-
* such as integer serials or Relay Global IDs ('ID!').
|
|
117
|
-
* @public
|
|
118
|
-
*/
|
|
119
|
-
declare function defaultRecordArgType(_tableName: string): string;
|
|
120
|
-
/**
|
|
121
|
-
* Default reverse connection name: derives PostGraphile's connection field convention.
|
|
122
|
-
* PostGraphile convention: `{targetPlural}By{FkColumnPascal}Id`
|
|
123
|
-
* - When backlink is defined: FK column is derived from the backlink field name
|
|
124
|
-
* - When backlink is absent: FK column is derived from the parent doctype's table name
|
|
125
|
-
* @public
|
|
126
|
-
*/
|
|
127
|
-
declare function defaultReverseConnectionName(params: {
|
|
128
|
-
doctype: string;
|
|
129
|
-
linkName: string;
|
|
130
|
-
backlink?: string;
|
|
131
|
-
target: string;
|
|
132
|
-
}): string;
|
|
133
|
-
/**
|
|
134
|
-
* Fieldtypes unconditionally excluded from the generated scalar query selection set.
|
|
135
|
-
* - `'Display'`: display-only composite component with no backing DB column
|
|
19
|
+
* Create a PostGraphile plugin that extends the GraphQL schema with Stonecrop functionality.
|
|
136
20
|
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* @
|
|
142
|
-
*/
|
|
143
|
-
declare const RELATION_FIELDTYPES: Set<string>;
|
|
144
|
-
/**
|
|
145
|
-
* Filter fields to only those directly queryable as scalars.
|
|
146
|
-
* Excludes Display fields (no backing DB column) and Link fields that have an
|
|
147
|
-
* explicit `links` declaration (those require sub-selection, not scalar reads).
|
|
148
|
-
* Link fields without a `links` declaration are scalar FK UUID columns and ARE included.
|
|
149
|
-
* @public
|
|
150
|
-
*/
|
|
151
|
-
declare function queryableFieldNames(meta: DoctypeMeta): string;
|
|
152
|
-
/**
|
|
153
|
-
* Build a GraphQL query to fetch a single record by ID.
|
|
154
|
-
* When includeNested is set, recursively includes descendant link sub-selections.
|
|
155
|
-
* @public
|
|
156
|
-
*/
|
|
157
|
-
declare function buildRecordQuery(meta: DoctypeMeta, recordFieldName: (t: string) => string, recordArgName: (t: string) => string, recordArgType: (t: string) => string, getMeta: (slug: string) => DoctypeMeta | undefined, options?: BuildRecordQueryOptions, reverseConnectionNameFn?: (params: ReverseConnectionParams) => string): string;
|
|
158
|
-
/**
|
|
159
|
-
* Build a GraphQL connection query to fetch a list of records.
|
|
160
|
-
* Only declares variables ($limit, $offset, $orderBy) that are actually used in the query,
|
|
161
|
-
* avoiding GraphQL spec §5.8.3 violations from unused variable declarations.
|
|
162
|
-
* Excludes Link relation fields and Display fields from the selection set.
|
|
163
|
-
* @public
|
|
164
|
-
*/
|
|
165
|
-
declare function buildListQuery(meta: DoctypeMeta, args: BuildListQueryArgs, connectionFieldName: (t: string) => string, orderByTypeName: (t: string) => string): string;
|
|
166
|
-
/**
|
|
167
|
-
* Merge nested connection results into flat arrays.
|
|
168
|
-
* For `noneOrMany`/`atLeastOne` links, the query returns `{ nodes: [...] }`.
|
|
169
|
-
* This flattens them to just `[]` for easier consumption.
|
|
170
|
-
* @public
|
|
171
|
-
*/
|
|
172
|
-
declare function mergeNestedResults(params: MergeNestedResultsParams): Record<string, unknown>;
|
|
173
|
-
/**
|
|
174
|
-
* Extract a single record from a PostGraphile query result using the record field name.
|
|
175
|
-
* @public
|
|
176
|
-
*/
|
|
177
|
-
declare function extractSingleResult(params: ExtractSingleResultParams): unknown;
|
|
178
|
-
/**
|
|
179
|
-
* Extract the list of nodes from a PostGraphile connection query result.
|
|
180
|
-
* Returns an empty array if the connection field is absent.
|
|
21
|
+
* The `PgExecutor` is obtained automatically from `build.input.pgRegistry.pgExecutors`
|
|
22
|
+
* during schema construction — it does not need to be supplied by the caller.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Optional plugin configuration
|
|
25
|
+
* @returns A PostGraphile plugin
|
|
181
26
|
* @public
|
|
182
27
|
*/
|
|
183
|
-
declare
|
|
184
|
-
export { defaultRecordFieldName, defaultConnectionFieldName, defaultOrderByTypeName, defaultRecordArgName, defaultRecordArgType, defaultReverseConnectionName, buildRecordQuery, buildListQuery, queryableFieldNames, RELATION_FIELDTYPES, extractSingleResult, extractListResult, mergeNestedResults, };
|
|
28
|
+
export declare const createStonecropPlugin: (options?: StonecropPluginOptions) => GraphileConfig.Plugin;
|
|
185
29
|
//# sourceMappingURL=postgraphile.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgraphile.d.ts","sourceRoot":"","sources":["../../../src/plugin/postgraphile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"postgraphile.d.ts","sourceRoot":"","sources":["../../../src/plugin/postgraphile.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAS5D;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACtC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,qBAAqB,GAAI,UAAS,sBAA2B,KAAG,cAAc,CAAC,MAkU3F,CAAA"}
|