@coderich/autograph 0.13.12 → 0.13.13
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/schema/Schema.js +45 -15
package/package.json
CHANGED
package/src/schema/Schema.js
CHANGED
|
@@ -8,14 +8,15 @@ const Pipeline = require('../data/Pipeline');
|
|
|
8
8
|
const Emitter = require('../data/Emitter');
|
|
9
9
|
|
|
10
10
|
const operations = ['Query', 'Mutation', 'Subscription'];
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const
|
|
11
|
+
const interfaceKinds = [Kind.INTERFACE_TYPE_DEFINITION, Kind.INTERFACE_TYPE_EXTENSION];
|
|
12
|
+
// const unionKinds = [Kind.UNION_TYPE_DEFINITION, Kind.UNION_TYPE_EXTENSION];
|
|
13
|
+
const scalarKinds = [Kind.SCALAR_TYPE_DEFINITION, Kind.SCALAR_TYPE_EXTENSION, Kind.ENUM_TYPE_DEFINITION, Kind.ENUM_TYPE_EXTENSION];
|
|
14
14
|
const fieldKinds = [Kind.FIELD_DEFINITION].concat(scalarKinds);
|
|
15
|
-
const
|
|
15
|
+
const modelKinds = [Kind.OBJECT_TYPE_DEFINITION, Kind.OBJECT_TYPE_EXTENSION].concat(interfaceKinds);
|
|
16
|
+
const allowedKinds = modelKinds.concat(fieldKinds).concat(Kind.DOCUMENT, Kind.NON_NULL_TYPE, Kind.NAMED_TYPE, Kind.LIST_TYPE, Kind.DIRECTIVE).concat(scalarKinds);
|
|
16
17
|
const pipelines = ['finalize', 'construct', 'restruct', 'instruct', 'normalize', 'serialize'];
|
|
17
18
|
const inputPipelines = ['finalize', 'construct', 'instruct', 'normalize', 'serialize'];
|
|
18
|
-
|
|
19
|
+
const scalars = ['ID', 'String', 'Float', 'Int', 'Boolean'];
|
|
19
20
|
|
|
20
21
|
module.exports = class Schema {
|
|
21
22
|
#config;
|
|
@@ -112,7 +113,6 @@ module.exports = class Schema {
|
|
|
112
113
|
parse() {
|
|
113
114
|
if (this.#schema) return this.#schema;
|
|
114
115
|
|
|
115
|
-
// const schema = buildASTSchema(this.#typeDefs);
|
|
116
116
|
const { directives, namespace } = this.#config;
|
|
117
117
|
this.#schema = { types: {}, models: {}, indexes: [], namespace };
|
|
118
118
|
let model, field, isField, isList;
|
|
@@ -121,7 +121,7 @@ module.exports = class Schema {
|
|
|
121
121
|
// Deprecate
|
|
122
122
|
this.#schema.getModel = name => this.#schema.models[`${name}`];
|
|
123
123
|
|
|
124
|
-
// Parse AST
|
|
124
|
+
// Parse AST (build/defined this.#schema)
|
|
125
125
|
visit(this.#typeDefs, {
|
|
126
126
|
enter: (node) => {
|
|
127
127
|
const name = node.name?.value;
|
|
@@ -153,6 +153,10 @@ module.exports = class Schema {
|
|
|
153
153
|
toString: () => name,
|
|
154
154
|
};
|
|
155
155
|
if (model) model.fields[name] = field;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (scalarKinds.includes(node.kind)) {
|
|
159
|
+
scalars.push(node.name.value);
|
|
156
160
|
} else if (node.kind === Kind.NON_NULL_TYPE) {
|
|
157
161
|
field[isList ? 'isArrayRequired' : 'isRequired'] = true;
|
|
158
162
|
} else if (node.kind === Kind.NAMED_TYPE) {
|
|
@@ -169,8 +173,7 @@ module.exports = class Schema {
|
|
|
169
173
|
|
|
170
174
|
node.arguments.forEach((arg) => {
|
|
171
175
|
const key = arg.name.value;
|
|
172
|
-
const
|
|
173
|
-
const value = kind === 'NullValue' ? null : Util.map(values, n => n.value);
|
|
176
|
+
const value = Schema.#resolveNodeValue(arg.value);
|
|
174
177
|
target.directives[name][key] = value;
|
|
175
178
|
|
|
176
179
|
if (name === directives.index) this.#schema.indexes[this.#schema.indexes.length - 1][key] = value;
|
|
@@ -313,8 +316,8 @@ module.exports = class Schema {
|
|
|
313
316
|
$field.linkBy ??= $field.model?.idField;
|
|
314
317
|
$field.linkField = $field.isVirtual ? $model.fields[$model.idField] : $field;
|
|
315
318
|
$field.isFKReference = !$field.isPrimaryKey && $field.model?.isMarkedModel && !$field.model?.isEmbedded;
|
|
316
|
-
// $field.isScalar = Boolean(!$field.model || scalars.includes($field.type));
|
|
317
319
|
$field.isEmbedded = Boolean($field.model && !$field.isFKReference && !$field.isPrimaryKey);
|
|
320
|
+
$field.isScalar = scalars.includes($field.type);
|
|
318
321
|
|
|
319
322
|
if ($field.isArray) $field.pipelines.normalize.unshift('toArray');
|
|
320
323
|
if ($field.isPrimaryKey) $field.pipelines.serialize.unshift('$pk'); // Will create/convert to FK type always
|
|
@@ -362,6 +365,23 @@ module.exports = class Schema {
|
|
|
362
365
|
return fieldKeys.reduce((parent, key) => Object.values(parent.fields || parent.model.fields).find(el => el[prop] === key) || parent, $model);
|
|
363
366
|
};
|
|
364
367
|
|
|
368
|
+
// Mutate typeDefs
|
|
369
|
+
let $model;
|
|
370
|
+
this.#typeDefs = visit(this.#typeDefs, {
|
|
371
|
+
enter: (node) => {
|
|
372
|
+
const name = node.name?.value;
|
|
373
|
+
if (!allowedKinds.includes(node.kind) || operations.includes(name)) return false;
|
|
374
|
+
|
|
375
|
+
if (modelKinds.includes(node.kind)) {
|
|
376
|
+
$model = this.#schema.models[name];
|
|
377
|
+
} else if (fieldKinds.includes(node.kind)) {
|
|
378
|
+
if (!Util.uvl($model?.fields[name]?.crud, 'crud')?.includes('r')) return null;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return undefined;
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
|
|
365
385
|
// Return schema
|
|
366
386
|
return this.#schema;
|
|
367
387
|
}
|
|
@@ -390,6 +410,15 @@ module.exports = class Schema {
|
|
|
390
410
|
return this.#config.makeExecutableSchema(this.toObject());
|
|
391
411
|
}
|
|
392
412
|
|
|
413
|
+
static #resolveNodeValue(node) {
|
|
414
|
+
switch (node.kind) {
|
|
415
|
+
case 'NullValue': return null;
|
|
416
|
+
case 'ListValue': return node.values.map(Schema.#resolveNodeValue);
|
|
417
|
+
case 'ObjectValue': return node.fields.reduce((prev, field) => Object.assign(prev, { [field.name.value]: Schema.#resolveNodeValue(field.value) }), {});
|
|
418
|
+
default: return node.value ?? node;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
393
422
|
static #framework(directives) {
|
|
394
423
|
const { model, field, link, index } = directives;
|
|
395
424
|
|
|
@@ -480,6 +509,7 @@ module.exports = class Schema {
|
|
|
480
509
|
return {
|
|
481
510
|
typeDefs: `
|
|
482
511
|
scalar AutoGraphMixed
|
|
512
|
+
scalar AutoGraphDateTime
|
|
483
513
|
|
|
484
514
|
interface Node { id: ID! }
|
|
485
515
|
|
|
@@ -506,10 +536,10 @@ module.exports = class Schema {
|
|
|
506
536
|
|
|
507
537
|
return `
|
|
508
538
|
input ${model}InputWhere {
|
|
509
|
-
${fields.map(field => `${field}: ${field.model
|
|
539
|
+
${fields.map(field => `${field}: ${field.model ? `${field.model}InputWhere` : 'AutoGraphMixed'}`)}
|
|
510
540
|
}
|
|
511
541
|
input ${model}InputSort {
|
|
512
|
-
${fields.map(field => `${field}: ${field.model
|
|
542
|
+
${fields.map(field => `${field}: ${field.model ? `${field.model}InputSort` : 'SortOrderEnum'}`)}
|
|
513
543
|
}
|
|
514
544
|
type ${model}Connection {
|
|
515
545
|
count: Int!
|
|
@@ -598,7 +628,7 @@ module.exports = class Schema {
|
|
|
598
628
|
}
|
|
599
629
|
|
|
600
630
|
input ${model}SubscriptionInputWhere {
|
|
601
|
-
${fields.map(field => `${field}: ${field.model
|
|
631
|
+
${fields.map(field => `${field}: ${field.model ? `${field.model}InputWhere` : 'AutoGraphMixed'}`)}
|
|
602
632
|
}
|
|
603
633
|
|
|
604
634
|
type ${model}SubscriptionPayload {
|
|
@@ -691,12 +721,12 @@ module.exports = class Schema {
|
|
|
691
721
|
|
|
692
722
|
static #getGQLType(field, suffix) {
|
|
693
723
|
let { type } = field;
|
|
694
|
-
const { isEmbedded, isRequired, isScalar, isArray, isArrayRequired, defaultValue } = field;
|
|
724
|
+
const { isEmbedded, isRequired, isScalar, isArray, isArrayRequired, isPrimaryKey, defaultValue } = field;
|
|
695
725
|
const modelType = `${type}${suffix}`;
|
|
696
726
|
if (suffix && !isScalar) type = isEmbedded ? modelType : 'ID';
|
|
697
727
|
type = isArray ? `[${type}${isArrayRequired ? '!' : ''}]` : type;
|
|
698
728
|
if (!suffix && isRequired) type += '!';
|
|
699
|
-
if (suffix === 'InputCreate' && isRequired && defaultValue
|
|
729
|
+
if (suffix === 'InputCreate' && !isPrimaryKey && isRequired && defaultValue == null) type += '!';
|
|
700
730
|
return type;
|
|
701
731
|
}
|
|
702
732
|
|