@coderich/autograph 0.13.6 → 0.13.7
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 +32 -22
package/package.json
CHANGED
package/src/schema/Schema.js
CHANGED
|
@@ -24,17 +24,24 @@ module.exports = class Schema {
|
|
|
24
24
|
constructor(config) {
|
|
25
25
|
this.#config = config;
|
|
26
26
|
this.#config.namespace ??= 'autograph';
|
|
27
|
-
this.#
|
|
27
|
+
this.#config.directives ??= {};
|
|
28
|
+
this.#config.directives.model ??= 'model';
|
|
29
|
+
this.#config.directives.field ??= 'field';
|
|
30
|
+
this.#config.directives.link ??= 'link';
|
|
31
|
+
this.#config.directives.index ??= 'index';
|
|
32
|
+
this.#typeDefs = Schema.#framework(this.#config.directives);
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
/**
|
|
31
36
|
* Decorate each marked @model with config-driven field decorators
|
|
32
37
|
*/
|
|
33
38
|
decorate() {
|
|
39
|
+
const { directives } = this.#config;
|
|
40
|
+
|
|
34
41
|
this.#typeDefs = visit(this.#typeDefs, {
|
|
35
42
|
enter: (node) => {
|
|
36
43
|
if (modelKinds.includes(node.kind) && !operations.includes(node.name.value)) {
|
|
37
|
-
const directive = node.directives.find(({ name }) => name.value ===
|
|
44
|
+
const directive = node.directives.find(({ name }) => name.value === directives.model);
|
|
38
45
|
|
|
39
46
|
if (directive) {
|
|
40
47
|
const arg = directive.arguments.find(({ name }) => name.value === 'decorate');
|
|
@@ -94,7 +101,8 @@ module.exports = class Schema {
|
|
|
94
101
|
if (this.#schema) return this.#schema;
|
|
95
102
|
|
|
96
103
|
// const schema = buildASTSchema(this.#typeDefs);
|
|
97
|
-
|
|
104
|
+
const { directives, namespace } = this.#config;
|
|
105
|
+
this.#schema = { types: {}, models: {}, indexes: [], namespace };
|
|
98
106
|
let model, field, isField, isList;
|
|
99
107
|
const thunks = [];
|
|
100
108
|
|
|
@@ -141,8 +149,8 @@ module.exports = class Schema {
|
|
|
141
149
|
const target = isField ? field : model;
|
|
142
150
|
target.directives[name] = target.directives[name] || {};
|
|
143
151
|
|
|
144
|
-
if (name ===
|
|
145
|
-
else if (name ===
|
|
152
|
+
if (name === directives.model) model.isMarkedModel = true;
|
|
153
|
+
else if (name === directives.index) this.#schema.indexes.push({ model });
|
|
146
154
|
|
|
147
155
|
node.arguments.forEach((arg) => {
|
|
148
156
|
const key = arg.name.value;
|
|
@@ -150,54 +158,54 @@ module.exports = class Schema {
|
|
|
150
158
|
const value = values ? values.map(n => n.value) : val;
|
|
151
159
|
target.directives[name][key] = value;
|
|
152
160
|
|
|
153
|
-
if (name ===
|
|
161
|
+
if (name === directives.index) this.#schema.indexes[this.#schema.indexes.length - 1][key] = value;
|
|
154
162
|
|
|
155
163
|
switch (`${name}-${key}`) {
|
|
156
164
|
// Model specific directives
|
|
157
|
-
case
|
|
165
|
+
case `${directives.model}-id`: {
|
|
158
166
|
model.idField = value;
|
|
159
167
|
break;
|
|
160
168
|
}
|
|
161
|
-
case
|
|
169
|
+
case `${directives.model}-source`: {
|
|
162
170
|
model.source = this.#config.dataSources?.[value];
|
|
163
171
|
break;
|
|
164
172
|
}
|
|
165
|
-
case
|
|
173
|
+
case `${directives.model}-loader`: {
|
|
166
174
|
model.loader = this.#config.dataLoaders?.[value];
|
|
167
175
|
break;
|
|
168
176
|
}
|
|
169
|
-
case
|
|
177
|
+
case `${directives.model}-embed`: {
|
|
170
178
|
model.isEmbedded = value;
|
|
171
179
|
break;
|
|
172
180
|
}
|
|
173
181
|
// Field specific directives
|
|
174
|
-
case
|
|
182
|
+
case `${directives.field}-default`: {
|
|
175
183
|
field.defaultValue = value;
|
|
176
184
|
break;
|
|
177
185
|
}
|
|
178
|
-
case
|
|
186
|
+
case `${directives.field}-connection`: {
|
|
179
187
|
field.isConnection = value;
|
|
180
188
|
break;
|
|
181
189
|
}
|
|
182
|
-
case
|
|
190
|
+
case `${directives.field}-validate`: { // Alias for finalize
|
|
183
191
|
target.pipelines.finalize = target.pipelines.finalize.concat(value).filter(Boolean);
|
|
184
192
|
break;
|
|
185
193
|
}
|
|
186
|
-
case
|
|
194
|
+
case `${directives.link}-by`: {
|
|
187
195
|
field.linkBy = value;
|
|
188
196
|
field.isVirtual = true;
|
|
189
197
|
break;
|
|
190
198
|
}
|
|
191
199
|
// Generic by target directives
|
|
192
|
-
case
|
|
200
|
+
case `${directives.model}-persist`: case `${directives.field}-persist`: {
|
|
193
201
|
target.isPersistable = value;
|
|
194
202
|
break;
|
|
195
203
|
}
|
|
196
|
-
case
|
|
204
|
+
case `${directives.model}-crud`: case `${directives.model}-scope`: case `${directives.field}-crud`: {
|
|
197
205
|
target[key] = Util.nvl(value, '');
|
|
198
206
|
break;
|
|
199
207
|
}
|
|
200
|
-
case
|
|
208
|
+
case `${directives.model}-key`: case `${directives.model}-meta`: case `${directives.field}-key`: case `${directives.field}-onDelete`: {
|
|
201
209
|
target[key] = value;
|
|
202
210
|
break;
|
|
203
211
|
}
|
|
@@ -354,7 +362,9 @@ module.exports = class Schema {
|
|
|
354
362
|
return this.#config.makeExecutableSchema(this.toObject());
|
|
355
363
|
}
|
|
356
364
|
|
|
357
|
-
static #framework() {
|
|
365
|
+
static #framework(directives) {
|
|
366
|
+
const { model, field, link, index } = directives;
|
|
367
|
+
|
|
358
368
|
return parse(`
|
|
359
369
|
scalar AutoGraphMixed
|
|
360
370
|
|
|
@@ -362,7 +372,7 @@ module.exports = class Schema {
|
|
|
362
372
|
enum AutoGraphOnDeleteEnum { cascade nullify restrict defer }
|
|
363
373
|
enum AutoGraphPipelineEnum { ${Object.keys(Pipeline).filter(k => !k.startsWith('$')).join(' ')} }
|
|
364
374
|
|
|
365
|
-
directive
|
|
375
|
+
directive @${model}(
|
|
366
376
|
id: String # Specify the ID/PK field (default "id")
|
|
367
377
|
key: String # Specify db table/collection name
|
|
368
378
|
crud: AutoGraphMixed # CRUD API
|
|
@@ -374,7 +384,7 @@ module.exports = class Schema {
|
|
|
374
384
|
persist: Boolean # Persist this model (default true)
|
|
375
385
|
) on OBJECT | INTERFACE
|
|
376
386
|
|
|
377
|
-
directive
|
|
387
|
+
directive @${field}(
|
|
378
388
|
key: String # Specify db key
|
|
379
389
|
persist: Boolean # Persist this field (default true)
|
|
380
390
|
connection: Boolean # Treat this field as a connection type (default false - rolling this out slowly)
|
|
@@ -392,13 +402,13 @@ module.exports = class Schema {
|
|
|
392
402
|
validate: [AutoGraphPipelineEnum!] # Alias for finalize
|
|
393
403
|
) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | SCALAR
|
|
394
404
|
|
|
395
|
-
directive
|
|
405
|
+
directive @${link}(
|
|
396
406
|
to: AutoGraphMixed # The MODEL to link to (default's to modelRef)
|
|
397
407
|
by: AutoGraphMixed! # The FIELD to match yourself by
|
|
398
408
|
use: AutoGraphMixed # The VALUE to use (default's to @link'd value); useful for many-to-many relationships
|
|
399
409
|
) on FIELD_DEFINITION
|
|
400
410
|
|
|
401
|
-
directive
|
|
411
|
+
directive @${index}(
|
|
402
412
|
name: String
|
|
403
413
|
on: [AutoGraphMixed!]!
|
|
404
414
|
type: AutoGraphIndexEnum!
|