@stonecrop/graphql-middleware 0.11.0 → 0.11.2
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 +59 -63
- package/dist/graphql-middleware.d.ts +165 -0
- package/dist/index.js +422 -225
- package/dist/index.js.map +1 -1
- package/dist/plugin/postgraphile.js +216 -20
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.d.ts +56 -16
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/typeDefs.d.ts +7 -0
- package/dist/src/typeDefs.d.ts.map +1 -0
- package/dist/src/types/index.d.ts +1 -0
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/query.d.ts +130 -0
- package/dist/src/types/query.d.ts.map +1 -0
- package/dist/typeDefs.js +79 -0
- package/dist/{src/types/index.js → types/query.js} +1 -1
- package/package.json +2 -2
- package/dist/index.cjs +0 -86
- package/dist/index.cjs.map +0 -1
- package/dist/src/index.js +0 -8
- package/dist/src/plugin/postgraphile.js +0 -381
- package/dist/src/registry/actions.js +0 -156
- package/dist/src/registry/doctypes.js +0 -167
- package/dist/src/tsdoc-metadata.json +0 -11
package/README.md
CHANGED
|
@@ -1,57 +1,70 @@
|
|
|
1
1
|
# @stonecrop/graphql-middleware
|
|
2
2
|
|
|
3
|
-
GraphQL backend for the Stonecrop framework.
|
|
3
|
+
GraphQL backend for the Stonecrop framework. Reads doctype schemas and exposes them as GraphQL resolvers that handle query construction, response parsing, and action dispatch.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Currently integrated with PostGraphile (to be extracted into a swappable adapter in the future).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
- **Query construction** — Builds GraphQL queries from doctype metadata, respecting fetch strategies and nesting depth
|
|
10
|
+
- **Response merging** — Flattens connection format (`{ nodes: [...] }`) into plain arrays
|
|
11
|
+
- **Action dispatch** — Routes doctype actions to registered handlers
|
|
12
|
+
|
|
13
|
+
## How it works
|
|
14
|
+
|
|
15
|
+
Doctype schemas declare fields, relationships, and workflow. The middleware uses that schema to:
|
|
16
|
+
|
|
17
|
+
1. Accept a `doctype` and optional `options` from the client
|
|
18
|
+
2. Build a query with field selections and (if `includeNested` is set) nested sub-selections for related records
|
|
19
|
+
3. Execute against the GraphQL engine and merge the response
|
|
20
|
+
4. Return flat data to the client
|
|
21
|
+
|
|
22
|
+
The client never constructs queries — it passes `includeNested` through and receives pre-merged results.
|
|
23
|
+
|
|
24
|
+
## Setup
|
|
25
|
+
|
|
26
|
+
The middleware is a PostGraphile plugin. It needs an executor to bridge between the middleware's query strings and your GraphQL engine:
|
|
8
27
|
|
|
9
28
|
```typescript
|
|
10
29
|
import { createServer } from 'postgraphile/grafserv/h3/v1'
|
|
11
|
-
import { grafserv } from 'postgraphile/grafserv'
|
|
12
30
|
import { PostGraphileAmberPreset } from 'postgraphile/presets/amber'
|
|
13
31
|
import { makePgService } from 'postgraphile/adaptors/pg'
|
|
32
|
+
import { graphql } from 'graphql'
|
|
14
33
|
import {
|
|
15
34
|
createStonecropPlugin,
|
|
16
35
|
loadDoctypes,
|
|
17
36
|
registerBuiltinHandlers,
|
|
18
|
-
registerHandler,
|
|
19
37
|
} from '@stonecrop/graphql-middleware'
|
|
20
38
|
|
|
21
|
-
//
|
|
39
|
+
// Scan doctype JSON files and register them with the middleware
|
|
22
40
|
loadDoctypes('./doctypes')
|
|
23
41
|
|
|
24
|
-
// Register action handlers
|
|
42
|
+
// Register built-in action handlers (submit, approve, etc.)
|
|
25
43
|
registerBuiltinHandlers()
|
|
26
|
-
registerHandler('submitOrder', async (args, ctx) => {
|
|
27
|
-
const [orderId] = args as [string]
|
|
28
|
-
// Custom business logic
|
|
29
|
-
return { submitted: true }
|
|
30
|
-
})
|
|
31
44
|
|
|
32
|
-
//
|
|
45
|
+
// Executor bridges the middleware's query strings and your GraphQL engine
|
|
33
46
|
const executor = {
|
|
34
47
|
async query(query: string, variables?: Record<string, unknown>) {
|
|
35
|
-
|
|
36
|
-
return graphqlExecute(schema, query, variables)
|
|
48
|
+
return graphql({ schema, source: query, variableValues: variables })
|
|
37
49
|
},
|
|
38
50
|
async mutate(mutation: string, variables?: Record<string, unknown>) {
|
|
39
|
-
return
|
|
51
|
+
return graphql({ schema, source: mutation, variableValues: variables })
|
|
40
52
|
},
|
|
41
53
|
}
|
|
42
54
|
|
|
55
|
+
// PostGraphile configuration
|
|
43
56
|
const preset: GraphileConfig.Preset = {
|
|
44
|
-
extends: [PostGraphileAmberPreset],
|
|
45
|
-
plugins: [createStonecropPlugin({ executor })],
|
|
46
|
-
pgServices: [
|
|
47
|
-
makePgService({
|
|
48
|
-
connectionString: process.env.DATABASE_URL,
|
|
49
|
-
}),
|
|
50
|
-
],
|
|
57
|
+
extends: [PostGraphileAmberPreset], // Base preset with PostgreSQL integration
|
|
58
|
+
plugins: [createStonecropPlugin({ executor })], // Stonecrop GraphQL resolvers
|
|
59
|
+
pgServices: [makePgService({ connectionString: process.env.DATABASE_URL })], // Database connection
|
|
51
60
|
}
|
|
52
61
|
```
|
|
53
62
|
|
|
54
|
-
|
|
63
|
+
The middleware builds PostGraphile-formatted query strings (e.g., `SalesOrderById`, `SalesOrderItemsBySalesOrderId`). The executor runs them — it doesn't control what queries are constructed.
|
|
64
|
+
|
|
65
|
+
## Doctype Schemas
|
|
66
|
+
|
|
67
|
+
The middleware loads doctype definitions from JSON files. Each file defines a doctype's structure:
|
|
55
68
|
|
|
56
69
|
```json
|
|
57
70
|
{
|
|
@@ -59,9 +72,7 @@ const preset: GraphileConfig.Preset = {
|
|
|
59
72
|
"tableName": "sales_orders",
|
|
60
73
|
"fields": [
|
|
61
74
|
{ "fieldname": "id", "fieldtype": "UUID" },
|
|
62
|
-
{ "fieldname": "
|
|
63
|
-
{ "fieldname": "status", "fieldtype": "Select" },
|
|
64
|
-
{ "fieldname": "total", "fieldtype": "Currency" }
|
|
75
|
+
{ "fieldname": "status", "fieldtype": "Select" }
|
|
65
76
|
],
|
|
66
77
|
"links": {
|
|
67
78
|
"items": {
|
|
@@ -71,54 +82,39 @@ const preset: GraphileConfig.Preset = {
|
|
|
71
82
|
}
|
|
72
83
|
},
|
|
73
84
|
"workflow": {
|
|
74
|
-
"states": ["Draft", "Submitted"
|
|
85
|
+
"states": ["Draft", "Submitted"],
|
|
75
86
|
"actions": {
|
|
76
|
-
"submit": {
|
|
77
|
-
"label": "Submit",
|
|
78
|
-
"handler": "submitOrder",
|
|
79
|
-
"requiredFields": ["customer"],
|
|
80
|
-
"allowedStates": ["Draft"]
|
|
81
|
-
}
|
|
87
|
+
"submit": { "label": "Submit", "handler": "submitOrder" }
|
|
82
88
|
}
|
|
83
89
|
}
|
|
84
90
|
}
|
|
85
91
|
```
|
|
86
92
|
|
|
87
|
-
|
|
93
|
+
The `links` object declares relationships used when `includeNested` is requested. The `workflow` object enables action dispatch.
|
|
88
94
|
|
|
89
|
-
|
|
95
|
+
## Actions
|
|
90
96
|
|
|
91
|
-
|
|
92
|
-
import { StonecropClient } from '@stonecrop/graphql-client'
|
|
97
|
+
Actions are custom logic triggered by the client. Register handlers with `registerHandler`:
|
|
93
98
|
|
|
94
|
-
|
|
95
|
-
|
|
99
|
+
```typescript
|
|
100
|
+
registerHandler('submitOrder', async (args, ctx) => {
|
|
101
|
+
const [orderId] = args as [string]
|
|
102
|
+
return { submitted: true }
|
|
96
103
|
})
|
|
104
|
+
```
|
|
97
105
|
|
|
98
|
-
|
|
99
|
-
const meta = await client.getMeta({ doctype: 'SalesOrder' })
|
|
100
|
-
|
|
101
|
-
// Fetch records with optional filtering
|
|
102
|
-
const orders = await client.getRecords(
|
|
103
|
-
{ name: 'SalesOrder' },
|
|
104
|
-
{
|
|
105
|
-
limit: 10,
|
|
106
|
-
orderBy: 'createdAt',
|
|
107
|
-
}
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
// Fetch single record (flat)
|
|
111
|
-
const order = await client.getRecord({ name: 'SalesOrder' }, 'uuid-here')
|
|
106
|
+
The `args` are passed from the client, and `ctx` provides the doctype metadata and GraphQL executor.
|
|
112
107
|
|
|
113
|
-
|
|
114
|
-
const recipe = await client.getRecord({ name: 'Recipe' }, 'r1', {
|
|
115
|
-
includeNested: true,
|
|
116
|
-
})
|
|
108
|
+
## API
|
|
117
109
|
|
|
118
|
-
|
|
119
|
-
const result = await client.runAction({ name: 'SalesOrder' }, 'submit', ['uuid-here'])
|
|
120
|
-
```
|
|
110
|
+
The middleware exposes these GraphQL operations:
|
|
121
111
|
|
|
122
|
-
|
|
112
|
+
| Operation | Description |
|
|
113
|
+
|-----------|-------------|
|
|
114
|
+
| `stonecropRecord(doctype, id, options?)` | Fetch a single record, optionally with nested links |
|
|
115
|
+
| `stonecropRecords(doctype, filters?, orderBy?, limit?, offset?)` | Fetch multiple records |
|
|
116
|
+
| `stonecropMeta(doctype)` | Fetch doctype metadata |
|
|
117
|
+
| `stonecropAllMeta` | Fetch all doctype metadata |
|
|
118
|
+
| `stonecropAction(doctype, action, args?)` | Execute a doctype action |
|
|
123
119
|
|
|
124
|
-
For
|
|
120
|
+
For type signatures and detailed parameters, see [API Reference](./api.md).
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DoctypeMeta } from '@stonecrop/schema';
|
|
2
|
+
import { DocumentNode } from 'graphql';
|
|
2
3
|
import { GraphileConfig as GraphileConfig_2 } from 'postgraphile/graphile-build';
|
|
3
4
|
import { ValidationError } from '@stonecrop/schema';
|
|
4
5
|
|
|
@@ -21,6 +22,46 @@ export declare interface ActionContext {
|
|
|
21
22
|
*/
|
|
22
23
|
export declare type ActionHandler = (args: unknown[], context: ActionContext) => Promise<unknown>;
|
|
23
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Build a GraphQL connection query to fetch a list of records.
|
|
27
|
+
* Only declares variables ($limit, $offset, $orderBy) that are actually used in the query,
|
|
28
|
+
* avoiding GraphQL spec §5.8.3 violations from unused variable declarations.
|
|
29
|
+
* Excludes Link and Doctype relation fields from the selection set.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare function buildListQuery(meta: DoctypeMeta, args: BuildListQueryArgs, connectionFieldName: (t: string) => string, orderByTypeName: (t: string) => string): string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Arguments for buildListQuery
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export declare interface BuildListQueryArgs {
|
|
39
|
+
/** Maximum number of records to return */
|
|
40
|
+
limit?: number;
|
|
41
|
+
/** Number of records to skip */
|
|
42
|
+
offset?: number;
|
|
43
|
+
/** OrderBy specification */
|
|
44
|
+
orderBy?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Build a GraphQL query to fetch a single record by ID.
|
|
49
|
+
* When includeNested is set, recursively includes descendant link sub-selections.
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export 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;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Options for buildRecordQuery nested selection building
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export declare interface BuildRecordQueryOptions {
|
|
59
|
+
/** Include nested/related records */
|
|
60
|
+
includeNested?: boolean | string[];
|
|
61
|
+
/** Maximum nesting depth */
|
|
62
|
+
maxDepth?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
24
65
|
/**
|
|
25
66
|
* Built-in handlers available for registration
|
|
26
67
|
* @public
|
|
@@ -83,6 +124,20 @@ export declare function defaultRecordArgType(_tableName: string): string;
|
|
|
83
124
|
*/
|
|
84
125
|
export declare function defaultRecordFieldName(tableName: string): string;
|
|
85
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Default reverse connection name: derives PostGraphile's connection field convention.
|
|
129
|
+
* PostGraphile convention: `{targetPlural}By{FkColumnPascal}Id`
|
|
130
|
+
* - When backlink is defined: FK column is derived from the backlink field name
|
|
131
|
+
* - When backlink is absent: FK column is derived from the parent doctype's table name
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
134
|
+
export declare function defaultReverseConnectionName(params: {
|
|
135
|
+
doctype: string;
|
|
136
|
+
linkName: string;
|
|
137
|
+
backlink?: string;
|
|
138
|
+
target: string;
|
|
139
|
+
}): string;
|
|
140
|
+
|
|
86
141
|
export { DoctypeMeta }
|
|
87
142
|
|
|
88
143
|
/**
|
|
@@ -101,6 +156,45 @@ export declare class DoctypeValidationError extends Error {
|
|
|
101
156
|
errors: ValidationError[]);
|
|
102
157
|
}
|
|
103
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Extract the list of nodes from a PostGraphile connection query result.
|
|
161
|
+
* Returns an empty array if the connection field is absent.
|
|
162
|
+
* @public
|
|
163
|
+
*/
|
|
164
|
+
export declare function extractListResult(params: ExtractListResultParams): unknown[];
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Parameters for extractListResult
|
|
168
|
+
* @public
|
|
169
|
+
*/
|
|
170
|
+
export declare interface ExtractListResultParams {
|
|
171
|
+
/** The raw query result */
|
|
172
|
+
result: unknown;
|
|
173
|
+
/** Doctype metadata */
|
|
174
|
+
meta: DoctypeMeta;
|
|
175
|
+
/** Function to derive the connection field name from a table name */
|
|
176
|
+
connectionFieldName: (tableName: string) => string;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Extract a single record from a PostGraphile query result using the record field name.
|
|
181
|
+
* @public
|
|
182
|
+
*/
|
|
183
|
+
export declare function extractSingleResult(params: ExtractSingleResultParams): unknown;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Parameters for extractSingleResult
|
|
187
|
+
* @public
|
|
188
|
+
*/
|
|
189
|
+
export declare interface ExtractSingleResultParams {
|
|
190
|
+
/** The raw query result */
|
|
191
|
+
result: unknown;
|
|
192
|
+
/** Doctype metadata */
|
|
193
|
+
meta: DoctypeMeta;
|
|
194
|
+
/** Function to derive the record field name from a table name */
|
|
195
|
+
recordFieldName: (tableName: string) => string;
|
|
196
|
+
}
|
|
197
|
+
|
|
104
198
|
/**
|
|
105
199
|
* Get all loaded doctypes
|
|
106
200
|
* @public
|
|
@@ -173,6 +267,27 @@ export declare interface LoadDoctypesOptions {
|
|
|
173
267
|
onError?: (file: string, errors: ValidationError[]) => void;
|
|
174
268
|
}
|
|
175
269
|
|
|
270
|
+
/**
|
|
271
|
+
* Merge nested connection results into flat arrays.
|
|
272
|
+
* For `noneOrMany`/`atLeastOne` links, the query returns `{ nodes: [...] }`.
|
|
273
|
+
* This flattens them to just `[]` for easier consumption.
|
|
274
|
+
* @public
|
|
275
|
+
*/
|
|
276
|
+
export declare function mergeNestedResults(params: MergeNestedResultsParams): Record<string, unknown>;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Parameters for mergeNestedResults
|
|
280
|
+
* @public
|
|
281
|
+
*/
|
|
282
|
+
export declare interface MergeNestedResultsParams {
|
|
283
|
+
/** The record object with nested connection data */
|
|
284
|
+
record: Record<string, unknown>;
|
|
285
|
+
/** Doctype metadata */
|
|
286
|
+
meta: DoctypeMeta;
|
|
287
|
+
/** Lookup function to get doctype metadata by slug */
|
|
288
|
+
getMeta: (slug: string) => DoctypeMeta | undefined;
|
|
289
|
+
}
|
|
290
|
+
|
|
176
291
|
/**
|
|
177
292
|
* Filter fields to only those directly queryable as scalars, excluding Link and Doctype
|
|
178
293
|
* relation fields that require GraphQL sub-selections.
|
|
@@ -201,6 +316,21 @@ export declare function registerHandler(name: string, handler: ActionHandler): v
|
|
|
201
316
|
*/
|
|
202
317
|
export declare const RELATION_FIELDTYPES: Set<string>;
|
|
203
318
|
|
|
319
|
+
/**
|
|
320
|
+
* Parameters for reverse connection name inflection
|
|
321
|
+
* @public
|
|
322
|
+
*/
|
|
323
|
+
export declare interface ReverseConnectionParams {
|
|
324
|
+
/** Parent doctype slug */
|
|
325
|
+
doctype: string;
|
|
326
|
+
/** Link key on the parent */
|
|
327
|
+
linkName: string;
|
|
328
|
+
/** Link field on the target that points back to the parent (optional) */
|
|
329
|
+
backlink?: string;
|
|
330
|
+
/** Target doctype slug */
|
|
331
|
+
target: string;
|
|
332
|
+
}
|
|
333
|
+
|
|
204
334
|
/**
|
|
205
335
|
* Inflection callbacks for mapping table names to GraphQL query field names.
|
|
206
336
|
* Override these when using a non-Amber inflection preset (e.g., V4, SimplifyInflection).
|
|
@@ -238,6 +368,23 @@ export declare interface StonecropInflectionConfig {
|
|
|
238
368
|
* @example Integer PK: "sales_orders" → "Int!"
|
|
239
369
|
*/
|
|
240
370
|
recordArgType?: (tableName: string) => string;
|
|
371
|
+
/**
|
|
372
|
+
* Derive the GraphQL connection field name for a reverse-FK link.
|
|
373
|
+
* PostGraphile convention: `{targetPlural}By{FkColumnPascal}Id`
|
|
374
|
+
* - When backlink is provided: FK column is derived from the backlink field
|
|
375
|
+
* - When backlink is absent: FK column is derived from the parent doctype
|
|
376
|
+
* @example Recipe → RecipeTasksByRecipeId
|
|
377
|
+
*/
|
|
378
|
+
reverseConnectionName?: (params: {
|
|
379
|
+
/** Parent doctype slug */
|
|
380
|
+
doctype: string;
|
|
381
|
+
/** Link key on the parent */
|
|
382
|
+
linkName: string;
|
|
383
|
+
/** Link field on the target that points back to the parent (optional) */
|
|
384
|
+
backlink?: string;
|
|
385
|
+
/** Target doctype slug */
|
|
386
|
+
target: string;
|
|
387
|
+
}) => string;
|
|
241
388
|
}
|
|
242
389
|
|
|
243
390
|
/**
|
|
@@ -254,6 +401,24 @@ export declare interface StonecropPluginOptions {
|
|
|
254
401
|
inflection?: StonecropInflectionConfig;
|
|
255
402
|
}
|
|
256
403
|
|
|
404
|
+
/**
|
|
405
|
+
* Options for stonecropRecord queries
|
|
406
|
+
* @public
|
|
407
|
+
*/
|
|
408
|
+
export declare interface StonecropRecordOptions {
|
|
409
|
+
/** Include nested/related records */
|
|
410
|
+
includeNested?: boolean | string[];
|
|
411
|
+
/** Maximum nesting depth */
|
|
412
|
+
maxDepth?: number;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* GraphQL type definitions for Stonecrop's middleware API.
|
|
417
|
+
* Includes stonecropMeta, stonecropRecord, stonecropRecords, stonecropAction, and related types.
|
|
418
|
+
* @public
|
|
419
|
+
*/
|
|
420
|
+
export declare const typeDefs: DocumentNode;
|
|
421
|
+
|
|
257
422
|
/**
|
|
258
423
|
* Validate cross-doctype references (Link fields, inherits, etc.)
|
|
259
424
|
* Call after all doctypes are loaded.
|