@coderich/autograph 0.13.4 → 0.13.6
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 +1 -1
- package/src/schema/Schema.js +26 -11
package/package.json
CHANGED
package/src/data/Resolver.js
CHANGED
|
@@ -23,7 +23,7 @@ module.exports = class Resolver {
|
|
|
23
23
|
this.#dataLoaders = this.#createDataLoaders();
|
|
24
24
|
this.driver = this.raw; // Alias
|
|
25
25
|
this.model = this.match; // Alias
|
|
26
|
-
Util.set(this.#context,
|
|
26
|
+
Util.set(this.#context, `${this.#schema.namespace}.resolver`, this);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
getContext() {
|
package/src/schema/Schema.js
CHANGED
|
@@ -23,6 +23,7 @@ module.exports = class Schema {
|
|
|
23
23
|
|
|
24
24
|
constructor(config) {
|
|
25
25
|
this.#config = config;
|
|
26
|
+
this.#config.namespace ??= 'autograph';
|
|
26
27
|
this.#typeDefs = Schema.#framework();
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -66,7 +67,16 @@ module.exports = class Schema {
|
|
|
66
67
|
else if (schema instanceof Schema) schema = schema.toObject();
|
|
67
68
|
|
|
68
69
|
if (schema.typeDefs) {
|
|
69
|
-
const typeDefs = Util.ensureArray(schema.typeDefs).map(
|
|
70
|
+
const typeDefs = Util.ensureArray(schema.typeDefs).map((td) => {
|
|
71
|
+
try {
|
|
72
|
+
const $td = typeof td === 'string' ? parse(td) : td;
|
|
73
|
+
return $td;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
console.log(`Unable to parse typeDef (being ignored):\n${td}`); // eslint-disable-line
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}).filter(Boolean);
|
|
79
|
+
|
|
70
80
|
this.#typeDefs = mergeTypeDefs([typeDefs, this.#typeDefs], { noLocation: true, reverseDirectives: true, onFieldTypeConflict: a => a });
|
|
71
81
|
}
|
|
72
82
|
|
|
@@ -84,7 +94,7 @@ module.exports = class Schema {
|
|
|
84
94
|
if (this.#schema) return this.#schema;
|
|
85
95
|
|
|
86
96
|
// const schema = buildASTSchema(this.#typeDefs);
|
|
87
|
-
this.#schema = { types: {}, models: {}, indexes: [] };
|
|
97
|
+
this.#schema = { types: {}, models: {}, indexes: [], namespace: this.#config.namespace };
|
|
88
98
|
let model, field, isField, isList;
|
|
89
99
|
const thunks = [];
|
|
90
100
|
|
|
@@ -169,6 +179,10 @@ module.exports = class Schema {
|
|
|
169
179
|
field.isConnection = value;
|
|
170
180
|
break;
|
|
171
181
|
}
|
|
182
|
+
case 'field-validate': { // Alias for finalize
|
|
183
|
+
target.pipelines.finalize = target.pipelines.finalize.concat(value).filter(Boolean);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
172
186
|
case 'link-by': {
|
|
173
187
|
field.linkBy = value;
|
|
174
188
|
field.isVirtual = true;
|
|
@@ -375,6 +389,7 @@ module.exports = class Schema {
|
|
|
375
389
|
restruct: [AutoGraphPipelineEnum!]
|
|
376
390
|
serialize: [AutoGraphPipelineEnum!]
|
|
377
391
|
finalize: [AutoGraphPipelineEnum!]
|
|
392
|
+
validate: [AutoGraphPipelineEnum!] # Alias for finalize
|
|
378
393
|
) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | SCALAR
|
|
379
394
|
|
|
380
395
|
directive @link(
|
|
@@ -530,12 +545,12 @@ module.exports = class Schema {
|
|
|
530
545
|
}, {}),
|
|
531
546
|
Query: queryModels.reduce((prev, model) => {
|
|
532
547
|
return Object.assign(prev, {
|
|
533
|
-
[`get${model}`]: (doc, args, context, info) => context.
|
|
548
|
+
[`get${model}`]: (doc, args, context, info) => context[schema.namespace].resolver.match(model).args(args).info(info).one({ required: true }),
|
|
534
549
|
[`find${model}`]: (doc, args, context, info) => {
|
|
535
550
|
return {
|
|
536
|
-
edges: () => context.
|
|
537
|
-
count: () => context.
|
|
538
|
-
pageInfo: () => context.
|
|
551
|
+
edges: () => context[schema.namespace].resolver.match(model).args(args).info(info).many(),
|
|
552
|
+
count: () => context[schema.namespace].resolver.match(model).args(args).info(info).count(),
|
|
553
|
+
pageInfo: () => context[schema.namespace].resolver.match(model).args(args).info(info).many(),
|
|
539
554
|
};
|
|
540
555
|
},
|
|
541
556
|
});
|
|
@@ -544,7 +559,7 @@ module.exports = class Schema {
|
|
|
544
559
|
const { id } = args;
|
|
545
560
|
const [modelName] = fromGUID(id);
|
|
546
561
|
const model = schema.models[modelName];
|
|
547
|
-
return context.
|
|
562
|
+
return context[schema.namespace].resolver.match(model).id(id).info(info).one().then((result) => {
|
|
548
563
|
if (result == null) return result;
|
|
549
564
|
result.__typename = modelName; // eslint-disable-line no-underscore-dangle
|
|
550
565
|
return result;
|
|
@@ -553,9 +568,9 @@ module.exports = class Schema {
|
|
|
553
568
|
}),
|
|
554
569
|
...(mutationModels.length ? {
|
|
555
570
|
Mutation: mutationModels.reduce((prev, model) => {
|
|
556
|
-
if (model.crud?.includes('c')) prev[`create${model}`] = (doc, args, context, info) => context.
|
|
557
|
-
if (model.crud?.includes('u')) prev[`update${model}`] = (doc, args, context, info) => context.
|
|
558
|
-
if (model.crud?.includes('d')) prev[`delete${model}`] = (doc, args, context, info) => context.
|
|
571
|
+
if (model.crud?.includes('c')) prev[`create${model}`] = (doc, args, context, info) => context[schema.namespace].resolver.match(model).args(args).info(info).save(args.input);
|
|
572
|
+
if (model.crud?.includes('u')) prev[`update${model}`] = (doc, args, context, info) => context[schema.namespace].resolver.match(model).args(args).info(info).save(args.input);
|
|
573
|
+
if (model.crud?.includes('d')) prev[`delete${model}`] = (doc, args, context, info) => context[schema.namespace].resolver.match(model).args(args).info(info).delete();
|
|
559
574
|
return prev;
|
|
560
575
|
}, {}),
|
|
561
576
|
} : {}),
|
|
@@ -564,7 +579,7 @@ module.exports = class Schema {
|
|
|
564
579
|
[model]: Object.values(model.fields).filter(field => field.model?.isEntity).reduce((prev2, field) => {
|
|
565
580
|
return Object.assign(prev2, {
|
|
566
581
|
[field]: (doc, args, context, info) => {
|
|
567
|
-
return context.
|
|
582
|
+
return context[schema.namespace].resolver.match(field.model).where({ [field.linkBy]: doc[field.linkField.name] }).args(args).info(info).resolve(info);
|
|
568
583
|
},
|
|
569
584
|
});
|
|
570
585
|
}, {}),
|