@coderich/autograph 0.13.32 → 0.13.34

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.34",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -161,9 +161,9 @@ module.exports = class Pipeline {
161
161
  static resolve(params, pipeline) {
162
162
  const transformers = params.field.pipelines[pipeline] || [];
163
163
 
164
- return Util.pipeline(transformers.map(t => (value) => {
165
- return Pipeline[t]({ ...params, value });
166
- }), params.value);
164
+ return transformers.reduce((value, t) => {
165
+ return Util.uvl(Pipeline[t]({ ...params, value }), value);
166
+ }, params.value);
167
167
  }
168
168
  };
169
169
 
@@ -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,65 @@
1
+ const Util = require('@coderich/util');
2
+
3
+ // { query, path: path.concat(key), context: this.#context }
4
+
5
+ module.exports = class Transformer {
6
+ #config = {
7
+ args: {},
8
+ shape: {},
9
+ defaults: {},
10
+ operation: 'set',
11
+ };
12
+
13
+ #operations = {
14
+ get: {
15
+ get: () => {
16
+
17
+ },
18
+ },
19
+ set: {
20
+ set: (target, prop, startValue) => {
21
+ const transforms = this.#config.shape[prop] ?? [];
22
+
23
+ const result = transforms.reduce((value, t) => {
24
+ if (typeof t === 'function') return Util.uvl(t({ startValue, value, ...this.#config.args }), value);
25
+ prop = t;
26
+ return value;
27
+ }, startValue);
28
+
29
+ target[prop] = result;
30
+ return true;
31
+ },
32
+ },
33
+ };
34
+
35
+ #operation;
36
+
37
+ /**
38
+ * Allowing construction of object before knowing full configuration
39
+ */
40
+ constructor(config = {}) {
41
+ this.config(config);
42
+ }
43
+
44
+ /**
45
+ * Re-assign configuration after instantiation
46
+ */
47
+ config(config = {}) {
48
+ Object.assign(this.#config, config);
49
+ this.#operation = this.#operations[this.#config.operation];
50
+ return this;
51
+ }
52
+
53
+ /**
54
+ * Re-assign args after instantiation
55
+ */
56
+ args(args = {}) {
57
+ Object.assign(this.#config.args, args);
58
+ return this;
59
+ }
60
+
61
+ transform(mixed, args) {
62
+ this.args(args);
63
+ return Util.map(mixed, data => Object.assign(new Proxy({}, this.#operation), this.#config.defaults, data));
64
+ }
65
+ };
@@ -64,6 +64,7 @@ module.exports = class Query {
64
64
  */
65
65
  transform(asClone = true) {
66
66
  return Promise.all([
67
+ // this.#model.transformers.input.transform(this.#query.input),
67
68
  this.pipeline('input', this.#query.input),
68
69
  this.#query.isNative ? this.#query.where : this.pipeline('where', this.#query.where ?? {}),
69
70
  this.pipeline('sort', this.#query.sort),
@@ -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,10 @@ 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: {
148
+ input: new Transformer(),
149
+ doc: new Transformer({ args: { model, schema: this.#schema } }),
150
+ },
146
151
  directives: {},
147
152
  toString: () => name,
148
153
  };
@@ -153,6 +158,9 @@ module.exports = class Schema {
153
158
  name,
154
159
  key: name,
155
160
  pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
161
+ transformers: {
162
+ input: new Transformer({ args: { model, field, schema: this.#schema } }),
163
+ },
156
164
  directives: {},
157
165
  toString: () => name,
158
166
  };
@@ -341,6 +349,31 @@ module.exports = class Schema {
341
349
  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
350
  where: Object.values($model.fields).filter(f => f.pipelines.instruct.length).reduce((prev, f) => Object.assign(prev, { [f.name]: undefined }), {}),
343
351
  };
352
+
353
+ $model.transformers.input.config({
354
+ shape: Object.values($model.fields).reduce((prev, curr) => {
355
+ const rules = [
356
+ a => Pipeline.$default({ ...a, field: curr }),
357
+ a => Pipeline.$cast({ ...a, field: curr }),
358
+ a => Pipeline.$normalize({ ...a, field: curr }),
359
+ a => Pipeline.$instruct({ ...a, field: curr }),
360
+ // a => Pipeline.$finalize({ ...a, field: curr }),
361
+ ];
362
+ // if (curr.isEmbedded) rules.push(a => Util.map(a.value, value => curr.model.transformers.input.transform(value)));
363
+ if (curr.isEmbedded) rules.push(a => curr.model.transformers.input.transform(a.value));
364
+ // rules.push(curr.key); // rename
365
+ return Object.assign(prev, { [curr.name]: rules });
366
+ }, {}),
367
+ });
368
+
369
+ $model.transformers.doc.config({
370
+ shape: Object.values($model.fields).reduce((prev, curr) => {
371
+ const rules = [curr.name]; // Rename key
372
+ if (curr.isArray) rules.unshift(({ value }) => (value == null ? value : Util.ensureArray(value)));
373
+ if (curr.isEmbedded) rules.unshift(({ value }) => Util.map(value, v => curr.model.transformers.doc.transform(v)));
374
+ return Object.assign(prev, { [curr.key]: rules });
375
+ }, {}),
376
+ });
344
377
  });
345
378
  } else if (node.kind === Kind.FIELD_DEFINITION) {
346
379
  const $field = field;