@coderich/autograph 0.9.4 → 0.9.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/CHANGELOG.md CHANGED
@@ -3,6 +3,7 @@
3
3
  ## v0.9.x
4
4
  - Subscriptions API
5
5
  - postMutation no longer mutates "doc" and adds "result"
6
+ - Added onDelete defer option
6
7
 
7
8
  ## v0.8.x
8
9
  - Engine 14+
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/autograph",
3
3
  "author": "Richard Livolsi (coderich)",
4
- "version": "0.9.4",
4
+ "version": "0.9.8",
5
5
  "description": "AutoGraph",
6
6
  "keywords": [
7
7
  "graphql",
@@ -4,6 +4,7 @@ const ResultSet = require('../data/ResultSet');
4
4
  const DataLoader = require('../data/DataLoader');
5
5
  const DataTransaction = require('../data/DataTransaction');
6
6
  const QueryBuilder = require('../query/QueryBuilder');
7
+ const { createSystemEvent } = require('../service/event.service');
7
8
 
8
9
  module.exports = class Resolver {
9
10
  constructor(schema, context = {}) {
@@ -36,6 +37,31 @@ module.exports = class Resolver {
36
37
  */
37
38
  raw(model) {
38
39
  return this.toModelEntity(model).raw();
40
+ // const entity = this.toModelEntity(model);
41
+ // const driver = entity.raw();
42
+ // if (!method) return driver;
43
+
44
+ // const resolver = this;
45
+ // const crud = ['get', 'find', 'count'].indexOf(method) > -1 ? 'read' : method;
46
+ // const query = new Query({ model: entity, resolver, crud });
47
+
48
+ // return new Proxy(driver, {
49
+ // get(target, prop, rec) {
50
+ // const value = Reflect.get(target, prop, rec);
51
+
52
+ // if (typeof value === 'function') {
53
+ // return (...args) => {
54
+ // return value.bind(target)(...args).then((result) => {
55
+ // const doc = resolver.toResultSet(model, result);
56
+ // createSystemEvent('Response', { method, query: query.doc(doc) });
57
+ // return result;
58
+ // });
59
+ // };
60
+ // }
61
+
62
+ // return value;
63
+ // },
64
+ // });
39
65
  }
40
66
 
41
67
  /**
@@ -58,8 +84,8 @@ module.exports = class Resolver {
58
84
  default: {
59
85
  // This is needed in SF tests...
60
86
  const key = model.idKey();
61
- const { where } = query.toDriver();
62
- if (Object.prototype.hasOwnProperty.call(where, key) && where[key] == null) return Promise.resolve(null);
87
+ const { where, method } = query.toDriver();
88
+ if (Object.prototype.hasOwnProperty.call(where, key) && where[key] == null) return Promise.resolve(method === 'findMany' ? [] : null);
63
89
 
64
90
  //
65
91
  return this.loaders.get(`${model}`).load(query);
@@ -86,8 +112,22 @@ module.exports = class Resolver {
86
112
  return entity;
87
113
  }
88
114
 
89
- toResultSet(model, data) {
90
- return new ResultSet(new Query({ model: this.toModel(model), resolver: this }), data);
115
+ toResultSet(model, data, method) {
116
+ const crud = ['get', 'find', 'count'].indexOf(method) > -1 ? 'read' : method;
117
+ const query = new Query({ model: this.toModel(model), resolver: this, crud });
118
+ const result = new ResultSet(query, data);
119
+ return createSystemEvent('Response', {
120
+ model,
121
+ crud,
122
+ method,
123
+ result,
124
+ doc: result,
125
+ merged: result,
126
+ resolver: this,
127
+ key: `${method}${model}`,
128
+ context: this.getContext(),
129
+ query: query.doc(result).merged(result),
130
+ }, () => result);
91
131
  }
92
132
 
93
133
  // DataLoader Proxy Methods
@@ -1,7 +1,6 @@
1
- const { get, remove } = require('lodash');
1
+ const { remove } = require('lodash');
2
2
  const Boom = require('../core/Boom');
3
- const { createSystemEvent } = require('../service/event.service');
4
- const { isPlainObject, objectContaining, mergeDeep, hashObject } = require('../service/app.service');
3
+ const { isPlainObject, objectContaining, mergeDeep, map } = require('../service/app.service');
5
4
 
6
5
  exports.paginateResultSet = (rs, first, after, last, before) => {
7
6
  let hasNextPage = false;
@@ -38,68 +37,61 @@ exports.paginateResultSet = (rs, first, after, last, before) => {
38
37
  return { hasNextPage, hasPreviousPage };
39
38
  };
40
39
 
41
- exports.spliceEmbeddedArray = async (query, doc, key, from, to) => {
40
+ /**
41
+ * @param from <Array>
42
+ * @param to <Array>
43
+ */
44
+ exports.spliceEmbeddedArray = (query, doc, key, from, to) => {
42
45
  const { model } = query.toObject();
43
46
  const field = model.getField(key);
44
- if (!field || !field.isArray()) return Promise.reject(Boom.badRequest(`Cannot splice field '${key}'`));
45
-
46
47
  const modelRef = field.getModelRef();
47
- const $from = model.deserialize(query, { [key]: from })[key];
48
- let $to = model.deserialize(query, { [key]: to })[key];
48
+ const op = from && to ? 'edit' : (from ? 'pull' : 'push'); // eslint-disable-line no-nested-ternary
49
+ const promises = [];
49
50
 
50
- // Edit
51
- if (from && to) {
52
- const arr = get(doc, key) || [];
53
- if ($from.length > 1 && $to.length === 1) $to = Array.from($from).fill($to[0]);
51
+ // Can only splice arrays
52
+ if (!field || !field.isArray()) return Promise.reject(Boom.badRequest(`Cannot splice field '${key}'`));
54
53
 
55
- const edits = arr.map((el) => {
56
- return $from.reduce((prev, val, i) => {
57
- if (objectContaining(el, val)) return isPlainObject(prev) ? mergeDeep(prev, $to[i]) : $to[i];
58
- return prev;
59
- }, el);
60
- });
54
+ // We have to deserialize because this normalizes the data (casting etc)
55
+ let $to = model.deserialize(query, { [key]: to })[key] || to;
56
+ const $from = model.deserialize(query, { [key]: from })[key] || from;
61
57
 
62
- if (field.isEmbedded()) {
63
- return Promise.all(edits.map((edit, i) => {
64
- if (hashObject(edit) !== hashObject(arr[i])) {
65
- return createSystemEvent('Mutation', { method: 'update', query: query.clone().model(modelRef).input(edit).doc(doc) }, () => {
66
- edit = modelRef.appendDefaultFields(query, modelRef.appendCreateFields(edit, true));
67
- return modelRef.validate(query, edit).then(() => edit);
68
- });
69
- }
70
-
71
- return Promise.resolve(edit);
72
- })).then((results) => {
73
- return { [key]: mergeDeep(edits, results) };
74
- });
75
- }
58
+ // If it's embedded we need to append default/create fields for insertion
59
+ if ($to && field.isEmbedded()) $to = $to.map(el => modelRef.appendDefaultFields(query, modelRef.appendCreateFields(el, true)));
76
60
 
77
- return { [key]: edits };
78
- }
61
+ // Convenience so the user does not have to explicity type out the same value over and over to replace
62
+ if ($from && $from.length > 1 && $to && $to.length === 1) $to = Array.from($from).fill($to[0]);
79
63
 
80
- // Pull
81
- if (from) {
82
- const data = { [key]: get(doc, key) || [] };
83
- remove(data[key], el => $from.find(val => objectContaining(el, val)));
84
- return data;
85
- }
64
+ // Traverse the document till we find the segment to modify (in place)
65
+ return key.split('.').reduce((prev, segment, i, arr) => {
66
+ if (prev == null) return prev;
86
67
 
87
- // Push
88
- if (to) {
89
- if (field.isEmbedded()) {
90
- return Promise.all($to.map((input) => {
91
- return createSystemEvent('Mutation', { method: 'create', query: query.clone().model(modelRef).input(input).doc(doc) }, () => {
92
- input = modelRef.appendDefaultFields(query, modelRef.appendCreateFields(input, true));
93
- return modelRef.validate(query, input).then(() => input);
94
- });
95
- })).then((results) => {
96
- return { [key]: (get(doc, key) || []).concat(...results) };
97
- });
98
- }
68
+ return map(prev, (data) => {
69
+ if (i < (arr.length - 1)) return data[segment]; // We have not found the target segment yet
70
+ data[segment] = data[segment] || []; // Ensuring target segment is an array
99
71
 
100
- return { [key]: (get(doc, key) || []).concat($to) };
101
- }
72
+ switch (op) {
73
+ case 'edit': {
74
+ data[segment].forEach((el, j) => {
75
+ $from.forEach((val, k) => {
76
+ if (objectContaining(el, val)) data[segment][j] = isPlainObject(el) ? mergeDeep(el, $to[k]) : $to[k];
77
+ });
78
+ });
79
+ break;
80
+ }
81
+ case 'push': {
82
+ data[segment].push(...$to);
83
+ break;
84
+ }
85
+ case 'pull': {
86
+ remove(data[segment], el => $from.find(val => objectContaining(el, val)));
87
+ break;
88
+ }
89
+ default: {
90
+ break;
91
+ }
92
+ }
102
93
 
103
- // Should never get here
104
- return Promise.reject(new Error('Invalid spliceEmbeddedArray'));
94
+ return Promise.all(promises);
95
+ });
96
+ }, doc);
105
97
  };
@@ -4,6 +4,7 @@ const { map, ensureArray, keyPaths, mapPromise, toGUID, hashObject } = require('
4
4
 
5
5
  module.exports = class ResultSet {
6
6
  constructor(query, data, adjustForPagination = true) {
7
+ if (data == null) return data;
7
8
  const { resolver, model, sort, first, after, last, before } = query.toObject();
8
9
  const fields = model.getFields().filter(f => f.getName() !== 'id');
9
10
 
@@ -175,4 +175,9 @@ module.exports = class Field extends Node {
175
175
  type = this.isArray() ? `[${type}${this.isArrayElementRequired() ? '!' : ''}]` : type;
176
176
  return `${type}${req}`;
177
177
  }
178
+
179
+ getSubscriptionType() {
180
+ if (this.isFKReference()) return this.isArray() ? '[ID]' : 'ID';
181
+ return this.getGQLType();
182
+ }
178
183
  };
@@ -90,7 +90,7 @@ module.exports = (schema) => {
90
90
  }
91
91
 
92
92
  type ${model.getName()}SubscriptionPayloadEventData {
93
- ${getGQLWhereFields(model).map(field => `${field.getName()}: ${field.isFKReference() ? 'ID' : field.getGQLType()}`)}
93
+ ${getGQLWhereFields(model).map(field => `${field.getName()}: ${field.getSubscriptionType()}`)}
94
94
  }
95
95
 
96
96
  interface ${model.getName()}SubscriptionQuery {
@@ -11,7 +11,7 @@ module.exports = (schema) => {
11
11
  enum AutoGraphTransformEnum { ${Object.keys(Transformer.getInstances()).join(' ')} }
12
12
  enum AutoGraphAuthzEnum { private protected public }
13
13
  enum AutoGraphValueScopeEnum { self context }
14
- enum AutoGraphOnDeleteEnum { cascade nullify restrict }
14
+ enum AutoGraphOnDeleteEnum { cascade nullify restrict defer }
15
15
  enum AutoGraphIndexEnum { unique }
16
16
 
17
17
  directive @model(
@@ -78,7 +78,7 @@ module.exports = class QueryBuilder {
78
78
  break;
79
79
  }
80
80
  case 'push': case 'pull': case 'splice': {
81
- crud = 'update'; // Your logics wants this to be a simple "update". Sub documents systemEvents will emit either "create" or "udpate"
81
+ crud = 'update'; // Your logic wants this to be a simple "update". Sub documents systemEvents will emit either "create" or "udpate"
82
82
  method = id ? `${cmd}One` : `${cmd}Many`;
83
83
  break;
84
84
  }
@@ -130,17 +130,33 @@ module.exports = class QueryResolver {
130
130
  });
131
131
  }
132
132
 
133
+ spliceOne(query) {
134
+ const { args } = query.toObject();
135
+ const [key, ...values] = args;
136
+ return this.splice(query.args([key, ...values]));
137
+ }
138
+
139
+ spliceMany(query) {
140
+ const { model, match, transaction, args, flags } = query.toObject();
141
+ const [key, ...values] = args;
142
+
143
+ return this.resolver.match(model).match(match).flags(flags).many().then((docs) => {
144
+ const txn = this.resolver.transaction(transaction);
145
+ docs.forEach(doc => txn.match(model).id(doc.id).splice(key, ...values));
146
+ return txn.run();
147
+ });
148
+ }
149
+
133
150
  splice(query) {
134
151
  const { model, match, args, flags } = query.toObject();
135
152
  const [key, from, to] = args;
136
153
 
137
154
  return this.resolver.match(model).match(match).flags(flags).one({ required: true }).then(async (doc) => {
138
- const data = await DataService.spliceEmbeddedArray(query, doc, key, from, to);
139
- const merged = mergeDeep(doc, data);
155
+ await DataService.spliceEmbeddedArray(query, doc, key, from, to);
140
156
 
141
- return createSystemEvent('Mutation', { method: 'splice', query: query.doc(doc).input(data).merged(merged) }, async () => {
142
- await model.validate(query, data);
143
- const $doc = mergeDeep(model.serialize(query, doc, true), model.serialize(query, data, true));
157
+ return createSystemEvent('Mutation', { method: 'splice', query: query.doc(doc).merged(doc) }, async () => {
158
+ await model.validate(query, doc);
159
+ const $doc = model.serialize(query, doc, true);
144
160
  return this.resolver.resolve(query.method('updateOne').doc(doc).$doc($doc));
145
161
  });
146
162
  });
@@ -111,6 +111,11 @@ exports.resolveReferentialIntegrity = (query) => {
111
111
  txn.match(ref).where($where).flags(flags).count().then(count => (count ? reject(new Error('Restricted')) : count));
112
112
  break;
113
113
  }
114
+ case 'defer': {
115
+ // Defer to the embedded object
116
+ // Marks the field as an onDelete candidate otherwise it (and the embedded object) will get skipped
117
+ break;
118
+ }
114
119
  default: throw new Error(`Unknown onDelete operator: '${op}'`);
115
120
  }
116
121
  });