@stonecrop/graphql-middleware 0.17.0 → 0.18.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/dist/graphql-middleware.d.ts +13 -5
- package/dist/index.js +446 -338
- package/dist/index.js.map +1 -1
- package/dist/src/fields.d.ts +37 -0
- package/dist/src/fields.d.ts.map +1 -0
- package/dist/src/fields.js +48 -0
- package/dist/src/plugin/postgraphile.d.ts +13 -5
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +229 -40
- package/dist/src/registry/doctypes.d.ts +12 -1
- package/dist/src/registry/doctypes.d.ts.map +1 -1
- package/dist/src/registry/doctypes.js +65 -2
- package/dist/src/typeDefs.d.ts.map +1 -1
- package/dist/src/typeDefs.js +15 -0
- package/package.json +2 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { parseDoctype, validateDoctype } from '@stonecrop/schema';
|
|
4
|
+
import { columnBackedFields, flattenFields } from '../fields';
|
|
4
5
|
const doctypeRegistry = new Map();
|
|
5
6
|
/**
|
|
6
7
|
* Error thrown when a doctype definition fails validation
|
|
@@ -131,7 +132,8 @@ export function clearRegistry() {
|
|
|
131
132
|
doctypeRegistry.clear();
|
|
132
133
|
}
|
|
133
134
|
/**
|
|
134
|
-
* Validate
|
|
135
|
+
* Validate doctype references — link targets, `inherits`, and each link's binding — against what
|
|
136
|
+
* is loaded.
|
|
135
137
|
*
|
|
136
138
|
* Resolution goes through `getMeta`, never through the registry Map directly. The Map is keyed by
|
|
137
139
|
* doctype **name** while a link target is a **slug** (`DoctypeMeta.links[].target` is documented as
|
|
@@ -144,6 +146,16 @@ export function clearRegistry() {
|
|
|
144
146
|
* only echo it onto the wire) — one rule is cheaper than two, and it cannot be wrong in the
|
|
145
147
|
* permissive direction.
|
|
146
148
|
*
|
|
149
|
+
* The **binding** checks are the same defect class as an unresolvable target, one step further in:
|
|
150
|
+
* a link whose target resolves can still name no way to reach the rows, and the reader's answer for
|
|
151
|
+
* that is `continue` or a `null` — indistinguishable on the wire from a relation that is genuinely
|
|
152
|
+
* empty. Both halves of a declaration are `.optional()` in the schema because which one is required
|
|
153
|
+
* depends on cardinality, which Zod cannot express here; this is where that dependency is enforced.
|
|
154
|
+
*
|
|
155
|
+
* Only what the runtime actually consults is required. A `backlink` naming a column the target
|
|
156
|
+
* table does not have already fails loudly — Postgres rejects the SELECT — so it is left alone;
|
|
157
|
+
* the check exists for the silent cases, not to restate the database's own errors.
|
|
158
|
+
*
|
|
147
159
|
* Returns rather than throws so the caller decides; `assertReferencesResolve` in the plugin is that
|
|
148
160
|
* caller. Kept out of the package's public exports: it reads a module-level registry, so it is only
|
|
149
161
|
* meaningful after this module's own loaders have run.
|
|
@@ -160,8 +172,9 @@ export function validateReferences() {
|
|
|
160
172
|
message: `References unknown doctype: ${doctype.inherits}`,
|
|
161
173
|
});
|
|
162
174
|
}
|
|
175
|
+
const fields = flattenFields(doctype.fields);
|
|
163
176
|
// Check link field targets — `doctype` is both the link marker and its target.
|
|
164
|
-
for (const field of
|
|
177
|
+
for (const field of fields) {
|
|
165
178
|
if (field.kind !== 'field')
|
|
166
179
|
continue;
|
|
167
180
|
const target = field.doctype;
|
|
@@ -174,6 +187,11 @@ export function validateReferences() {
|
|
|
174
187
|
}
|
|
175
188
|
// Check link-declaration targets (component-primary links live in the `links` map)
|
|
176
189
|
if (doctype.links) {
|
|
190
|
+
const declared = new Set(fields.map(f => f.fieldname));
|
|
191
|
+
// A foreign key is read from a column, so a link can only bind to a field that has one.
|
|
192
|
+
// The rule itself lives in one place — see `columnBackedFields` — because the read path,
|
|
193
|
+
// the write path and this check all have to answer it the same way.
|
|
194
|
+
const columnBacked = new Set(columnBackedFields(doctype.fields).map(f => f.fieldname));
|
|
177
195
|
for (const [key, link] of Object.entries(doctype.links)) {
|
|
178
196
|
if (getMeta(link.target) === undefined) {
|
|
179
197
|
errors.push({
|
|
@@ -181,6 +199,51 @@ export function validateReferences() {
|
|
|
181
199
|
message: `Link references unknown doctype: ${link.target}`,
|
|
182
200
|
});
|
|
183
201
|
}
|
|
202
|
+
// A custom fetch strategy hands the whole read to its handler, which is given the
|
|
203
|
+
// row and the declaration and finds the target however it likes. The runtime returns
|
|
204
|
+
// before consulting either binding below, so requiring one would refuse a link that
|
|
205
|
+
// works.
|
|
206
|
+
if (link.fetch?.method === 'custom')
|
|
207
|
+
continue;
|
|
208
|
+
// The two cardinalities bind through different halves of the declaration, so the
|
|
209
|
+
// check has to split the same way the reader does.
|
|
210
|
+
if (link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne') {
|
|
211
|
+
// Many-side: the target rows are found by a column on the *target* table, so
|
|
212
|
+
// this doctype needs no field of its own — and the in-repo hosts disagree on
|
|
213
|
+
// whether one exists, which is why only `backlink` can be required here.
|
|
214
|
+
if (!link.backlink) {
|
|
215
|
+
errors.push({
|
|
216
|
+
path: [doctype.name, 'links', key, 'backlink'],
|
|
217
|
+
message: `Link "${key}" is ${link.cardinality} but names no \`backlink\`, so the runtime cannot ` +
|
|
218
|
+
`find the target rows and drops the link with no error. Name the field on "${link.target}" ` +
|
|
219
|
+
`that points back to this doctype.`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
// One-side: the foreign key is a column on *this* table, read as
|
|
225
|
+
// `link.fieldname ?? key`. A name matching no declared field is not selected, so the
|
|
226
|
+
// link resolves to null forever and reads as "this record has no such relation".
|
|
227
|
+
const fieldname = link.fieldname ?? key;
|
|
228
|
+
if (!columnBacked.has(fieldname)) {
|
|
229
|
+
errors.push({
|
|
230
|
+
// Point at whichever half actually carries the binding: the `fieldname`
|
|
231
|
+
// property when it is set, otherwise the key itself. Naming `target` here
|
|
232
|
+
// would send the reader to the one part that resolved correctly.
|
|
233
|
+
path: link.fieldname ? [doctype.name, 'links', key, 'fieldname'] : [doctype.name, 'links', key],
|
|
234
|
+
// Two different repairs, so two different diagnoses: a name nothing declares
|
|
235
|
+
// is a typo, while a name that resolves to a column-less field is a binding
|
|
236
|
+
// pointed at the wrong one of several real fields.
|
|
237
|
+
message: declared.has(fieldname)
|
|
238
|
+
? `Link "${key}" binds to field "${fieldname}", which has no database column of its own — ` +
|
|
239
|
+
`it is computed, or a container rather than a value — so there is no foreign key to read ` +
|
|
240
|
+
`and the record read fails on the missing column. Bind the link to the field holding the ` +
|
|
241
|
+
`foreign key.`
|
|
242
|
+
: `Link "${key}" binds to field "${fieldname}", which this doctype does not declare, so it ` +
|
|
243
|
+
`resolves to null with no error. Declare that field, or set \`fieldname\` to the field ` +
|
|
244
|
+
`holding the foreign key. (A link's key is its fieldname unless \`fieldname\` overrides it.)`,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
184
247
|
}
|
|
185
248
|
}
|
|
186
249
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeDefs.d.ts","sourceRoot":"","sources":["../../src/typeDefs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"typeDefs.d.ts","sourceRoot":"","sources":["../../src/typeDefs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,gCA6GpB,CAAA"}
|
package/dist/src/typeDefs.js
CHANGED
|
@@ -57,6 +57,13 @@ export const typeDefs = gql `
|
|
|
57
57
|
data: JSON
|
|
58
58
|
doctype: String!
|
|
59
59
|
unknownLinks: [String!]
|
|
60
|
+
"""
|
|
61
|
+
Fields whose linked rows were cut short by a row cap, so data holds a prefix of the
|
|
62
|
+
relation rather than all of it. Null when nothing was truncated. A link cannot be paged,
|
|
63
|
+
so a client that sees a name here must not write that relation back — the rows it never
|
|
64
|
+
received would be deleted.
|
|
65
|
+
"""
|
|
66
|
+
truncatedLinks: [String!]
|
|
60
67
|
}
|
|
61
68
|
|
|
62
69
|
type StonecropRecordsResult {
|
|
@@ -78,6 +85,14 @@ export const typeDefs = gql `
|
|
|
78
85
|
success: Boolean!
|
|
79
86
|
data: JSON
|
|
80
87
|
error: String
|
|
88
|
+
"""
|
|
89
|
+
Keys the write discarded because the doctype declares no column for them, or because the
|
|
90
|
+
value was a nested relation rather than a column value. Null when everything sent was
|
|
91
|
+
stored. The action still succeeded — this is what it did not keep, and a client that
|
|
92
|
+
reports an unqualified success without checking it tells the user data was saved that
|
|
93
|
+
was not.
|
|
94
|
+
"""
|
|
95
|
+
droppedFields: [String!]
|
|
81
96
|
}
|
|
82
97
|
|
|
83
98
|
extend type Query {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/graphql-middleware",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "GraphQL middleware for PostGraphile and Stonecrop schema",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"graphql": "^16.14.0",
|
|
24
24
|
"pluralize": "^8.0.0",
|
|
25
25
|
"postgraphile": "^5.0.3",
|
|
26
|
-
"@stonecrop/schema": "0.
|
|
26
|
+
"@stonecrop/schema": "0.18.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@electric-sql/pglite": "^0.4.5",
|