@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
package/README.md
CHANGED
|
@@ -1,84 +1,109 @@
|
|
|
1
1
|
# @stonecrop/graphql-middleware
|
|
2
2
|
|
|
3
|
-
GraphQL backend for the Stonecrop framework. Reads doctype schemas and exposes them as
|
|
4
|
-
|
|
5
|
-
Currently integrated with PostGraphile (to be extracted into a swappable adapter in the future).
|
|
3
|
+
GraphQL backend for the Stonecrop framework. Reads doctype schemas and exposes them as PostGraphile plan-step resolvers that handle record fetching, fetch-strategy dispatch, and action execution.
|
|
6
4
|
|
|
7
5
|
## What it does
|
|
8
6
|
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
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.
|
|
7
|
+
- **Record fetching** — Resolves `stonecropRecord` and `stonecropRecords` via direct parameterised SQL, batched via `loadOneWithPgClient` from `@dataplan/pg`
|
|
8
|
+
- **Fetch-strategy dispatch** — For `stonecropRecord`, iterates `meta.links` and applies `sync` / `lazy` / `custom` strategies; sync links issue additional SQL and merge results into the record; lazy links are absent from the response
|
|
9
|
+
- **Action dispatch** — Routes doctype actions to registered handlers via `sideEffectWithPgClient`
|
|
10
|
+
- **Preset** — `createStonecropPreset()` wraps `PostGraphileAmberPreset` so user apps never import from PostGraphile directly
|
|
23
11
|
|
|
24
12
|
## Setup
|
|
25
13
|
|
|
26
|
-
The middleware is a PostGraphile plugin. It needs an executor to bridge between the middleware's query strings and your GraphQL engine:
|
|
27
|
-
|
|
28
14
|
```typescript
|
|
29
|
-
import { createServer } from 'postgraphile/grafserv/h3/v1'
|
|
30
|
-
import { PostGraphileAmberPreset } from 'postgraphile/presets/amber'
|
|
31
|
-
import { makePgService } from 'postgraphile/adaptors/pg'
|
|
32
|
-
import { graphql } from 'graphql'
|
|
33
15
|
import {
|
|
16
|
+
createStonecropPreset,
|
|
34
17
|
createStonecropPlugin,
|
|
18
|
+
makePgService,
|
|
35
19
|
loadDoctypes,
|
|
36
20
|
registerBuiltinHandlers,
|
|
37
21
|
} from '@stonecrop/graphql-middleware'
|
|
38
22
|
|
|
39
|
-
// Scan doctype JSON files and register them with the middleware
|
|
40
23
|
loadDoctypes('./doctypes')
|
|
41
|
-
|
|
42
|
-
// Register built-in action handlers (submit, approve, etc.)
|
|
43
24
|
registerBuiltinHandlers()
|
|
44
25
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
},
|
|
50
|
-
async mutate(mutation: string, variables?: Record<string, unknown>) {
|
|
51
|
-
return graphql({ schema, source: mutation, variableValues: variables })
|
|
52
|
-
},
|
|
26
|
+
const preset: GraphileConfig.Preset = {
|
|
27
|
+
extends: [createStonecropPreset()],
|
|
28
|
+
plugins: [createStonecropPlugin()],
|
|
29
|
+
pgServices: [makePgService({ connectionString: process.env.DATABASE_URL })],
|
|
53
30
|
}
|
|
31
|
+
```
|
|
54
32
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
33
|
+
`createStonecropPlugin()` discovers the `PgExecutor` automatically from `pgServices` — no executor argument is needed. An optional `StonecropPluginOptions` object accepts:
|
|
34
|
+
|
|
35
|
+
| Option | Default | Description |
|
|
36
|
+
|--------|---------|-------------|
|
|
37
|
+
| `pkField` | `'id'` | Primary key column name for record lookups |
|
|
38
|
+
|
|
39
|
+
## Debugging
|
|
40
|
+
|
|
41
|
+
### Nuxt module shortcut
|
|
42
|
+
|
|
43
|
+
When using `@stonecrop/nuxt-grafserv`, set `debug: true` in `nuxt.config.ts` to enable all development aids at once:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
export default defineNuxtConfig({
|
|
47
|
+
grafserv: {
|
|
48
|
+
type: 'postgraphile',
|
|
49
|
+
debug: true,
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`debug: true` automatically:
|
|
55
|
+
- Enables the **Ruru Explain** tab (`grafast.explain`) to inspect Grafast plan steps and generated SQL
|
|
56
|
+
- Injects `createDebugPlugin()` to log Stonecrop resolver plan construction
|
|
57
|
+
- Configures `grafserv.maskError` using PostGraphile's recommended pattern: logs every error server-side, returns `GraphQLError` and safe errors directly, and masks unknown errors with a SHA-1 hash
|
|
58
|
+
|
|
59
|
+
**Never enable `debug` in production** — it exposes query internals and detailed error messages to clients.
|
|
60
|
+
|
|
61
|
+
### Custom preset
|
|
62
|
+
|
|
63
|
+
If you manage your own preset file, import the debug plugin directly:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { createStonecropPlugin, createDebugPlugin } from '@stonecrop/graphql-middleware'
|
|
67
|
+
|
|
68
|
+
export default {
|
|
69
|
+
plugins: [createStonecropPlugin(), createDebugPlugin()],
|
|
70
|
+
grafast: { explain: true },
|
|
60
71
|
}
|
|
61
72
|
```
|
|
62
73
|
|
|
63
|
-
|
|
74
|
+
### Environment variables
|
|
75
|
+
|
|
76
|
+
PostGraphile's native `DEBUG` variables still work alongside Stonecrop's debug plugin:
|
|
77
|
+
|
|
78
|
+
| Variable | What it shows |
|
|
79
|
+
|----------|---------------|
|
|
80
|
+
| `DEBUG="@dataplan/pg:PgExecutor"` | SQL queries executed by plan steps |
|
|
81
|
+
| `DEBUG="@dataplan/pg:PgExecutor:explain"` | SQL plus `EXPLAIN` output |
|
|
82
|
+
| `DEBUG="graphile-build:warn"` | Warnings during schema construction |
|
|
83
|
+
| `DEBUG="graphile-build:SchemaBuilder"` | Hook execution order during schema build |
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
DEBUG="@dataplan/pg:PgExecutor:explain,graphile-build:warn" node server.js
|
|
87
|
+
```
|
|
64
88
|
|
|
65
89
|
## Doctype Schemas
|
|
66
90
|
|
|
67
|
-
|
|
91
|
+
Each doctype JSON file defines structure, relationships, and workflow:
|
|
68
92
|
|
|
69
93
|
```json
|
|
70
94
|
{
|
|
71
95
|
"name": "SalesOrder",
|
|
72
96
|
"tableName": "sales_orders",
|
|
73
97
|
"fields": [
|
|
74
|
-
{ "fieldname": "id", "fieldtype": "
|
|
98
|
+
{ "fieldname": "id", "fieldtype": "Data" },
|
|
75
99
|
{ "fieldname": "status", "fieldtype": "Select" }
|
|
76
100
|
],
|
|
77
101
|
"links": {
|
|
78
102
|
"items": {
|
|
79
103
|
"target": "sales-order-item",
|
|
80
104
|
"cardinality": "noneOrMany",
|
|
81
|
-
"backlink": "
|
|
105
|
+
"backlink": "sales_order_id",
|
|
106
|
+
"fetch": { "method": "sync", "limit": 100 }
|
|
82
107
|
}
|
|
83
108
|
},
|
|
84
109
|
"workflow": {
|
|
@@ -90,49 +115,64 @@ The middleware loads doctype definitions from JSON files. Each file defines a do
|
|
|
90
115
|
}
|
|
91
116
|
```
|
|
92
117
|
|
|
93
|
-
|
|
118
|
+
### Fetch strategies
|
|
119
|
+
|
|
120
|
+
Each entry in `links` can declare a `fetch` strategy:
|
|
121
|
+
|
|
122
|
+
| Strategy | Behaviour |
|
|
123
|
+
|----------|-----------|
|
|
124
|
+
| `{ "method": "sync", "limit": N }` | Linked records are fetched in the same resolver call and merged into `data` |
|
|
125
|
+
| `{ "method": "lazy" }` | Link is absent from the response; client retrieves it via `stonecropRecords` with `filters` |
|
|
126
|
+
| `{ "method": "custom", "handler": "myHandler" }` | Calls a registered fetch handler (see below) |
|
|
127
|
+
|
|
128
|
+
When `fetch` is omitted, `noneOrMany`/`atLeastOne` links default to `sync` (limit 50) and `atMostOne`/`one` links default to `lazy`.
|
|
94
129
|
|
|
95
130
|
### Display-only fields
|
|
96
131
|
|
|
97
|
-
Fields with `fieldtype: "Display"`
|
|
132
|
+
Fields with `fieldtype: "Display"` have no backing database column and are excluded from all SQL queries:
|
|
98
133
|
|
|
99
134
|
```json
|
|
100
135
|
{ "fieldname": "planner", "fieldtype": "Display", "component": "Planner", "label": "Resource Planner" }
|
|
101
136
|
```
|
|
102
137
|
|
|
103
|
-
All other non-`Link` fieldtypes are assumed to map to a scalar DB column and are included in the query.
|
|
104
|
-
|
|
105
|
-
## Architecture
|
|
106
|
-
|
|
107
|
-
### Why the middleware builds queries
|
|
108
|
-
|
|
109
|
-
The client (`@stonecrop/graphql-client`) is intentionally transport-only — it sends requests and receives flat data. It never constructs GraphQL queries. All query generation lives in the middleware, which is the only layer aware of PostGraphile naming conventions (inflection).
|
|
110
|
-
|
|
111
|
-
This boundary exists because PostGraphile's schema naming is configurable. A single application might use `ById` for UUID primary keys, `ByRowId` for `row_id` columns, or entirely custom conventions. If the client hardcoded any of these conventions, it would silently generate wrong queries for any non-default setup. By keeping query construction server-side, the middleware's `StonecropInflectionConfig` becomes the single source of truth.
|
|
112
|
-
|
|
113
138
|
## Actions
|
|
114
139
|
|
|
115
|
-
|
|
140
|
+
Register action handlers with `registerHandler`. Handlers receive the action arguments and an `ActionContext` that includes the active `PgClient`:
|
|
116
141
|
|
|
117
142
|
```typescript
|
|
143
|
+
import { registerHandler } from '@stonecrop/graphql-middleware'
|
|
144
|
+
|
|
118
145
|
registerHandler('submitOrder', async (args, ctx) => {
|
|
119
146
|
const [orderId] = args as [string]
|
|
147
|
+
// ctx.pgClient is available for additional database work
|
|
120
148
|
return { submitted: true }
|
|
121
149
|
})
|
|
122
150
|
```
|
|
123
151
|
|
|
124
|
-
|
|
152
|
+
## Custom fetch handlers
|
|
125
153
|
|
|
126
|
-
|
|
154
|
+
Register custom fetch handlers for links that use `{ "method": "custom" }`:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { registerFetchHandler } from '@stonecrop/graphql-middleware'
|
|
158
|
+
|
|
159
|
+
registerFetchHandler('loadLineItems', async (pgClient, parentRecord, link) => {
|
|
160
|
+
// Returns a single record or an array depending on link.cardinality
|
|
161
|
+
const { rows } = await pgClient.query(...)
|
|
162
|
+
return rows
|
|
163
|
+
})
|
|
164
|
+
```
|
|
127
165
|
|
|
128
|
-
|
|
166
|
+
## API
|
|
129
167
|
|
|
130
168
|
| Operation | Description |
|
|
131
169
|
|-----------|-------------|
|
|
132
|
-
| `stonecropRecord(doctype, id, options?)` | Fetch a single record
|
|
133
|
-
| `stonecropRecords(doctype, filters?, orderBy?, limit?, offset
|
|
170
|
+
| `stonecropRecord(doctype, id, options?)` | Fetch a single record; `options.includeNested` controls which links are resolved |
|
|
171
|
+
| `stonecropRecords(doctype, filters?, orderBy?, limit?, offset?)` | Fetch a list with optional filtering and ordering |
|
|
134
172
|
| `stonecropMeta(doctype)` | Fetch doctype metadata |
|
|
135
|
-
| `stonecropAllMeta` | Fetch all doctype metadata |
|
|
173
|
+
| `stonecropAllMeta` | Fetch all registered doctype metadata |
|
|
136
174
|
| `stonecropAction(doctype, action, args?)` | Execute a doctype action |
|
|
137
175
|
|
|
138
|
-
|
|
176
|
+
`stonecropRecords` accepts `orderBy` as `FIELD_ASC` or `FIELD_DESC` (e.g. `"status_ASC"`). The field name is validated against the doctype's known fields before interpolation.
|
|
177
|
+
|
|
178
|
+
For full type signatures see [API Reference](./api.md).
|