@coderich/autograph 0.13.12 → 0.13.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/autograph",
3
3
  "main": "index.js",
4
- "version": "0.13.12",
4
+ "version": "0.13.14",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -63,6 +63,7 @@ module.exports = class Pipeline {
63
63
  Pipeline.define('createdAt', ({ value }) => value || new Date(), { ignoreNull: false });
64
64
  Pipeline.define('timestamp', () => Date.now(), { ignoreNull: false });
65
65
  Pipeline.define('dedupe', ({ value }) => uniqWith(value, (b, c) => hashObject(b) === hashObject(c)), { itemize: false });
66
+ Pipeline.define('toId', ({ model, value }) => model.source.idValue(value.id || value)); // Deprecate
66
67
 
67
68
  // Structures
68
69
  Pipeline.define('$instruct', params => Pipeline.resolve(params, 'instruct'), { ignoreNull: false });
@@ -280,8 +280,11 @@ module.exports = class Resolver {
280
280
  const event = { schema: this.#schema, context: this.#context, resolver: this, query };
281
281
 
282
282
  // Backwards compat
283
- query.match = { ...query.where };
283
+ Object.assign(event, query);
284
+ query.match = query.where;
284
285
  query.toObject = () => query;
286
+ event.merged = { ...event.input };
287
+ event.input = event.args?.input;
285
288
 
286
289
  return Emitter.emit(`pre${type}`, event).then(async (resultEarly) => {
287
290
  if (resultEarly !== undefined) return resultEarly;
@@ -289,13 +292,16 @@ module.exports = class Resolver {
289
292
  if (query.isMutation) query.input = await tquery.pipeline('input', query.input, ['$finalize']);
290
293
  if (query.isMutation) await Emitter.emit('finalize', event);
291
294
  return thunk().then((result) => {
295
+ event.result = result; // backwards compat
292
296
  query.result = result;
293
297
  return Emitter.emit(`post${type}`, event);
294
298
  });
295
299
  }).then((result = query.result) => {
300
+ event.result = result; // backwards compat
296
301
  query.result = result;
297
302
  return Emitter.emit('preResponse', event);
298
303
  }).then((result = query.result) => {
304
+ event.result = result; // backwards compat
299
305
  query.result = result;
300
306
  return Emitter.emit('postResponse', event);
301
307
  }).then((result = query.result) => result).catch((e) => {
@@ -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
- // const interfaceKinds = [Kind.INTERFACE_TYPE_DEFINITION, Kind.INTERFACE_TYPE_EXTENSION];
12
- const scalarKinds = [Kind.SCALAR_TYPE_DEFINITION, Kind.SCALAR_TYPE_EXTENSION];
13
- const modelKinds = [Kind.OBJECT_TYPE_DEFINITION, Kind.OBJECT_TYPE_EXTENSION, Kind.INTERFACE_TYPE_DEFINITION, Kind.INTERFACE_TYPE_EXTENSION];
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 allowedKinds = modelKinds.concat(fieldKinds).concat(Kind.DOCUMENT, Kind.NON_NULL_TYPE, Kind.NAMED_TYPE, Kind.LIST_TYPE, Kind.DIRECTIVE);
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
- // const scalars = ['ID', 'String', 'Float', 'Int', 'Boolean'];
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 { value: val, values = { value: val }, kind } = arg.value;
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
@@ -334,6 +337,8 @@ module.exports = class Schema {
334
337
  isField = false;
335
338
  } else if (node.kind === Kind.LIST_TYPE) {
336
339
  isList = false;
340
+ } else if (scalarKinds.includes(node.kind)) {
341
+ isField = false;
337
342
  }
338
343
  },
339
344
  });
@@ -362,6 +367,23 @@ module.exports = class Schema {
362
367
  return fieldKeys.reduce((parent, key) => Object.values(parent.fields || parent.model.fields).find(el => el[prop] === key) || parent, $model);
363
368
  };
364
369
 
370
+ // Mutate typeDefs
371
+ let $model;
372
+ this.#typeDefs = visit(this.#typeDefs, {
373
+ enter: (node) => {
374
+ const name = node.name?.value;
375
+ if (!allowedKinds.includes(node.kind) || operations.includes(name)) return false;
376
+
377
+ if (modelKinds.includes(node.kind)) {
378
+ $model = this.#schema.models[name];
379
+ } else if (fieldKinds.includes(node.kind)) {
380
+ if (!Util.uvl($model?.fields[name]?.crud, 'crud')?.includes('r')) return null;
381
+ }
382
+
383
+ return undefined;
384
+ },
385
+ });
386
+
365
387
  // Return schema
366
388
  return this.#schema;
367
389
  }
