@coderich/autograph 0.13.32 → 0.13.33

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.32",
4
+ "version": "0.13.33",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -1,7 +1,6 @@
1
1
  const { graphql } = require('graphql');
2
2
  const Boom = require('@hapi/boom');
3
3
  const Util = require('@coderich/util');
4
- const Pipeline = require('./Pipeline');
5
4
  const Emitter = require('./Emitter');
6
5
  const Loader = require('./Loader');
7
6
  const DataLoader = require('./DataLoader');
@@ -221,14 +220,10 @@ module.exports = class Resolver {
221
220
  const self = this;
222
221
  if (result == null) return result;
223
222
  if (typeof result !== 'object') return result;
223
+ model = this.#schema.models[model];
224
+
224
225
  return Object.defineProperties(Util.map(result, (doc) => {
225
- // Transform result to domain model
226
- const $doc = this.#schema.models[model].walk(doc, (node) => {
227
- if (node.value === undefined) return undefined;
228
- if (node.value != null && node.field.isArray) node.value = Util.ensureArray(node.value);
229
- node.key = node.field.name;
230
- return node;
231
- }, { key: 'key' });
226
+ const $doc = model.transformers.doc.transform(doc);
232
227
 
233
228
  // Assign useful/needed meta data
234
229
  return Object.defineProperties($doc, {
@@ -0,0 +1,47 @@
1
+ const Util = require('@coderich/util');
2
+
3
+ module.exports = class Transformer {
4
+ #config;
5
+ #operation;
6
+ #operations = {
7
+ get: {
8
+ get: () => {
9
+
10
+ },
11
+ },
12
+ set: {
13
+ set: (target, prop, value) => {
14
+ const transforms = this.#config.shape[prop] ?? [];
15
+ const $value = transforms.reduce((prev, t) => {
16
+ if (typeof t === 'function') return t(prev);
17
+ prop = t;
18
+ return prev;
19
+ }, value);
20
+ target[prop] = $value;
21
+ return true;
22
+ },
23
+ },
24
+ };
25
+
26
+ /**
27
+ * Allowing construction of object before knowing configuration
28
+ */
29
+ constructor(config = {}) {
30
+ this.config(config);
31
+ }
32
+
33
+ /**
34
+ * Re-assign configuration after instantiation
35
+ */
36
+ config(config = {}) {
37
+ this.#config = config;
38
+ this.#config.shape ??= {};
39
+ this.#config.defaults ??= {};
40
+ this.#config.operation ??= 'set';
41
+ this.#operation = this.#operations[this.#config.operation];
42
+ }
43
+
44
+ transform(mixed) {
45
+ return Util.map(mixed, data => Object.assign(new Proxy({}, this.#operation), this.#config.defaults, data));
46
+ }
47
+ };
@@ -4,6 +4,7 @@ const Util = require('@coderich/util');
4
4
  const { Kind, parse, visit } = require('graphql');
5
5
  const { mergeTypeDefs, mergeFields, mergeDirectives } = require('@graphql-tools/merge');
6
6
  const { isLeafValue, mergeDeep, fromGUID } = require('../service/AppService');
7
+ const Transformer = require('../data/Transformer');
7
8
  const Pipeline = require('../data/Pipeline');
8
9
  const Emitter = require('../data/Emitter');
9
10
 
@@ -143,6 +144,7 @@ module.exports = class Schema {
143
144
  loader: this.#config.dataLoaders?.default,
144
145
  generator: this.#config.generators?.default,
145
146
  pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
147
+ transformers: { doc: new Transformer() },
146
148
  directives: {},
147
149
  toString: () => name,
148
150
  };
@@ -153,6 +155,7 @@ module.exports = class Schema {
153
155
  name,
154
156
  key: name,
155
157
  pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
158
+ transformers: { doc: new Transformer() },
156
159
  directives: {},
157
160
  toString: () => name,
158
161
  };
@@ -341,6 +344,15 @@ module.exports = class Schema {
341
344
  input: Object.values($model.fields).filter(f => f.defaultValue !== undefined || inputPipelines.some(k => f.pipelines[k].length)).reduce((prev, f) => Object.assign(prev, { [f.name]: undefined }), {}),
342
345
  where: Object.values($model.fields).filter(f => f.pipelines.instruct.length).reduce((prev, f) => Object.assign(prev, { [f.name]: undefined }), {}),
343
346
  };
347
+
348
+ $model.transformers.doc.config({
349
+ shape: Object.values($model.fields).reduce((prev, curr) => {
350
+ const rules = [curr.name]; // Rename key
351
+ if (curr.isArray) rules.unshift(v => (v == null ? v : Util.ensureArray(v)));
352
+ if (curr.isEmbedded) rules.unshift(v => Util.map(v, el => curr.model.transformers.doc.transform(el)));
353
+ return Object.assign(prev, { [curr.key]: rules });
354
+ }, {}),
355
+ });
344
356
  });
345
357
  } else if (node.kind === Kind.FIELD_DEFINITION) {
346
358
  const $field = field;