@coderich/autograph 0.13.6 → 0.13.8
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 +54 -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,15 +362,19 @@ 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
|
|
370
|
+
scalar AutoGraphDriver # DELETE WHEN MIGRATED
|
|
360
371
|
|
|
361
372
|
enum AutoGraphIndexEnum { unique }
|
|
373
|
+
enum AutoGraphAuthzEnum { private protected public } # DELETE WHEN MIGRATED
|
|
362
374
|
enum AutoGraphOnDeleteEnum { cascade nullify restrict defer }
|
|
363
375
|
enum AutoGraphPipelineEnum { ${Object.keys(Pipeline).filter(k => !k.startsWith('$')).join(' ')} }
|
|
364
376
|
|
|
365
|
-
directive
|
|
377
|
+
directive @${model}(
|
|
366
378
|
id: String # Specify the ID/PK field (default "id")
|
|
367
379
|
key: String # Specify db table/collection name
|
|
368
380
|
crud: AutoGraphMixed # CRUD API
|
|
@@ -372,9 +384,19 @@ module.exports = class Schema {
|
|
|
372
384
|
decorate: AutoGraphMixed # Decorator (default: "default")
|
|
373
385
|
embed: Boolean # Mark this an embedded model (default false)
|
|
374
386
|
persist: Boolean # Persist this model (default true)
|
|
387
|
+
|
|
388
|
+
# TEMP TO APPEASE TRANSITION
|
|
389
|
+
driver: AutoGraphDriver # External data driver
|
|
390
|
+
createdAt: String # Specify db key (default "createdAt")
|
|
391
|
+
updatedAt: String # Specify db key (default "updatedAt")
|
|
392
|
+
gqlScope: AutoGraphMixed # Dictate how GraphQL API behaves
|
|
393
|
+
dalScope: AutoGraphMixed # Dictate how the DAL behaves
|
|
394
|
+
fieldScope: AutoGraphMixed # Dictate how a FIELD may use me
|
|
395
|
+
authz: AutoGraphAuthzEnum # Access level used for authorization (default: private)
|
|
396
|
+
namespace: String # Logical grouping of models that can be globbed (useful for authz)
|
|
375
397
|
) on OBJECT | INTERFACE
|
|
376
398
|
|
|
377
|
-
directive
|
|
399
|
+
directive @${field}(
|
|
378
400
|
key: String # Specify db key
|
|
379
401
|
persist: Boolean # Persist this field (default true)
|
|
380
402
|
connection: Boolean # Treat this field as a connection type (default false - rolling this out slowly)
|
|
@@ -390,15 +412,25 @@ module.exports = class Schema {
|
|
|
390
412
|
serialize: [AutoGraphPipelineEnum!]
|
|
391
413
|
finalize: [AutoGraphPipelineEnum!]
|
|
392
414
|
validate: [AutoGraphPipelineEnum!] # Alias for finalize
|
|
415
|
+
|
|
416
|
+
# TEMP TO APPEASE TRANSITION
|
|
417
|
+
id: String # Specify the ModelRef this field FK References
|
|
418
|
+
ref: AutoGraphMixed # Specify the modelRef field's name (overrides isEmbedded)
|
|
419
|
+
gqlScope: AutoGraphMixed # Dictate how GraphQL API behaves
|
|
420
|
+
dalScope: AutoGraphMixed # Dictate how the DAL behaves
|
|
421
|
+
fieldScope: AutoGraphMixed # Dictate how a FIELD may use me
|
|
422
|
+
destruct: [AutoGraphPipelineEnum!]
|
|
423
|
+
transform: [AutoGraphPipelineEnum!]
|
|
424
|
+
deserialize: [AutoGraphPipelineEnum!]
|
|
393
425
|
) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | SCALAR
|
|
394
426
|
|
|
395
|
-
directive
|
|
427
|
+
directive @${link}(
|
|
396
428
|
to: AutoGraphMixed # The MODEL to link to (default's to modelRef)
|
|
397
429
|
by: AutoGraphMixed! # The FIELD to match yourself by
|
|
398
430
|
use: AutoGraphMixed # The VALUE to use (default's to @link'd value); useful for many-to-many relationships
|
|
399
431
|
) on FIELD_DEFINITION
|
|
400
432
|
|
|
401
|
-
directive
|
|
433
|
+
directive @${index}(
|
|
402
434
|
name: String
|
|
403
435
|
on: [AutoGraphMixed!]!
|
|
404
436
|
type: AutoGraphIndexEnum!
|