@coderich/autograph 0.13.0 → 0.13.1
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/query/Query.js +1 -0
- package/src/schema/Schema.js +27 -18
package/package.json
CHANGED
package/src/query/Query.js
CHANGED
|
@@ -75,6 +75,7 @@ module.exports = class Query {
|
|
|
75
75
|
const { input, where, sort, before, after, isNative, isCursorPaging } = this.#query;
|
|
76
76
|
|
|
77
77
|
const query = this.clone({
|
|
78
|
+
model: this.#model.key,
|
|
78
79
|
select: Object.values(this.#model.fields).map(field => field.key),
|
|
79
80
|
input: this.#model.walk(input, node => node.value !== undefined && Object.assign(node, { key: node.field.key })),
|
|
80
81
|
where: isNative ? where : this.#model.walk(where, node => Object.assign(node, { key: node.field.key })),
|
package/src/schema/Schema.js
CHANGED
|
@@ -61,11 +61,19 @@ module.exports = class Schema {
|
|
|
61
61
|
* Merge typeDefs and resolvers
|
|
62
62
|
*/
|
|
63
63
|
merge(schema = {}) {
|
|
64
|
+
// Normalize schema input
|
|
64
65
|
if (typeof schema === 'string') schema = { typeDefs: schema };
|
|
65
66
|
else if (schema instanceof Schema) schema = schema.toObject();
|
|
66
|
-
|
|
67
|
-
if (typeDefs)
|
|
68
|
-
|
|
67
|
+
|
|
68
|
+
if (schema.typeDefs) {
|
|
69
|
+
const typeDefs = Util.ensureArray(schema.typeDefs).map(td => (typeof td === 'string' ? parse(td) : td));
|
|
70
|
+
this.#typeDefs = mergeTypeDefs([typeDefs, this.#typeDefs], { noLocation: true, reverseDirectives: true, onFieldTypeConflict: a => a });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (schema.resolvers) {
|
|
74
|
+
this.#resolvers = mergeDeep(this.#resolvers, schema.resolvers);
|
|
75
|
+
}
|
|
76
|
+
|
|
69
77
|
return this;
|
|
70
78
|
}
|
|
71
79
|
|
|
@@ -344,6 +352,7 @@ module.exports = class Schema {
|
|
|
344
352
|
scope: AutoGraphMixed #
|
|
345
353
|
meta: AutoGraphMixed # Custom input "meta" field for mutations
|
|
346
354
|
source: AutoGraphMixed # Data source (default: "default")
|
|
355
|
+
decorate: AutoGraphMixed # Decorator (default: "default")
|
|
347
356
|
embed: Boolean # Mark this an embedded model (default false)
|
|
348
357
|
persist: Boolean # Persist this model (default true)
|
|
349
358
|
) on OBJECT | INTERFACE
|
|
@@ -381,15 +390,15 @@ module.exports = class Schema {
|
|
|
381
390
|
|
|
382
391
|
static #api(schema) {
|
|
383
392
|
// These models are for creating types
|
|
384
|
-
const readModels = Object.values(schema.models).filter(model => model.crud
|
|
385
|
-
const createModels = Object.values(schema.models).filter(model => model.crud
|
|
386
|
-
const updateModels = Object.values(schema.models).filter(model => model.crud
|
|
393
|
+
const readModels = Object.values(schema.models).filter(model => model.crud?.includes('r'));
|
|
394
|
+
const createModels = Object.values(schema.models).filter(model => model.crud?.includes('c'));
|
|
395
|
+
const updateModels = Object.values(schema.models).filter(model => model.crud?.includes('u'));
|
|
387
396
|
|
|
388
397
|
// These are for defining schema queries/mutations
|
|
389
398
|
const entityModels = Object.values(schema.models).filter(model => model.isEntity);
|
|
390
|
-
const queryModels = entityModels.filter(model => model.crud
|
|
391
|
-
const mutationModels = entityModels.filter(model => ['c', 'u', 'd'].some(el => model.crud
|
|
392
|
-
const subscriptionModels = entityModels.filter(model => model.crud
|
|
399
|
+
const queryModels = entityModels.filter(model => model.crud?.includes('r'));
|
|
400
|
+
const mutationModels = entityModels.filter(model => ['c', 'u', 'd'].some(el => model.crud?.includes(el)));
|
|
401
|
+
const subscriptionModels = entityModels.filter(model => model.crud?.includes('s'));
|
|
393
402
|
|
|
394
403
|
return {
|
|
395
404
|
typeDefs: `
|
|
@@ -415,7 +424,7 @@ module.exports = class Schema {
|
|
|
415
424
|
`)}
|
|
416
425
|
|
|
417
426
|
${readModels.map((model) => {
|
|
418
|
-
const fields = Object.values(model.fields).filter(field => field.crud
|
|
427
|
+
const fields = Object.values(model.fields).filter(field => field.crud?.includes('r'));
|
|
419
428
|
const connectionFields = fields.filter(field => field.isConnection);
|
|
420
429
|
|
|
421
430
|
return `
|
|
@@ -443,7 +452,7 @@ module.exports = class Schema {
|
|
|
443
452
|
})}
|
|
444
453
|
|
|
445
454
|
${createModels.map((model) => {
|
|
446
|
-
const fields = Object.values(model.fields).filter(field => field.crud
|
|
455
|
+
const fields = Object.values(model.fields).filter(field => field.crud?.includes('c') && !field.isVirtual);
|
|
447
456
|
|
|
448
457
|
return `
|
|
449
458
|
input ${model}InputCreate {
|
|
@@ -453,7 +462,7 @@ module.exports = class Schema {
|
|
|
453
462
|
})}
|
|
454
463
|
|
|
455
464
|
${updateModels.map((model) => {
|
|
456
|
-
const fields = Object.values(model.fields).filter(field => field.crud
|
|
465
|
+
const fields = Object.values(model.fields).filter(field => field.crud?.includes('u') && !field.isVirtual);
|
|
457
466
|
|
|
458
467
|
return `
|
|
459
468
|
input ${model}InputUpdate {
|
|
@@ -484,9 +493,9 @@ module.exports = class Schema {
|
|
|
484
493
|
${mutationModels.map((model) => {
|
|
485
494
|
const api = [];
|
|
486
495
|
const meta = model.meta ? `meta: ${model.meta}` : '';
|
|
487
|
-
if (model.crud
|
|
488
|
-
if (model.crud
|
|
489
|
-
if (model.crud
|
|
496
|
+
if (model.crud?.includes('c')) api.push(`create${model}(input: ${model}InputCreate! ${meta}): ${model}!`);
|
|
497
|
+
if (model.crud?.includes('u')) api.push(`update${model}(id: ID! input: ${model}InputUpdate ${meta}): ${model}!`);
|
|
498
|
+
if (model.crud?.includes('d')) api.push(`delete${model}(id: ID! ${meta}): ${model}!`);
|
|
490
499
|
return api.join('\n');
|
|
491
500
|
})}
|
|
492
501
|
}
|
|
@@ -541,9 +550,9 @@ module.exports = class Schema {
|
|
|
541
550
|
}),
|
|
542
551
|
...(mutationModels.length ? {
|
|
543
552
|
Mutation: mutationModels.reduce((prev, model) => {
|
|
544
|
-
if (model.crud
|
|
545
|
-
if (model.crud
|
|
546
|
-
if (model.crud
|
|
553
|
+
if (model.crud?.includes('c')) prev[`create${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).save(args.input);
|
|
554
|
+
if (model.crud?.includes('u')) prev[`update${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).save(args.input);
|
|
555
|
+
if (model.crud?.includes('d')) prev[`delete${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).delete();
|
|
547
556
|
return prev;
|
|
548
557
|
}, {}),
|
|
549
558
|
} : {}),
|