@coderich/autograph 0.13.25 → 0.13.27

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.25",
4
+ "version": "0.13.27",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -6,9 +6,11 @@ const { hashObject } = require('../service/AppService');
6
6
  module.exports = class Loader {
7
7
  #model;
8
8
  #loader;
9
+ #resolver;
9
10
 
10
- constructor(model) {
11
+ constructor(model, resolver) {
11
12
  this.#model = model;
13
+ this.#resolver = resolver;
12
14
  model.loader.cacheKeyFn ??= (query => hashObject(query.toCacheKey()));
13
15
  this.#loader = new DataLoader(keys => this.#resolve(keys), model.loader);
14
16
  }
@@ -28,8 +30,8 @@ module.exports = class Loader {
28
30
 
29
31
  return this.#model.source.client.resolve($query).then((data) => {
30
32
  if (data == null) return null; // Explicit return null;
31
- if ($query.isCursorPaging && Array.isArray(data)) return Loader.#paginateResults(data, query.toObject());
32
- return data;
33
+ if ($query.isCursorPaging && Array.isArray(data)) data = Loader.#paginateResults(data, query.toObject());
34
+ return this.#resolver.toResultSet(this.#model, data);
33
35
  });
34
36
  }));
35
37
  }
@@ -190,7 +190,7 @@ module.exports = class Resolver {
190
190
  */
191
191
  async resolve(query) {
192
192
  let thunk;
193
- const { model, doc, crud, isMutation, flags } = query.toObject();
193
+ const { doc, model, crud, isMutation, flags } = query.toObject();
194
194
  const currSession = this.#sessions.slice(-1).pop();
195
195
 
196
196
  if (isMutation) {
@@ -202,7 +202,8 @@ module.exports = class Resolver {
202
202
  currSession?.thunks.push(...this.#sessions.map(s => () => s.parent.clear(model)));
203
203
 
204
204
  // Return results
205
- return crud === 'delete' ? doc : results;
205
+ if (crud === 'delete') return doc;
206
+ return this.toResultSet(model, results);
206
207
  });
207
208
  } else {
208
209
  thunk = tquery => this.#dataLoaders[model].resolve(tquery);
@@ -211,7 +212,7 @@ module.exports = class Resolver {
211
212
  return this.#createSystemEvent(query, (tquery) => {
212
213
  return thunk(tquery).then((result) => {
213
214
  if (flags?.required && (result == null || result?.length === 0)) throw Boom.notFound();
214
- return crud === 'delete' ? result : this.toResultSet(model, result);
215
+ return result;
215
216
  });
216
217
  });
217
218
  }
@@ -224,8 +225,8 @@ module.exports = class Resolver {
224
225
  // Transform result to domain model
225
226
  const $doc = this.#schema.models[model].walk(doc, (node) => {
226
227
  if (node.value === undefined) return undefined;
228
+ if (node.value != null && node.field.isArray) node.value = Util.ensureArray(node.value);
227
229
  node.key = node.field.name;
228
- node.value = Pipeline.$cast(node);
229
230
  return node;
230
231
  }, { key: 'key' });
231
232
 
@@ -257,6 +258,15 @@ module.exports = class Resolver {
257
258
  },
258
259
  $model: { value: model },
259
260
  $cursor: { value: doc.$cursor },
261
+ // Backwards compat
262
+ $save: { value: (...args) => $doc.$.save(...args) },
263
+ $lookup: {
264
+ value: async (prop, args) => {
265
+ const field = model.fields[prop];
266
+ const method = field.isArray ? 'many' : 'one';
267
+ return $doc.$.lookup(prop).args(args)[method]();
268
+ },
269
+ },
260
270
  });
