@coderich/autograph 0.9.3 → 0.9.7
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 +1 -0
- package/package.json +1 -1
- package/src/core/Resolver.js +44 -4
- package/src/core/Schema.js +2 -2
- package/src/data/DataService.js +48 -56
- package/src/data/ResultSet.js +1 -0
- package/src/graphql/ast/Schema.js +3 -3
- package/src/graphql/extension/framework.js +1 -1
- package/src/query/QueryBuilder.js +1 -1
- package/src/query/QueryResolver.js +21 -5
- package/src/query/QueryService.js +5 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/core/Resolver.js
CHANGED
|
@@ -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
|
-
|
|
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
|
package/src/core/Schema.js
CHANGED
|
@@ -50,8 +50,8 @@ module.exports = class extends Schema {
|
|
|
50
50
|
this.modelsByKey = this.models.reduce((prev, model) => Object.assign(prev, { [model.getKey()]: model }), {});
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
loadDir(dir) {
|
|
54
|
-
super.loadDir(dir);
|
|
53
|
+
loadDir(dir, options) {
|
|
54
|
+
super.loadDir(dir, options);
|
|
55
55
|
this.createModels();
|
|
56
56
|
return this;
|
|
57
57
|
}
|
package/src/data/DataService.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { remove } = require('lodash');
|
|
2
2
|
const Boom = require('../core/Boom');
|
|
3
|
-
const {
|
|
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
|
-
|
|
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
|
|
48
|
-
|
|
48
|
+
const op = from && to ? 'edit' : (from ? 'pull' : 'push'); // eslint-disable-line no-nested-ternary
|
|
49
|
+
const promises = [];
|
|
49
50
|
|
|
50
|
-
//
|
|
51
|
-
if (
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
63
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
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
|
-
|
|
104
|
-
|
|
94
|
+
return Promise.all(promises);
|
|
95
|
+
});
|
|
96
|
+
}, doc);
|
|
105
97
|
};
|
package/src/data/ResultSet.js
CHANGED
|
@@ -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
|
|
|
@@ -106,12 +106,12 @@ module.exports = class Schema extends Node {
|
|
|
106
106
|
return this.schema.context;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
loadDir(dir) {
|
|
109
|
+
loadDir(dir, options) {
|
|
110
110
|
// Typedefs
|
|
111
|
-
const typeDefs = Glob.sync(`${dir}/**/*.{gql,graphql}
|
|
111
|
+
const typeDefs = Glob.sync(`${dir}/**/*.{gql,graphql}`, options).map(file => loadFile(file)).join('\n\n');
|
|
112
112
|
|
|
113
113
|
// Possibly full schema definitions
|
|
114
|
-
const schema = Glob.sync(`${dir}/**/*.js
|
|
114
|
+
const schema = Glob.sync(`${dir}/**/*.js`, options).map(file => reqFile(file)).reduce((prev, data) => {
|
|
115
115
|
return Merge(prev, data);
|
|
116
116
|
}, {
|
|
117
117
|
typeDefs: typeDefs.length ? typeDefs : undefined,
|
|
@@ -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
|
|
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
|
-
|
|
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).
|
|
142
|
-
await model.validate(query,
|
|
143
|
-
const $doc =
|
|
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
|
});
|