@@ -390,6 +412,15 @@ module.exports = class Schema {
390
412
  return this.#config.makeExecutableSchema(this.toObject());
391
413
  }
392
414
 
415
+ static #resolveNodeValue(node) {
416
+ switch (node.kind) {
417
+ case 'NullValue': return null;
418
+ case 'ListValue': return node.values.map(Schema.#resolveNodeValue);
419
+ case 'ObjectValue': return node.fields.reduce((prev, field) => Object.assign(prev, { [field.name.value]: Schema.#resolveNodeValue(field.value) }), {});
420
+ default: return node.value ?? node;
421
+ }
422
+ }
423
+
393
424
  static #framework(directives) {
394
425
  const { model, field, link, index } = directives;
395
426
 
@@ -480,6 +511,7 @@ module.exports = class Schema {
480
511
  return {
481
512
  typeDefs: `
482
513
  scalar AutoGraphMixed
514
+ scalar AutoGraphDateTime
483
515
 
484
516
  interface Node { id: ID! }
485
517
 
@@ -506,10 +538,10 @@ module.exports = class Schema {
506
538
 
507
539
  return `
508
540
  input ${model}InputWhere {
509
- ${fields.map(field => `${field}: ${field.model?.isEntity ? `${field.model}InputWhere` : 'AutoGraphMixed'}`)}
541
+ ${fields.map(field => `${field}: ${field.model ? `${field.model}InputWhere` : 'AutoGraphMixed'}`)}
510
542
  }
511
543
  input ${model}InputSort {
512
- ${fields.map(field => `${field}: ${field.model?.isEntity ? `${field.model}InputSort` : 'SortOrderEnum'}`)}
544
+ ${fields.map(field => `${field}: ${field.model ? `${field.model}InputSort` : 'SortOrderEnum'}`)}
513
545
  }
514
546
  type ${model}Connection {
515
547
  count: Int!
@@ -598,7 +630,7 @@ module.exports = class Schema {
598
630
  }
599
631
 
600
632
  input ${model}SubscriptionInputWhere {
601
- ${fields.map(field => `${field}: ${field.model?.isEntity ? `${field.model}InputWhere` : 'AutoGraphMixed'}`)}
633
+ ${fields.map(field => `${field}: ${field.model ? `${field.model}InputWhere` : 'AutoGraphMixed'}`)}
602
634
  }
603
635
 
604
636
  type ${model}SubscriptionPayload {
@@ -691,12 +723,12 @@ module.exports = class Schema {
691
723
 
692
724
  static #getGQLType(field, suffix) {
693
725
  let { type } = field;
694
- const { isEmbedded, isRequired, isScalar, isArray, isArrayRequired, defaultValue } = field;
726
+ const { isEmbedded, isRequired, isScalar, isArray, isArrayRequired, isPrimaryKey, defaultValue } = field;
695
727
  const modelType = `${type}${suffix}`;
696
728
  if (suffix && !isScalar) type = isEmbedded ? modelType : 'ID';
697
729
  type = isArray ? `[${type}${isArrayRequired ? '!' : ''}]` : type;
698
730
  if (!suffix && isRequired) type += '!';
699
- if (suffix === 'InputCreate' && isRequired && defaultValue != null) type += '!';
731
+ if (suffix === 'InputCreate' && !isPrimaryKey && isRequired && defaultValue == null) type += '!';
700
732
  return type;
701
733
  }
702
734