261
271
  }), {
262
272
  $pageInfo: { value: result.$pageInfo },
@@ -271,7 +281,7 @@ module.exports = class Resolver {
271
281
  return Object.entries(this.#schema.models).filter(([key, value]) => {
272
282
  return value.loader && value.isEntity;
273
283
  }).reduce((prev, [key, value]) => {
274
- return Object.assign(prev, { [key]: new DataLoader(value) });
284
+ return Object.assign(prev, { [key]: new DataLoader(value, this) });
275
285
  }, {});
276
286
  }
277
287
 
@@ -292,11 +302,12 @@ module.exports = class Resolver {
292
302
  // if (query.crud === 'update' && Util.isEqual({ added: {}, updated: {}, deleted: {} }, Util.changeset(query.doc, query.input))) return query.doc;
293
303
  const tquery = await $query.transform();
294
304
  // await Emitter.emit('validate', event); // We need to re-connect tquery to event
295
- return thunk(tquery).then((result) => {
296
- event.result = result; // backwards compat
297
- query.result = result;
298
- return Emitter.emit(`post${type}`, event);
299
- });
305
+ return thunk(tquery);
306
+ }).then((result) => {
307
+ event.doc ??= result; // Case of create
308
+ event.result = result; // backwards compat
309
+ query.result = result;
310
+ return Emitter.emit(`post${type}`, event);
300
311
  }).then((result = query.result) => result).catch((e) => {
301
312
  const { data = {} } = e;
302
313
  throw Boom.boomify(e, { data: { ...event, ...data } });
@@ -34,7 +34,7 @@ module.exports = class QueryResolver extends QueryBuilder {
34
34
  }
35
35
  case 'updateOne': {
36
36
  return this.#get(query).then((doc) => {
37
- const merged = mergeDeep({}, Util.unflatten(doc, { safe: true }), Util.unflatten(input, { safe: true }));
37
+ const merged = mergeDeep({}, doc, Util.unflatten(input, { safe: true }));
38
38
  return this.#resolver.resolve(query.clone({ doc, input: merged }));
39
39
  });
40
40
  }
@@ -164,13 +164,15 @@ module.exports = class Schema {
164
164
  }
165
165
 
166
166
  if (enumKinds.includes(node.kind)) {
167
+ const values = Schema.#resolveNodeValue(node);
168
+
167
169
  target = this.#schema.enums[name] = {
170
+ values,
168
171
  directives: {},
169
172
  pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
170
173
  };
171
174
 
172
175
  // Define (and assign) an Allow pipeline for the enumeration
173
- const values = Schema.#resolveNodeValue(node);
174
176
  Pipeline.define(name, Pipeline.Allow(...values), { configurable: true });
175
177
  target.pipelines.finalize.push(name);
176
178
  }
@@ -360,7 +362,10 @@ module.exports = class Schema {
360
362
  // Merge Enums and Scalar type definitions
361
363
  const enumer = this.#schema.enums[$field.type];
362
364
  const scalar = this.#schema.scalars[$field.type];
363
- if (enumer) Object.entries(enumer.pipelines).forEach(([key, values]) => $field.pipelines[key].push(...values));
365
+ if (enumer) {
366
+ $field.allows = enumer.values;
367
+ Object.entries(enumer.pipelines).forEach(([key, values]) => $field.pipelines[key].push(...values));
368
+ }
364
369
  if (scalar) Object.entries(scalar.pipelines).forEach(([key, values]) => $field.pipelines[key].push(...values));
365
370
 
366
371
  if ($field.isArray) $field.pipelines.normalize.unshift('toArray');
@@ -745,6 +750,7 @@ module.exports = class Schema {
745
750
  [model]: Object.values(model.fields).filter(field => field.model?.isEntity).reduce((prev2, field) => {
746
751
  return Object.assign(prev2, {
747
752
  [field]: (doc, args, context, info) => {
753
+ if (!doc.$) doc = context[schema.namespace].resolver.toResultSet(model, doc); // Ensure resultSet
748
754
  return doc.$.lookup(field).args(args).info(info).resolve(info);
749
755
  },
750
756
  });