@coderich/autograph 0.13.100 → 0.13.101

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.100",
4
+ "version": "0.13.101",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -5,6 +5,7 @@ module.exports = class Transaction {
5
5
  #context;
6
6
  #resolver;
7
7
  #sourceMap;
8
+ #queries = [];
8
9
 
9
10
  constructor(config) {
10
11
  this.#schema = config.schema;
@@ -24,13 +25,15 @@ module.exports = class Transaction {
24
25
  }));
25
26
  }
26
27
 
27
- return new QueryResolverTransaction({
28
+ this.#queries.push(new QueryResolverTransaction({
28
29
  resolver: this.#resolver,
29
30
  schema: this.#schema,
30
31
  context: this.#context,
31
32
  transaction: this.#sourceMap.get(client),
32
33
  query: { model: `${model}` },
33
- });
34
+ }));
35
+
36
+ return this.#queries.at(-1);
34
37
  }
35
38
 
36
39
  commit() {
@@ -42,8 +45,10 @@ module.exports = class Transaction {
42
45
  }
43
46
 
44
47
  #close(op) {
45
- return Promise.all(Array.from(this.#sourceMap.entries()).map(([client, promise]) => {
46
- return promise.then(transaction => transaction[op]());
47
- }));
48
+ return Promise.all(this.#queries.map(q => q.promise())).then(() => {
49
+ return Promise.all(Array.from(this.#sourceMap.entries()).map(([client, promise]) => {
50
+ return promise.then(transaction => transaction[op]());
51
+ }));
52
+ });
48
53
  }
49
54
  };
@@ -1,7 +1,7 @@
1
1
  const get = require('lodash.get');
2
2
  const Util = require('@coderich/util');
3
3
  const QueryBuilder = require('./QueryBuilder');
4
- const { mergeDeep } = require('../service/AppService');
4
+ const { mergeDeep, withResolvers } = require('../service/AppService');
5
5
 
6
6
  module.exports = class QueryResolver extends QueryBuilder {
7
7
  #model;
@@ -9,6 +9,7 @@ module.exports = class QueryResolver extends QueryBuilder {
9
9
  #config;
10
10
  #context;
11
11
  #resolver;
12
+ #resolution = withResolvers(); // Promise for when the query is resolved
12
13
 
13
14
  constructor(config) {
14
15
  const { schema, context, resolver, query } = config;
@@ -20,7 +21,21 @@ module.exports = class QueryResolver extends QueryBuilder {
20
21
  this.#model = schema.models[query.model];
21
22
  }
22
23
 
24
+ promise() {
25
+ return this.#resolution.promise;
26
+ }
27
+
23
28
  terminate() {
29
+ return this.#terminate().then((result) => {
30
+ this.#resolution.resolve(result);
31
+ return result;
32
+ }).catch((e) => {
33
+ this.#resolution.reject(e);
34
+ return Promise.reject(e);
35
+ });
36
+ }
37
+
38
+ #terminate() {
24
39
  const query = super.terminate();
25
40
  const { op, args: { input } } = query.toObject();
26
41
 
@@ -101,7 +116,7 @@ module.exports = class QueryResolver extends QueryBuilder {
101
116
  });
102
117
  }
103
118
  default: {
104
- throw new Error(`Unknown operation "${op}"`);
119
+ return Promise.reject(new Error(`Unknown operation "${op}"`));
105
120
  }
106
121
  }
107
122
  }
@@ -44,3 +44,9 @@ exports.JSONParse = (mixed) => {
44
44
  return undefined;
45
45
  }
46
46
  };
47
+
48
+ exports.withResolvers = () => {
49
+ let resolve, reject;
50
+ const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
51
+ return { promise, resolve, reject };
52
+ };