@snowtop/ent 0.1.0-alpha15 → 0.1.0-alpha150

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.
Files changed (170) hide show
  1. package/action/action.d.ts +27 -16
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +16 -3
  4. package/action/executor.js +90 -23
  5. package/action/experimental_action.d.ts +25 -16
  6. package/action/experimental_action.js +35 -9
  7. package/action/index.d.ts +3 -1
  8. package/action/index.js +7 -1
  9. package/action/operations.d.ts +125 -0
  10. package/action/operations.js +684 -0
  11. package/action/orchestrator.d.ts +38 -12
  12. package/action/orchestrator.js +427 -102
  13. package/action/relative_value.d.ts +47 -0
  14. package/action/relative_value.js +125 -0
  15. package/action/transaction.d.ts +10 -0
  16. package/action/transaction.js +23 -0
  17. package/auth/auth.d.ts +1 -1
  18. package/core/base.d.ts +51 -21
  19. package/core/base.js +7 -1
  20. package/core/clause.d.ts +85 -40
  21. package/core/clause.js +375 -64
  22. package/core/config.d.ts +15 -1
  23. package/core/config.js +10 -1
  24. package/core/const.d.ts +3 -0
  25. package/core/const.js +6 -0
  26. package/core/context.d.ts +4 -2
  27. package/core/context.js +20 -2
  28. package/core/convert.d.ts +1 -1
  29. package/core/date.js +1 -5
  30. package/core/db.d.ts +12 -8
  31. package/core/db.js +18 -8
  32. package/core/ent.d.ts +68 -94
  33. package/core/ent.js +535 -586
  34. package/core/global_schema.d.ts +7 -0
  35. package/core/global_schema.js +51 -0
  36. package/core/loaders/assoc_count_loader.d.ts +1 -0
  37. package/core/loaders/assoc_count_loader.js +10 -2
  38. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  39. package/core/loaders/assoc_edge_loader.js +10 -13
  40. package/core/loaders/index.d.ts +1 -1
  41. package/core/loaders/index.js +1 -3
  42. package/core/loaders/index_loader.d.ts +3 -3
  43. package/core/loaders/loader.d.ts +2 -2
  44. package/core/loaders/loader.js +5 -5
  45. package/core/loaders/object_loader.d.ts +30 -9
  46. package/core/loaders/object_loader.js +225 -78
  47. package/core/loaders/query_loader.d.ts +6 -12
  48. package/core/loaders/query_loader.js +54 -13
  49. package/core/loaders/raw_count_loader.js +5 -1
  50. package/core/logger.d.ts +1 -1
  51. package/core/logger.js +1 -0
  52. package/core/privacy.d.ts +7 -6
  53. package/core/privacy.js +21 -25
  54. package/core/query/assoc_query.d.ts +3 -2
  55. package/core/query/assoc_query.js +9 -1
  56. package/core/query/custom_clause_query.d.ts +27 -0
  57. package/core/query/custom_clause_query.js +88 -0
  58. package/core/query/custom_query.d.ts +17 -2
  59. package/core/query/custom_query.js +88 -13
  60. package/core/query/index.d.ts +1 -0
  61. package/core/query/index.js +3 -1
  62. package/core/query/query.d.ts +15 -3
  63. package/core/query/query.js +128 -53
  64. package/core/query/shared_assoc_test.d.ts +2 -1
  65. package/core/query/shared_assoc_test.js +44 -54
  66. package/core/query/shared_test.d.ts +8 -1
  67. package/core/query/shared_test.js +532 -236
  68. package/core/viewer.d.ts +2 -0
  69. package/core/viewer.js +3 -1
  70. package/graphql/graphql.d.ts +52 -19
  71. package/graphql/graphql.js +174 -136
  72. package/graphql/graphql_field_helpers.d.ts +7 -1
  73. package/graphql/graphql_field_helpers.js +21 -1
  74. package/graphql/index.d.ts +2 -2
  75. package/graphql/index.js +3 -5
  76. package/graphql/query/connection_type.d.ts +9 -9
  77. package/graphql/query/shared_assoc_test.js +1 -1
  78. package/graphql/query/shared_edge_connection.js +1 -19
  79. package/graphql/scalars/orderby_direction.d.ts +2 -0
  80. package/graphql/scalars/orderby_direction.js +15 -0
  81. package/imports/dataz/example1/_auth.js +128 -47
  82. package/imports/dataz/example1/_viewer.js +87 -39
  83. package/imports/index.d.ts +7 -2
  84. package/imports/index.js +20 -5
  85. package/index.d.ts +14 -5
  86. package/index.js +26 -10
  87. package/package.json +18 -17
  88. package/parse_schema/parse.d.ts +31 -9
  89. package/parse_schema/parse.js +179 -32
  90. package/schema/base_schema.d.ts +13 -3
  91. package/schema/base_schema.js +13 -0
  92. package/schema/field.d.ts +78 -21
  93. package/schema/field.js +231 -71
  94. package/schema/index.d.ts +2 -2
  95. package/schema/index.js +7 -2
  96. package/schema/json_field.d.ts +16 -4
  97. package/schema/json_field.js +32 -2
  98. package/schema/schema.d.ts +109 -20
  99. package/schema/schema.js +42 -53
  100. package/schema/struct_field.d.ts +15 -3
  101. package/schema/struct_field.js +117 -22
  102. package/schema/union_field.d.ts +1 -1
  103. package/scripts/custom_compiler.js +12 -8
  104. package/scripts/custom_graphql.js +145 -34
  105. package/scripts/migrate_v0.1.js +36 -0
  106. package/scripts/move_types.js +120 -0
  107. package/scripts/read_schema.js +22 -7
  108. package/testutils/action/complex_schemas.d.ts +69 -0
  109. package/testutils/action/complex_schemas.js +405 -0
  110. package/testutils/builder.d.ts +39 -43
  111. package/testutils/builder.js +75 -49
  112. package/testutils/db/fixture.d.ts +10 -0
  113. package/testutils/db/fixture.js +26 -0
  114. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +32 -8
  115. package/testutils/db/{test_db.js → temp_db.js} +244 -48
  116. package/testutils/db/value.d.ts +7 -0
  117. package/testutils/db/value.js +251 -0
  118. package/testutils/db_mock.d.ts +16 -4
  119. package/testutils/db_mock.js +52 -9
  120. package/testutils/db_time_zone.d.ts +4 -0
  121. package/testutils/db_time_zone.js +41 -0
  122. package/testutils/ent-graphql-tests/index.d.ts +7 -1
  123. package/testutils/ent-graphql-tests/index.js +56 -26
  124. package/testutils/fake_comms.js +1 -1
  125. package/testutils/fake_data/const.d.ts +2 -1
  126. package/testutils/fake_data/const.js +3 -0
  127. package/testutils/fake_data/fake_contact.d.ts +7 -3
  128. package/testutils/fake_data/fake_contact.js +13 -7
  129. package/testutils/fake_data/fake_event.d.ts +4 -1
  130. package/testutils/fake_data/fake_event.js +7 -6
  131. package/testutils/fake_data/fake_tag.d.ts +36 -0
  132. package/testutils/fake_data/fake_tag.js +89 -0
  133. package/testutils/fake_data/fake_user.d.ts +8 -5
  134. package/testutils/fake_data/fake_user.js +16 -15
  135. package/testutils/fake_data/index.js +5 -1
  136. package/testutils/fake_data/internal.d.ts +2 -0
  137. package/testutils/fake_data/internal.js +7 -1
  138. package/testutils/fake_data/tag_query.d.ts +13 -0
  139. package/testutils/fake_data/tag_query.js +43 -0
  140. package/testutils/fake_data/test_helpers.d.ts +11 -4
  141. package/testutils/fake_data/test_helpers.js +29 -13
  142. package/testutils/fake_data/user_query.d.ts +11 -4
  143. package/testutils/fake_data/user_query.js +54 -22
  144. package/testutils/fake_log.js +1 -1
  145. package/testutils/parse_sql.d.ts +6 -0
  146. package/testutils/parse_sql.js +16 -2
  147. package/testutils/test_edge_global_schema.d.ts +15 -0
  148. package/testutils/test_edge_global_schema.js +62 -0
  149. package/testutils/write.d.ts +2 -2
  150. package/testutils/write.js +33 -7
  151. package/tsc/ast.d.ts +25 -2
  152. package/tsc/ast.js +141 -17
  153. package/tsc/compilerOptions.js +5 -1
  154. package/tsc/move_generated.d.ts +1 -0
  155. package/tsc/move_generated.js +164 -0
  156. package/tsc/transform.d.ts +22 -0
  157. package/tsc/transform.js +182 -0
  158. package/tsc/transform_action.d.ts +22 -0
  159. package/tsc/transform_action.js +183 -0
  160. package/tsc/transform_ent.d.ts +17 -0
  161. package/tsc/transform_ent.js +60 -0
  162. package/tsc/transform_schema.d.ts +27 -0
  163. package/{scripts → tsc}/transform_schema.js +146 -117
  164. package/graphql/enums.d.ts +0 -3
  165. package/graphql/enums.js +0 -25
  166. package/scripts/move_generated.js +0 -142
  167. package/scripts/transform_code.js +0 -113
  168. package/scripts/transform_schema.d.ts +0 -1
  169. /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
  170. /package/scripts/{transform_code.d.ts → move_types.d.ts} +0 -0
