@coderich/autograph 0.13.53 → 0.13.54
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/package.json +1 -1
- package/src/data/Resolver.js +7 -3
- package/src/query/Query.js +5 -0
- package/src/query/QueryBuilder.js +1 -1
- package/src/schema/Schema.js +24 -7
package/package.json
CHANGED
package/src/data/Resolver.js
CHANGED
|
@@ -296,17 +296,21 @@ module.exports = class Resolver {
|
|
|
296
296
|
}
|
|
297
297
|
|
|
298
298
|
#createSystemEvent($query, thunk = () => {}) {
|
|
299
|
-
let
|
|
299
|
+
let tquery = $query.transform(false);
|
|
300
|
+
let query = tquery.toObject();
|
|
300
301
|
const type = query.isMutation ? 'Mutation' : 'Query';
|
|
301
302
|
let event = this.#createEvent(query);
|
|
302
303
|
|
|
303
304
|
return Emitter.emit(`pre${type}`, event).then(async (resultEarly) => {
|
|
304
305
|
if (resultEarly !== undefined) return resultEarly; // Nothing to validate/transform
|
|
305
306
|
// if (query.crud === 'update' && Util.isEqual({ added: {}, updated: {}, deleted: {} }, Util.changeset(query.doc, query.input))) return query.doc;
|
|
306
|
-
|
|
307
|
+
tquery = $query.transform(false);
|
|
307
308
|
query = tquery.toObject();
|
|
308
309
|
event = this.#createEvent(query);
|
|
309
|
-
if (['create', 'update'].includes(query.crud))
|
|
310
|
+
if (['create', 'update'].includes(query.crud)) {
|
|
311
|
+
tquery.validate();
|
|
312
|
+
await Promise.all([...query.input.$thunks, Emitter.emit('validate', event)]);
|
|
313
|
+
}
|
|
310
314
|
return thunk(tquery);
|
|
311
315
|
}).then((result) => {
|
|
312
316
|
event.result = result; // backwards compat
|
package/src/query/Query.js
CHANGED
|
@@ -63,6 +63,11 @@ module.exports = class Query {
|
|
|
63
63
|
return this;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
validate() {
|
|
67
|
+
const args = { query: this.#query, resolver: this.#resolver, context: this.#context };
|
|
68
|
+
this.#query.input = this.#model.transformers.validate.transform(this.#query.input, args);
|
|
69
|
+
}
|
|
70
|
+
|
|
66
71
|
/**
|
|
67
72
|
* Transform entire query for driver
|
|
68
73
|
*/
|
|
@@ -221,7 +221,7 @@ module.exports = class QueryBuilder {
|
|
|
221
221
|
const suffix = id || limit === 1 || (crud === 'create' && args.length < 2) ? 'One' : 'Many';
|
|
222
222
|
let input = suffix === 'One' ? args[0] : args;
|
|
223
223
|
if (input === undefined) input = {};
|
|
224
|
-
input.id = id;
|
|
224
|
+
if (id !== undefined) input.id = id;
|
|
225
225
|
this.#query.args.input = input;
|
|
226
226
|
return this.terminate(Object.assign(this.#query, {
|
|
227
227
|
op: `${crud}${suffix}`,
|
package/src/schema/Schema.js
CHANGED
|
@@ -19,6 +19,7 @@ const allowedKinds = modelKinds.concat(fieldKinds).concat(Kind.DOCUMENT, Kind.NO
|
|
|
19
19
|
const pipelines = ['validate', 'construct', 'restruct', 'instruct', 'normalize', 'serialize', 'deserialize'];
|
|
20
20
|
const createPipelines = ['validate', 'construct', 'instruct', 'normalize', 'serialize'];
|
|
21
21
|
const updatePipelines = ['validate', 'restruct', 'instruct', 'normalize', 'serialize'];
|
|
22
|
+
const validatePipelines = ['validate', 'instruct', 'normalize', 'serialize'];
|
|
22
23
|
const scalars = ['ID', 'String', 'Float', 'Int', 'Boolean'];
|
|
23
24
|
|
|
24
25
|
module.exports = class Schema {
|
|
@@ -147,6 +148,7 @@ module.exports = class Schema {
|
|
|
147
148
|
generator: this.#config.generators?.default,
|
|
148
149
|
pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
|
|
149
150
|
transformers: {
|
|
151
|
+
validate: new Transformer({ args: { schema: this.#schema, path: [] } }),
|
|
150
152
|
create: new Transformer({ args: { schema: this.#schema, path: [] } }),
|
|
151
153
|
update: new Transformer({ args: { schema: this.#schema, path: [] } }),
|
|
152
154
|
where: new Transformer({ args: { schema: this.#schema, path: [] } }),
|
|
@@ -355,6 +357,7 @@ module.exports = class Schema {
|
|
|
355
357
|
|
|
356
358
|
$model.transformers.create.config({
|
|
357
359
|
strictSchema: true,
|
|
360
|
+
keepUndefined: true,
|
|
358
361
|
shape: Object.values($model.fields).reduce((prev, curr) => {
|
|
359
362
|
const args = { model: $model, field: curr };
|
|
360
363
|
|
|
@@ -374,9 +377,6 @@ module.exports = class Schema {
|
|
|
374
377
|
}));
|
|
375
378
|
}
|
|
376
379
|
|
|
377
|
-
// Validate
|
|
378
|
-
rules.push(a => (a.query.op === 'createOne' ? Pipeline.$validate({ ...a, ...args, path: a.path.concat(curr.name) }) : undefined));
|
|
379
|
-
|
|
380
380
|
return Object.assign(prev, { [curr.name]: rules });
|
|
381
381
|
}, {}),
|
|
382
382
|
defaults: Object.values($model.fields).reduce((prev, curr) => {
|
|
@@ -388,6 +388,7 @@ module.exports = class Schema {
|
|
|
388
388
|
|
|
389
389
|
$model.transformers.update.config({
|
|
390
390
|
strictSchema: true,
|
|
391
|
+
keepUndefined: true,
|
|
391
392
|
shape: Object.values($model.fields).reduce((prev, curr) => {
|
|
392
393
|
const args = { model: $model, field: curr };
|
|
393
394
|
|
|
@@ -407,13 +408,9 @@ module.exports = class Schema {
|
|
|
407
408
|
}));
|
|
408
409
|
}
|
|
409
410
|
|
|
410
|
-
// Validate
|
|
411
|
-
rules.push(a => (a.query.op === 'updateOne' ? Pipeline.$validate({ ...a, ...args, path: a.path.concat(curr.name) }) : undefined));
|
|
412
|
-
|
|
413
411
|
return Object.assign(prev, { [curr.name]: rules });
|
|
414
412
|
}, {}),
|
|
415
413
|
defaults: Object.values($model.fields).reduce((prev, curr) => {
|
|
416
|
-
// if (curr.defaultValue !== undefined) return Object.assign(prev, { [curr.name]: curr.defaultValue });
|
|
417
414
|
if (updatePipelines.some(el => curr.pipelines[el].length)) return Object.assign(prev, { [curr.name]: undefined });
|
|
418
415
|
return prev;
|
|
419
416
|
}, {}),
|
|
@@ -474,6 +471,26 @@ module.exports = class Schema {
|
|
|
474
471
|
return Object.assign(prev, { [curr.key]: curr.defaultValue });
|
|
475
472
|
}, {}),
|
|
476
473
|
});
|
|
474
|
+
|
|
475
|
+
$model.transformers.validate.config({
|
|
476
|
+
strictSchema: true,
|
|
477
|
+
shape: Object.values($model.fields).reduce((prev, curr) => {
|
|
478
|
+
const args = { model: $model, field: curr };
|
|
479
|
+
const rules = [];
|
|
480
|
+
|
|
481
|
+
if (curr.isEmbedded) {
|
|
482
|
+
rules.push(a => Util.map(a.value, (value, i) => {
|
|
483
|
+
const path = a.path.concat(curr.name);
|
|
484
|
+
if (curr.isArray) path.push(i);
|
|
485
|
+
return curr.model.transformers.validate.transform(value, { ...args, thunks: a.thunks, query: a.query, resolver: a.resolver, context: a.context, path });
|
|
486
|
+
}));
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
rules.push(a => Pipeline.$validate({ ...a, ...args, path: a.path.concat(curr.name) }));
|
|
490
|
+
|
|
491
|
+
return Object.assign(prev, { [curr.name]: rules });
|
|
492
|
+
}, {}),
|
|
493
|
+
});
|
|
477
494
|
});
|
|
478
495
|
} else if (node.kind === Kind.FIELD_DEFINITION) {
|
|
479
496
|
const $field = field;
|