@@ -1,44 +1,51 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StructListType = exports.StructType = exports.StructField = void 0;
3
+ exports.StructTypeAsList = exports.StructListType = exports.StructType = exports.StructField = void 0;
4
+ const camel_case_1 = require("camel-case");
4
5
  const field_1 = require("./field");
5
6
  const schema_1 = require("./schema");
6
- const camel_case_1 = require("camel-case");
7
+ const global_schema_1 = require("../core/global_schema");
8
+ const logger_1 = require("../core/logger");
7
9
  class StructField extends field_1.BaseField {
8
- constructor(options) {
10
+ constructor(options, jsonAsList) {
9
11
  super();
10
12
  this.options = options;
13
+ this.jsonAsList = jsonAsList;
11
14
  this.type = {
12
15
  dbType: schema_1.DBType.JSONB,
13
16
  };
14
17
  this.type.subFields = options.fields;
15
18
  this.type.type = options.tsType;
16
19
  this.type.graphQLType = options.graphQLType || options.tsType;
20
+ this.type.globalType = this.options.globalType;
17
21
  if (options.jsonNotJSONB) {
18
22
  this.type.dbType = schema_1.DBType.JSON;
19
23
  }
24
+ if (jsonAsList) {
25
+ this.type.listElemType = {
26
+ dbType: schema_1.DBType.JSONB,
27
+ };
28
+ }
20
29
  }
21
- // right now, we store things in the db in lowerCase format
22
- // this will lead to issues if field changes.
23
- // TODO: use storageKey and convert back...
24
- format(obj, nested) {
30
+ formatImpl(obj, nested) {
25
31
  if (!(obj instanceof Object)) {
26
32
  throw new Error("valid was not called");
27
33
  }
28
34
  let ret = {};
29
35
  for (const k in this.options.fields) {
36
+ const field = this.options.fields[k];
37
+ // check two values
38
+ // store in dbKey format
30
39
  // TODO more #510
31
- let dbKey = (0, camel_case_1.camelCase)(k);
32
- let val = obj[dbKey];
33
- // for tests with snake_case
34
- if (val === undefined && obj[k] !== undefined) {
35
- val = obj[k];
36
- dbKey = k;
40
+ let dbKey = (0, schema_1.getStorageKey)(field, k);
41
+ let camelKey = (0, camel_case_1.camelCase)(k);
42
+ let val = obj[camelKey];
43
+ if (val === undefined && obj[dbKey] !== undefined) {
44
+ val = obj[dbKey];
37
45
  }
38
46
  if (val === undefined) {
39
47
  continue;
40
48
  }
41
- const field = this.options.fields[k];
42
49
  if (field.format) {
43
50
  // indicate nested so this isn't JSON stringified
44
51
  ret[dbKey] = field.format(val, true);
@@ -47,13 +54,53 @@ class StructField extends field_1.BaseField {
47
54
  ret[dbKey] = val;
48
55
  }
49
56
  }
50
- // don't json.stringify if nested
57
+ // don't json.stringify if nested or list
51
58
  if (nested) {
52
59
  return ret;
53
60
  }
54
61
  return JSON.stringify(ret);
55
62
  }
56
- async valid(obj) {
63
+ format(obj, nested) {
64
+ if (this.type.globalType) {
65
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
66
+ if (f && f.format) {
67
+ if (JSON.stringify(this.type.listElemType) !==
68
+ JSON.stringify(f?.type.listElemType)) {
69
+ if (this.jsonAsList) {
70
+ // handle as nested
71
+ // @ts-ignore
72
+ const formatted = obj.map((v) => f.format(v, true));
73
+ if (nested) {
74
+ return formatted;
75
+ }
76
+ else {
77
+ return JSON.stringify(formatted);
78
+ }
79
+ }
80
+ else {
81
+ const formatted = f.format([obj], true);
82
+ if (nested) {
83
+ return formatted[0];
84
+ }
85
+ else {
86
+ return JSON.stringify(formatted[0]);
87
+ }
88
+ }
89
+ }
90
+ // TODO handle format code
91
+ return f.format(obj);
92
+ }
93
+ }
94
+ if (Array.isArray(obj) && this.jsonAsList) {
95
+ const ret = obj.map((v) => this.formatImpl(v, true));
96
+ if (nested) {
97
+ return ret;
98
+ }
99
+ return JSON.stringify(ret);
100
+ }
101
+ return this.formatImpl(obj, nested);
102
+ }
103
+ async validImpl(obj) {
57
104
  if (!(obj instanceof Object)) {
58
105
  return false;
59
106
  }
@@ -63,12 +110,11 @@ class StructField extends field_1.BaseField {
63
110
  for (const k in this.options.fields) {
64
111
  const field = this.options.fields[k];
65
112
  // TODO more #510
66
- let dbKey = (0, camel_case_1.camelCase)(k);
67
- let val = obj[dbKey];
68
- // for tests with snake_case
69
- if (val === undefined && obj[k] !== undefined) {
70
- val = obj[k];
71
- dbKey = k;
113
+ let dbKey = (0, schema_1.getStorageKey)(field, k);
114
+ let camelKey = (0, camel_case_1.camelCase)(k);
115
+ let val = obj[camelKey];
116
+ if (val === undefined && obj[dbKey] !== undefined) {
117
+ val = obj[dbKey];
72
118
  }
73
119
  if (val === undefined || val === null) {
74
120
  // nullable, nothing to do here
@@ -89,6 +135,47 @@ class StructField extends field_1.BaseField {
89
135
  const ret = await Promise.all(promises);
90
136
  return ret.every((v) => v);
91
137
  }
138
+ async valid(obj) {
139
+ if (this.type.globalType) {
140
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
141
+ // list and global type is not valid.
142
+ if (f) {
143
+ if (f.valid) {
144
+ if (JSON.stringify(this.type.listElemType) !==
145
+ JSON.stringify(f?.type.listElemType)) {
146
+ if (this.jsonAsList) {
147
+ if (!Array.isArray(obj)) {
148
+ return false;
149
+ }
150
+ // @ts-ignore
151
+ const valid = await Promise.all(obj.map((v) => f.valid(v)));
152
+ return valid.every((b) => b);
153
+ }
154
+ else {
155
+ return f.valid([obj]);
156
+ }
157
+ }
158
+ return f.valid(obj);
159
+ }
160
+ return true;
161
+ }
162
+ else {
163
+ (0, logger_1.log)("error", `globalType ${this.type.globalType} not found in global schema`);
164
+ return false;
165
+ }
166
+ }
167
+ if (this.jsonAsList) {
168
+ if (!Array.isArray(obj)) {
169
+ return false;
170
+ }
171
+ const valid = await Promise.all(obj.map((v) => this.validImpl(v)));
172
+ return valid.every((b) => b);
173
+ }
174
+ if (!(obj instanceof Object)) {
175
+ return false;
176
+ }
177
+ return this.validImpl(obj);
178
+ }
92
179
  }
93
180
  exports.StructField = StructField;
94
181
  function StructType(options) {
@@ -96,7 +183,15 @@ function StructType(options) {
96
183
  return Object.assign(result, options);
97
184
  }
98
185
  exports.StructType = StructType;
186
+ /**
187
+ * @deprecated use StructTypeAsList
188
+ */
99
189
  function StructListType(options) {
100
190
  return new field_1.ListField(StructType(options), options);
101
191
  }
102
192
  exports.StructListType = StructListType;
193
+ function StructTypeAsList(options) {
194
+ let result = new StructField(options, true);
195
+ return Object.assign(result, options);
196
+ }
197
+ exports.StructTypeAsList = StructTypeAsList;
@@ -15,7 +15,7 @@ export declare class UnionField extends BaseField implements FieldOptions {
15
15
  type: Type;
16
16
  m: Map<Object, string>;
17
17
  constructor(options: UnionOptions);
18
- format(obj: any): string | Object;
18
+ format(obj: any): any;
19
19
  private validField;
20
20
  valid(obj: any): Promise<boolean>;
21
21
  }
@@ -2,7 +2,11 @@
2
2
  "use strict";
3
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
4
  if (k2 === undefined) k2 = k;
5
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
6
10
  }) : (function(o, m, k, k2) {
7
11
  if (k2 === undefined) k2 = k;
8
12
  o[k2] = m[k];
@@ -25,7 +29,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
29
  Object.defineProperty(exports, "__esModule", { value: true });
26
30
  const typescript_1 = __importDefault(require("typescript"));
27
31
  const path = __importStar(require("path"));
28
- const glob_1 = __importDefault(require("glob"));
32
+ const glob = __importStar(require("glob"));
29
33
  const compilerOptions_1 = require("../tsc/compilerOptions");
30
34
  // TODO this should probably be its own package but for now it's here
31
35
  class Compiler {
@@ -193,7 +197,7 @@ class Compiler {
193
197
  }
194
198
  relPath = path.relative(
195
199
  // just because of how imports work. it's relative from directory not current path
196
- path.dirname(fullPath), path.join(text.substr(0, idx).replace(r, str.substr(0, strIdx)), text.substr(idx)));
200
+ path.dirname(fullPath), path.join(text.substring(0, idx).replace(r, str.substring(0, strIdx)), text.substring(idx)));
197
201
  // if file ends with "..", we've reached a case where we're trying to
198
202
  // import something like foo/contact(.ts) from within foo/contact/bar/baz/page.ts
199
203
  // and we're confused about it so we need to detect that case and handle it
@@ -204,7 +208,7 @@ class Compiler {
204
208
  let text2 = text + ".ts";
205
209
  relPath = path.relative(
206
210
  // just because of how imports work. it's relative from directory not current path
207
- path.dirname(fullPath), path.join(text2.substr(0, idx).replace(r, str.substr(0, strIdx)), text2.substr(idx)));
211
+ path.dirname(fullPath), path.join(text2.substring(0, idx).replace(r, str.substring(0, strIdx)), text2.substring(idx)));
208
212
  }
209
213
  }
210
214
  if (!relPath.startsWith("..")) {
@@ -213,7 +217,7 @@ class Compiler {
213
217
  // tsc removes this by default so we need to also do it
214
218
  let tsIdx = relPath.indexOf(".ts");
215
219
  if (tsIdx !== -1) {
216
- relPath = relPath.substr(0, tsIdx);
220
+ relPath = relPath.substring(0, tsIdx);
217
221
  }
218
222
  return relPath;
219
223
  }
@@ -225,7 +229,7 @@ class Compiler {
225
229
  let relPath = checkPath(paths, text);
226
230
  if (relPath) {
227
231
  // update the node...
228
- return typescript_1.default.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, typescript_1.default.createLiteral(relPath));
232
+ return typescript_1.default.factory.updateImportDeclaration(importNode, importNode.modifiers, importNode.importClause, typescript_1.default.factory.createStringLiteral(relPath), importNode.assertClause);
229
233
  }
230
234
  }
231
235
  if (node.kind === typescript_1.default.SyntaxKind.ExportDeclaration) {
@@ -235,7 +239,7 @@ class Compiler {
235
239
  let relPath = checkPath(paths, text);
236
240
  if (relPath) {
237
241
  // update the node...
238
- return typescript_1.default.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, typescript_1.default.createLiteral(relPath), exportNode.isTypeOnly);
242
+ return typescript_1.default.factory.updateExportDeclaration(exportNode, exportNode.modifiers, exportNode.isTypeOnly, exportNode.exportClause, typescript_1.default.factory.createStringLiteral(relPath), exportNode.assertClause);
239
243
  }
240
244
  }
241
245
  }
@@ -261,6 +265,6 @@ class Compiler {
261
265
  // todo this should be configurable
262
266
  // TODO this should be broken into its own repo and npm module
263
267
  // TODO use includes and exclude in tsconfig.json if it exists
264
- new Compiler(glob_1.default.sync("**/*.ts", {
268
+ new Compiler(glob.sync("**/*.ts", {
265
269
  ignore: ["node_modules/**", "tests/**", "**/*.test.ts"],
266
270
  }), ["node_modules/@types/node"]).compile();
@@ -2,7 +2,11 @@
2
2
  "use strict";
3
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
4
  if (k2 === undefined) k2 = k;
5
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
6
10
  }) : (function(o, m, k, k2) {
7
11
  if (k2 === undefined) k2 = k;
8
12
  o[k2] = m[k];
@@ -23,19 +27,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
28
  };
25
29
  Object.defineProperty(exports, "__esModule", { value: true });
26
- const glob_1 = __importDefault(require("glob"));
30
+ const glob = __importStar(require("glob"));
27
31
  const json5_1 = __importDefault(require("json5"));
28
32
  const minimist_1 = __importDefault(require("minimist"));
29
- const graphql_1 = require("../graphql/graphql");
30
- const readline = __importStar(require("readline"));
31
33
  const path = __importStar(require("path"));
32
34
  const fs = __importStar(require("fs"));
35
+ const graphql_1 = require("../graphql/graphql");
36
+ const readline = __importStar(require("readline"));
33
37
  const imports_1 = require("../imports");
34
38
  const process_1 = require("process");
39
+ const child_process_1 = require("child_process");
40
+ const const_1 = require("../core/const");
35
41
  // need to use the GQLCapture from the package so that when we call GQLCapture.enable()
36
42
  // we're affecting the local paths as opposed to a different instance
37
43
  // life is hard
38
- const MODULE_PATH = "@snowtop/ent/graphql";
44
+ const MODULE_PATH = const_1.GRAPHQL_PATH;
39
45
  async function readInputs() {
40
46
  return await new Promise((resolve) => {
41
47
  const rl = readline.createInterface({
@@ -72,7 +78,7 @@ function processCustomObjects(l, gqlCapture, input) {
72
78
  }
73
79
  }
74
80
  }
75
- function transformArgs(f) {
81
+ function transformArgs(f, gqlCapture) {
76
82
  return (f.args || []).map((v) => {
77
83
  const ret = {
78
84
  ...v,
@@ -80,7 +86,7 @@ function transformArgs(f) {
80
86
  // duplicated from getType in graphql.ts
81
87
  if ((0, graphql_1.isCustomType)(ret.type)) {
82
88
  ret.type = v.type.type;
83
- (0, graphql_1.addCustomType)(v.type);
89
+ (0, graphql_1.addCustomType)(v.type, gqlCapture);
84
90
  }
85
91
  // scalar types not supported for now
86
92
  ret.tsType = graphql_1.knownAllowedNames.get(v.type);
@@ -95,23 +101,33 @@ function transformResultType(f) {
95
101
  type: f.resultType,
96
102
  tsType: graphql_1.knownAllowedNames.get(f.resultType),
97
103
  list: f.list,
104
+ connection: f.connection,
98
105
  nullable: f.nullable,
99
106
  },
100
107
  ]
101
108
  : [];
102
109
  }
103
- function processTopLevel(l, l2) {
110
+ function processTopLevel(l, l2, gqlCapture) {
104
111
  for (const custom of l) {
105
112
  l2.push({
106
113
  nodeName: custom.class,
107
114
  functionName: custom.functionName || custom.name,
108
115
  gqlName: custom.graphQLName || custom.name,
116
+ edgeName: custom.edgeName,
109
117
  fieldType: custom.fieldType,
110
- args: transformArgs(custom),
118
+ args: transformArgs(custom, gqlCapture),
111
119
  results: transformResultType(custom),
120
+ description: custom.description,
121
+ extraImports: custom.extraImports,
122
+ functionContents: custom.functionContents,
112
123
  });
113
124
  }
114
125
  }
126
+ function processCustomTypes(m, gqlCapture) {
127
+ for (const k in m) {
128
+ (0, graphql_1.addCustomType)(m[k], gqlCapture);
129
+ }
130
+ }
115
131
  function processCustomFields(fields, gqlCapture, nodeName) {
116
132
  const m = gqlCapture.getCustomFields();
117
133
  let results = [];
@@ -121,12 +137,71 @@ function processCustomFields(fields, gqlCapture, nodeName) {
121
137
  gqlName: f.graphQLName || f.name,
122
138
  functionName: f.functionName || f.name,
123
139
  fieldType: f.fieldType,
124
- args: transformArgs(f),
140
+ args: transformArgs(f, gqlCapture),
125
141
  results: transformResultType(f),
142
+ description: f.description,
126
143
  });
127
144
  }
128
145
  m.set(nodeName, results);
129
146
  }
147
+ async function captureDynamic(filePath, gqlCapture) {
148
+ if (!filePath) {
149
+ return;
150
+ }
151
+ return await new Promise((resolve, reject) => {
152
+ let cmd = "";
153
+ const args = [];
154
+ const env = {
155
+ ...process.env,
156
+ };
157
+ // really only exists if there's a bug with swc or something. we should almost always be using swc
158
+ if (process.env.DISABLE_SWC) {
159
+ cmd = "ts-node";
160
+ args.push("--transpileOnly");
161
+ }
162
+ else {
163
+ cmd = "node";
164
+ // we seem to get tsconfig-paths by default because child process but not 100% sure...
165
+ args.push("-r", "@swc-node/register");
166
+ env.SWCRC = "true";
167
+ }
168
+ args.push(filePath);
169
+ const r = (0, child_process_1.spawn)(cmd, args, {
170
+ env,
171
+ });
172
+ const datas = [];
173
+ r.stdout.on("data", (data) => {
174
+ datas.push(data.toString());
175
+ });
176
+ r.stderr.on("data", (data) => {
177
+ reject(new Error(data.toString()));
178
+ });
179
+ r.on("close", (code) => {
180
+ if (code !== 0) {
181
+ reject(new Error(`error code ${code} on dynamic path`));
182
+ return;
183
+ }
184
+ let json = json5_1.default.parse(datas.join(""));
185
+ for (const k in json) {
186
+ const v = json[k];
187
+ switch (k) {
188
+ case "queries":
189
+ processTopLevel(v, gqlCapture.getCustomQueries(), gqlCapture);
190
+ break;
191
+ case "mutations":
192
+ processTopLevel(v, gqlCapture.getCustomMutations(), gqlCapture);
193
+ break;
194
+ case "customTypes":
195
+ processCustomTypes(v, gqlCapture);
196
+ break;
197
+ default:
198
+ reject(new Error(`key ${k} is unsupported in dynamic custom graphql. only queries and mutations are supported`));
199
+ }
200
+ }
201
+ resolve(undefined);
202
+ });
203
+ });
204
+ }
130
205
  async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
131
206
  if (jsonPath !== undefined) {
132
207
  let json = json5_1.default.parse(fs.readFileSync(jsonPath, {
@@ -144,10 +219,13 @@ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
144
219
  processCustomObjects(json.objects, gqlCapture);
145
220
  }
146
221
  if (json.queries) {
147
- processTopLevel(json.queries, gqlCapture.getCustomQueries());
222
+ processTopLevel(json.queries, gqlCapture.getCustomQueries(), gqlCapture);
148
223
  }
149
224
  if (json.mutations) {
150
- processTopLevel(json.mutations, gqlCapture.getCustomMutations());
225
+ processTopLevel(json.mutations, gqlCapture.getCustomMutations(), gqlCapture);
226
+ }
227
+ if (json.customTypes) {
228
+ processCustomTypes(json.customTypes, gqlCapture);
151
229
  }
152
230
  return;
153
231
  }
@@ -178,12 +256,12 @@ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
178
256
  // ignore test files.
179
257
  "**/*.test.ts",
180
258
  ];
181
- const customGQLResolvers = glob_1.default.sync(path.join(filePath, "/graphql/resolvers/**/*.ts"), {
259
+ const customGQLResolvers = glob.sync(path.join(filePath, "/graphql/resolvers/**/*.ts"), {
182
260
  // no actions for now to speed things up
183
261
  // no index.ts or internal file.
184
262
  ignore: ignore,
185
263
  });
186
- const customGQLMutations = glob_1.default.sync(path.join(filePath, "/graphql/mutations/**/*.ts"), {
264
+ const customGQLMutations = glob.sync(path.join(filePath, "/graphql/mutations/**/*.ts"), {
187
265
  // no actions for now to speed things up
188
266
  // no index.ts or internal file.
189
267
  ignore: ignore,
@@ -208,11 +286,27 @@ async function requireFiles(files) {
208
286
  throw new Error(err);
209
287
  });
210
288
  }
289
+ // filePath is path-to-src
211
290
  async function parseImports(filePath) {
212
- // only do graphql files...
213
- return (0, imports_1.parseCustomImports)(path.join(filePath, "graphql"), {
214
- ignore: ["**/generated/**", "**/tests/**"],
215
- });
291
+ return (0, imports_1.parseCustomImports)(filePath, [
292
+ {
293
+ // graphql files
294
+ root: path.join(filePath, "graphql"),
295
+ opts: {
296
+ ignore: ["**/generated/**", "**/tests/**"],
297
+ },
298
+ },
299
+ {
300
+ // can't just use top level ent files but have to check for all (non-generated) files
301
+ // in src/ent/* because custom edges (or other things) could have @gqlField etc
302
+ // and then have to look for these imports etc
303
+ root: path.join(filePath, "ent"),
304
+ opts: {
305
+ // not in action files since we can't customize payloads (yet?)
306
+ ignore: ["**/generated/**", "**/tests/**", "**/actions/**"],
307
+ },
308
+ },
309
+ ]);
216
310
  }
217
311
  function findGraphQLPath(filePath) {
218
312
  while (filePath !== "/") {
@@ -227,21 +321,11 @@ function findGraphQLPath(filePath) {
227
321
  }
228
322
  return undefined;
229
323
  }
324
+ // test as follows:
325
+ // there should be an easier way to do this...
326
+ // also, there should be a way to get the list of objects here that's not manual
327
+ //echo "User\nContact\nContactEmail\nComment" | ts-node-script --log-error --project ./tsconfig.json -r tsconfig-paths/register ../../ts/src/scripts/custom_graphql.ts --path ~/code/ent/examples/simple/src/
230
328
  async function main() {
231
- // known custom types that are not required
232
- // if not in the schema, will be ignored
233
- // something like GraphQLUpload gotten via gqlArg({type: gqlFileUpload})
234
- // these 2 need this because they're added by the schema
235
- // if this list grows too long, need to build this into golang types and passed here
236
- // TODO foreign non-scalars eventually
237
- (0, graphql_1.addCustomType)({
238
- importPath: "../graphql/scalars/time",
239
- type: "GraphQLTime",
240
- });
241
- (0, graphql_1.addCustomType)({
242
- importPath: "graphql-type-json",
243
- type: "GraphQLJSON",
244
- });
245
329
  const options = (0, minimist_1.default)(process.argv.slice(2));
246
330
  if (!options.path) {
247
331
  throw new Error("path required");
@@ -266,9 +350,28 @@ async function main() {
266
350
  gqlCapture = r.GQLCapture;
267
351
  gqlCapture.enable(true);
268
352
  }
269
- const [inputsRead, _, imports] = await Promise.all([
353
+ // known custom types that are not required
354
+ // if not in the schema, will be ignored
355
+ // something like GraphQLUpload gotten via gqlArg({type: gqlFileUpload})
356
+ // these 2 need this because they're added by the schema
357
+ // if this list grows too long, need to build this into golang types and passed here
358
+ // TODO foreign non-scalars eventually
359
+ (0, graphql_1.addCustomType)({
360
+ importPath: MODULE_PATH,
361
+ // for go tests...
362
+ // TODO need a flag that only does this for go tests
363
+ // breaks when running locally sometimes...
364
+ secondaryImportPath: "../graphql/scalars/time",
365
+ type: "GraphQLTime",
366
+ }, gqlCapture);
367
+ (0, graphql_1.addCustomType)({
368
+ importPath: "graphql-type-json",
369
+ type: "GraphQLJSON",
370
+ }, gqlCapture);
371
+ const [inputsRead, _, __, imports] = await Promise.all([
270
372
  readInputs(),
271
373
  captureCustom(options.path, options.files, options.json_path, gqlCapture),
374
+ captureDynamic(options.dynamic_path, gqlCapture),
272
375
  parseImports(options.path),
273
376
  ]);
274
377
  const { nodes, nodesMap } = inputsRead;
@@ -286,6 +389,8 @@ async function main() {
286
389
  let queries = gqlCapture.getProcessedCustomQueries();
287
390
  let mutations = gqlCapture.getProcessedCustomMutations();
288
391
  let objects = fromMap(gqlCapture.getCustomObjects());
392
+ let interfaces = fromMap(gqlCapture.getCustomInterfaces());
393
+ let unions = fromMap(gqlCapture.getCustomUnions());
289
394
  let customTypes = fromMap(gqlCapture.getCustomTypes());
290
395
  let classes = {};
291
396
  let allFiles = {};
@@ -325,7 +430,7 @@ async function main() {
325
430
  };
326
431
  const buildClasses = (fields) => {
327
432
  fields.forEach((field) => {
328
- if (!nodesMap.has(field.nodeName)) {
433
+ if (field.nodeName && !nodesMap.has(field.nodeName)) {
329
434
  let info = imports.getInfoForClass(field.nodeName);
330
435
  classes[field.nodeName] = { ...info.class, path: info.file.path };
331
436
  buildFiles(info.file);
@@ -336,6 +441,10 @@ async function main() {
336
441
  };
337
442
  buildClasses(mutations);
338
443
  buildClasses(queries);
444
+ // call for every field in a node
445
+ for (const k in fields) {
446
+ buildClasses(fields[k]);
447
+ }
339
448
  console.log(JSON.stringify({
340
449
  args,
341
450
  inputs,
@@ -344,6 +453,8 @@ async function main() {
344
453
  mutations,
345
454
  classes,
346
455
  objects,
456
+ interfaces,
457
+ unions,
347
458
  files: allFiles,
348
459
  customTypes,
349
460
  }));
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const minimist_1 = __importDefault(require("minimist"));
7
+ const transform_1 = require("../tsc/transform");
8
+ const transform_schema_1 = require("../tsc/transform_schema");
9
+ const transform_ent_1 = require("../tsc/transform_ent");
10
+ const move_generated_1 = require("../tsc/move_generated");
11
+ const transform_action_1 = require("../tsc/transform_action");
12
+ const ast_1 = require("../tsc/ast");
13
+ // todo-sqlite
14
+ // ts-node-script --swc --project ./tsconfig.json -r tsconfig-paths/register ../../ts/src/scripts/migrate_v0.1.ts --transform_schema --old_base_class BaseEntTodoSchema --new_schema_class TodoEntSchema --transform_path src/schema/patterns/base
15
+ function main() {
16
+ const customInfo = (0, ast_1.getCustomInfo)();
17
+ const options = (0, minimist_1.default)(process.argv.slice(2));
18
+ // install 0.1.x dependencies
19
+ // maybe provide options to make this easier if someone wants to do this in steps to see what's happening
20
+ if (options.move_generated) {
21
+ (0, move_generated_1.moveGenerated)(customInfo.relativeImports);
22
+ }
23
+ // codegen write-all --disable-custom-graphql
24
+ if (options.transform_schema) {
25
+ (0, transform_1.transform)(new transform_schema_1.TransformSchema(customInfo.relativeImports, options.old_base_class, options.new_schema_class, options.transform_path));
26
+ }
27
+ // codegen write-all --disable-custom-graphql
28
+ if (options.transform_ent) {
29
+ (0, transform_1.transform)(new transform_ent_1.TransformEnt());
30
+ }
31
+ if (options.transform_action) {
32
+ (0, transform_1.transform)(new transform_action_1.TransformAction(customInfo));
33
+ }
34
+ // codegen write-all
35
+ }
36
+ main();