@snowtop/ent 0.1.0-alpha3 → 0.1.0-alpha8

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 (60) hide show
  1. package/action/action.d.ts +2 -0
  2. package/action/executor.d.ts +1 -1
  3. package/action/orchestrator.d.ts +10 -2
  4. package/action/orchestrator.js +128 -34
  5. package/core/base.d.ts +5 -1
  6. package/core/base.js +16 -0
  7. package/core/clause.d.ts +24 -3
  8. package/core/clause.js +246 -5
  9. package/core/config.d.ts +18 -0
  10. package/core/config.js +17 -0
  11. package/core/db.d.ts +3 -3
  12. package/core/db.js +2 -0
  13. package/core/ent.d.ts +2 -4
  14. package/core/ent.js +70 -23
  15. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  16. package/core/loaders/assoc_edge_loader.js +5 -4
  17. package/core/loaders/index_loader.js +1 -0
  18. package/core/loaders/object_loader.d.ts +7 -2
  19. package/core/loaders/object_loader.js +59 -4
  20. package/core/privacy.js +3 -0
  21. package/core/viewer.d.ts +1 -0
  22. package/core/viewer.js +4 -0
  23. package/graphql/graphql.d.ts +3 -2
  24. package/graphql/graphql.js +22 -21
  25. package/graphql/index.d.ts +1 -0
  26. package/graphql/index.js +3 -1
  27. package/graphql/mutations/union.d.ts +2 -0
  28. package/graphql/mutations/union.js +35 -0
  29. package/graphql/node_resolver.d.ts +0 -1
  30. package/index.d.ts +16 -1
  31. package/index.js +18 -5
  32. package/package.json +2 -2
  33. package/parse_schema/parse.d.ts +11 -4
  34. package/parse_schema/parse.js +50 -6
  35. package/schema/base_schema.d.ts +36 -1
  36. package/schema/base_schema.js +48 -2
  37. package/schema/field.js +1 -1
  38. package/schema/index.d.ts +2 -2
  39. package/schema/index.js +8 -1
  40. package/schema/schema.d.ts +50 -1
  41. package/schema/schema.js +113 -5
  42. package/schema/union_field.d.ts +2 -1
  43. package/schema/union_field.js +32 -14
  44. package/scripts/custom_graphql.js +122 -15
  45. package/scripts/transform_schema.js +204 -55
  46. package/testutils/builder.d.ts +5 -1
  47. package/testutils/builder.js +46 -2
  48. package/testutils/context/test_context.d.ts +2 -2
  49. package/testutils/context/test_context.js +7 -1
  50. package/testutils/db/test_db.d.ts +2 -1
  51. package/testutils/db/test_db.js +13 -4
  52. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  53. package/testutils/ent-graphql-tests/index.js +7 -5
  54. package/testutils/fake_data/fake_contact.d.ts +2 -6
  55. package/testutils/fake_data/fake_contact.js +9 -16
  56. package/testutils/fake_data/fake_event.d.ts +2 -6
  57. package/testutils/fake_data/fake_event.js +17 -24
  58. package/testutils/fake_data/fake_user.d.ts +2 -6
  59. package/testutils/fake_data/fake_user.js +10 -17
  60. package/testutils/fake_data/test_helpers.js +1 -1
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = void 0;
3
+ exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.EntSchemaWithTZ = exports.EntSchema = exports.Node = exports.Timestamps = void 0;
4
4
  const uuid_1 = require("uuid");
5
5
  const field_1 = require("./field");
6
6
  let tsFields = {
@@ -67,8 +67,53 @@ exports.Node = {
67
67
  name: "node",
68
68
  fields: nodeFields,
69
69
  };
70
- // Base ent schema. has Node Pattern by default.
70
+ // Ent schema. has Node Pattern by default.
71
71
  // exists just to have less typing and easier for clients to implement
72
+ class EntSchema {
73
+ constructor(cfg) {
74
+ this.patterns = [exports.Node];
75
+ this.fields = cfg.fields;
76
+ this.tableName = cfg.tableName;
77
+ if (cfg.patterns) {
78
+ this.patterns.push(...cfg.patterns);
79
+ }
80
+ this.edges = cfg.edges;
81
+ this.edgeGroups = cfg.edgeGroups;
82
+ this.actions = cfg.actions;
83
+ this.enumTable = cfg.enumTable;
84
+ this.dbRows = cfg.dbRows;
85
+ this.constraints = cfg.constraints;
86
+ this.indices = cfg.indices;
87
+ this.hideFromGraphQL = cfg.hideFromGraphQL;
88
+ }
89
+ }
90
+ exports.EntSchema = EntSchema;
91
+ class EntSchemaWithTZ {
92
+ constructor(cfg) {
93
+ this.patterns = [
94
+ {
95
+ // default schema added
96
+ name: "nodeWithTZ",
97
+ fields: nodeFieldsWithTZ,
98
+ },
99
+ ];
100
+ this.fields = cfg.fields;
101
+ this.tableName = cfg.tableName;
102
+ if (cfg.patterns) {
103
+ this.patterns.push(...cfg.patterns);
104
+ }
105
+ this.edges = cfg.edges;
106
+ this.edgeGroups = cfg.edgeGroups;
107
+ this.actions = cfg.actions;
108
+ this.enumTable = cfg.enumTable;
109
+ this.dbRows = cfg.dbRows;
110
+ this.constraints = cfg.constraints;
111
+ this.indices = cfg.indices;
112
+ this.hideFromGraphQL = cfg.hideFromGraphQL;
113
+ }
114
+ }
115
+ exports.EntSchemaWithTZ = EntSchemaWithTZ;
116
+ // @deprecated use EntSchema
72
117
  class BaseEntSchema {
73
118
  constructor() {
74
119
  this.patterns = [exports.Node];
@@ -78,6 +123,7 @@ class BaseEntSchema {
78
123
  }
79
124
  }
80
125
  exports.BaseEntSchema = BaseEntSchema;
126
+ // @deprecated use EntSchemaWithTZ
81
127
  class BaseEntSchemaWithTZ {
82
128
  constructor() {
83
129
  this.patterns = [
package/schema/field.js CHANGED
@@ -616,7 +616,7 @@ class ListField extends BaseField {
616
616
  for (let i = 0; i < val.length; i++) {
617
617
  let formatted = val[i];
618
618
  if (this.field.format) {
619
- formatted = this.field.format(val[i]);
619
+ formatted = this.field.format(val[i], nested);
620
620
  }
621
621
  // postgres supports arrays natively so we
622
622
  // structure it in the expected format
package/schema/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import Schema from "./schema";
2
2
  export { Schema };
3
- export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, ActionOperation, Action, EdgeAction, NoFields, FieldMap, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, } from "./schema";
4
- export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, } from "./base_schema";
3
+ export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, getFieldsWithPrivacy, getStorageKey, ActionOperation, Action, EdgeAction, NoFields, FieldMap, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, getObjectLoaderProperties, } from "./schema";
4
+ export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, EntSchema, EntSchemaWithTZ, } from "./base_schema";
5
5
  export * from "./field";
6
6
  export * from "./json_field";
7
7
  export * from "./struct_field";
package/schema/index.js CHANGED
@@ -10,20 +10,27 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
13
+ exports.EntSchemaWithTZ = exports.EntSchema = exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getStorageKey = exports.getFieldsWithPrivacy = exports.getFields = exports.DBType = void 0;
14
14
  var schema_1 = require("./schema");
15
15
  Object.defineProperty(exports, "DBType", { enumerable: true, get: function () { return schema_1.DBType; } });
16
16
  Object.defineProperty(exports, "getFields", { enumerable: true, get: function () { return schema_1.getFields; } });
17
+ Object.defineProperty(exports, "getFieldsWithPrivacy", { enumerable: true, get: function () { return schema_1.getFieldsWithPrivacy; } });
18
+ Object.defineProperty(exports, "getStorageKey", { enumerable: true, get: function () { return schema_1.getStorageKey; } });
17
19
  Object.defineProperty(exports, "ActionOperation", { enumerable: true, get: function () { return schema_1.ActionOperation; } });
18
20
  Object.defineProperty(exports, "NoFields", { enumerable: true, get: function () { return schema_1.NoFields; } });
19
21
  Object.defineProperty(exports, "ConstraintType", { enumerable: true, get: function () { return schema_1.ConstraintType; } });
20
22
  Object.defineProperty(exports, "requiredField", { enumerable: true, get: function () { return schema_1.requiredField; } });
21
23
  Object.defineProperty(exports, "optionalField", { enumerable: true, get: function () { return schema_1.optionalField; } });
24
+ Object.defineProperty(exports, "SQLStatementOperation", { enumerable: true, get: function () { return schema_1.SQLStatementOperation; } });
25
+ Object.defineProperty(exports, "getTransformedReadClause", { enumerable: true, get: function () { return schema_1.getTransformedReadClause; } });
26
+ Object.defineProperty(exports, "getObjectLoaderProperties", { enumerable: true, get: function () { return schema_1.getObjectLoaderProperties; } });
22
27
  var base_schema_1 = require("./base_schema");
23
28
  Object.defineProperty(exports, "Timestamps", { enumerable: true, get: function () { return base_schema_1.Timestamps; } });
24
29
  Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return base_schema_1.Node; } });
25
30
  Object.defineProperty(exports, "BaseEntSchema", { enumerable: true, get: function () { return base_schema_1.BaseEntSchema; } });
26
31
  Object.defineProperty(exports, "BaseEntSchemaWithTZ", { enumerable: true, get: function () { return base_schema_1.BaseEntSchemaWithTZ; } });
32
+ Object.defineProperty(exports, "EntSchema", { enumerable: true, get: function () { return base_schema_1.EntSchema; } });
33
+ Object.defineProperty(exports, "EntSchemaWithTZ", { enumerable: true, get: function () { return base_schema_1.EntSchemaWithTZ; } });
27
34
  __exportStar(require("./field"), exports);
28
35
  __exportStar(require("./json_field"), exports);
29
36
  __exportStar(require("./struct_field"), exports);
@@ -1,5 +1,6 @@
1
- import { Data, Ent, LoaderInfo } from "../core/base";
1
+ import { Data, Ent, LoaderInfo, PrivacyPolicy, Viewer } from "../core/base";
2
2
  import { Builder } from "../action/action";
3
+ import { Clause } from "../core/clause";
3
4
  export declare type FieldMap = {
4
5
  [key: string]: Field;
5
6
  };
@@ -62,6 +63,27 @@ export interface Pattern {
62
63
  name: string;
63
64
  fields: FieldMap | Field[];
64
65
  edges?: Edge[];
66
+ transformRead?: () => Clause;
67
+ transformWrite?: <T extends Ent>(stmt: UpdateOperation<T>) => TransformedUpdateOperation<T> | undefined;
68
+ transformsDelete?: boolean;
69
+ transformsInsert?: boolean;
70
+ transformsUpdate?: boolean;
71
+ }
72
+ export declare enum SQLStatementOperation {
73
+ Insert = "insert",
74
+ Update = "update",
75
+ Delete = "delete"
76
+ }
77
+ export interface UpdateOperation<T extends Ent> {
78
+ op: SQLStatementOperation;
79
+ existingEnt?: T;
80
+ viewer: Viewer;
81
+ data?: Map<string, any>;
82
+ }
83
+ export interface TransformedUpdateOperation<T extends Ent> {
84
+ op: SQLStatementOperation;
85
+ data?: Data;
86
+ existingEnt?: T;
65
87
  }
66
88
  export declare enum DBType {
67
89
  UUID = "UUID",
@@ -85,6 +107,7 @@ export declare enum DBType {
85
107
  export interface ImportType {
86
108
  path: string;
87
109
  type: string;
110
+ [x: string]: any;
88
111
  }
89
112
  declare type EnumMap = {
90
113
  [key: string]: string;
@@ -135,11 +158,13 @@ export interface FieldOptions {
135
158
  fieldEdge?: FieldEdge;
136
159
  primaryKey?: boolean;
137
160
  disableUserEditable?: boolean;
161
+ disableUserGraphQLEditable?: boolean;
138
162
  defaultValueOnCreate?(builder: Builder<Ent>, input: Data): any;
139
163
  defaultToViewerOnCreate?: boolean;
140
164
  defaultValueOnEdit?(builder: Builder<Ent>, input: Data): any;
141
165
  derivedWhenEmbedded?: boolean;
142
166
  polymorphic?: boolean | PolymorphicOptions;
167
+ privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
143
168
  getDerivedFields?(name: string): FieldMap;
144
169
  [x: string]: any;
145
170
  }
@@ -158,7 +183,17 @@ export interface SchemaConstructor {
158
183
  new (): Schema;
159
184
  }
160
185
  export declare type SchemaInputType = Schema | SchemaConstructor;
186
+ export declare function getSchema(value: SchemaInputType): Schema;
161
187
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
188
+ export declare function getStorageKey(field: Field, fieldName: string): string;
189
+ export declare function getFieldsWithPrivacy(value: SchemaInputType): Map<string, PrivacyPolicy>;
190
+ export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
191
+ interface objectLoaderOptions {
192
+ clause?: () => Clause | undefined;
193
+ instanceKey?: string;
194
+ }
195
+ export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
196
+ export declare function getTransformedUpdateOp<T extends Ent>(value: SchemaInputType, stmt: UpdateOperation<T>): TransformedUpdateOperation<T> | undefined;
162
197
  export declare enum ActionOperation {
163
198
  Create = 1,
164
199
  Edit = 2,
@@ -201,10 +236,24 @@ export interface Constraint {
201
236
  fkey?: ForeignKeyInfo;
202
237
  condition?: string;
203
238
  }
239
+ export interface FullTextWeight {
240
+ A?: string[];
241
+ B?: string[];
242
+ C?: string[];
243
+ D?: string[];
244
+ }
245
+ export interface FullText {
246
+ generatedColumnName?: string;
247
+ language?: "english" | "french" | "german" | "simple";
248
+ languageColumn?: string;
249
+ indexType?: "gin" | "gist";
250
+ weights?: FullTextWeight;
251
+ }
204
252
  export interface Index {
205
253
  name: string;
206
254
  columns: string[];
207
255
  unique?: boolean;
256
+ fulltext?: FullText;
208
257
  }
209
258
  export interface ForeignKeyInfo {
210
259
  tableName: string;
package/schema/schema.js CHANGED
@@ -1,6 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
3
+ exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getTransformedUpdateOp = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.getFieldsWithPrivacy = exports.getStorageKey = exports.getFields = exports.getSchema = exports.DBType = exports.SQLStatementOperation = void 0;
4
+ const snake_case_1 = require("snake-case");
5
+ // we also want this transformation to exist on a per-action basis
6
+ // if it exists on an action, we don't do the global schema transformation
7
+ var SQLStatementOperation;
8
+ (function (SQLStatementOperation) {
9
+ // transform insert e.g. to an update based on whatever logic
10
+ SQLStatementOperation["Insert"] = "insert";
11
+ // // transform select e.g. deleted_at. can't change from select to different query type
12
+ // // but can change the query
13
+ // Select = "select",
14
+ // e.g. change updated value
15
+ SQLStatementOperation["Update"] = "update";
16
+ // delete -> update theoretically e.g. deleted_at
17
+ SQLStatementOperation["Delete"] = "delete";
18
+ })(SQLStatementOperation = exports.SQLStatementOperation || (exports.SQLStatementOperation = {}));
4
19
  // we want --strictNullChecks flag so nullable is used to type graphql, ts, db
5
20
  // should eventually generate (boolean | null) etc
6
21
  // supported db types
@@ -28,14 +43,17 @@ var DBType;
28
43
  function isSchema(value) {
29
44
  return value.fields !== undefined;
30
45
  }
31
- function getFields(value) {
32
- let schema;
46
+ function getSchema(value) {
33
47
  if (isSchema(value)) {
34
- schema = value;
48
+ return value;
35
49
  }
36
50
  else {
37
- schema = new value();
51
+ return new value();
38
52
  }
53
+ }
54
+ exports.getSchema = getSchema;
55
+ function getFields(value) {
56
+ const schema = getSchema(value);
39
57
  function addFields(fields) {
40
58
  if (Array.isArray(fields)) {
41
59
  for (const field of fields) {
@@ -68,6 +86,96 @@ function getFields(value) {
68
86
  return m;
69
87
  }
70
88
  exports.getFields = getFields;
89
+ function getStorageKey(field, fieldName) {
90
+ return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
91
+ }
92
+ exports.getStorageKey = getStorageKey;
93
+ // returns a mapping of storage key to field privacy
94
+ function getFieldsWithPrivacy(value) {
95
+ const schema = getSchema(value);
96
+ function addFields(fields) {
97
+ if (Array.isArray(fields)) {
98
+ for (const field of fields) {
99
+ const name = field.name;
100
+ if (!field.name) {
101
+ throw new Error(`name required`);
102
+ }
103
+ if (field.getDerivedFields !== undefined) {
104
+ addFields(field.getDerivedFields(name));
105
+ }
106
+ if (field.privacyPolicy) {
107
+ let privacyPolicy;
108
+ if (typeof field.privacyPolicy === "function") {
109
+ privacyPolicy = field.privacyPolicy();
110
+ }
111
+ else {
112
+ privacyPolicy = field.privacyPolicy;
113
+ }
114
+ m.set(getStorageKey(field, name), privacyPolicy);
115
+ }
116
+ }
117
+ }
118
+ for (const name in fields) {
119
+ const field = fields[name];
120
+ if (field.getDerivedFields !== undefined) {
121
+ addFields(field.getDerivedFields(name));
122
+ }
123
+ if (field.privacyPolicy) {
124
+ let privacyPolicy;
125
+ if (typeof field.privacyPolicy === "function") {
126
+ privacyPolicy = field.privacyPolicy();
127
+ }
128
+ else {
129
+ privacyPolicy = field.privacyPolicy;
130
+ }
131
+ m.set(getStorageKey(field, name), privacyPolicy);
132
+ }
133
+ }
134
+ }
135
+ let m = new Map();
136
+ if (schema.patterns) {
137
+ for (const pattern of schema.patterns) {
138
+ addFields(pattern.fields);
139
+ }
140
+ }
141
+ addFields(schema.fields);
142
+ return m;
143
+ }
144
+ exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
145
+ function getTransformedReadClause(value) {
146
+ const schema = getSchema(value);
147
+ if (!schema.patterns) {
148
+ return;
149
+ }
150
+ for (const p of schema.patterns) {
151
+ // e.g. discarded_at, deleted_at, etc
152
+ if (p.transformRead) {
153
+ // return clause.Eq('deleted_at', null);
154
+ return p.transformRead();
155
+ }
156
+ }
157
+ return;
158
+ }
159
+ exports.getTransformedReadClause = getTransformedReadClause;
160
+ function getObjectLoaderProperties(value, tableName) {
161
+ return {
162
+ clause: () => getTransformedReadClause(value),
163
+ instanceKey: `${tableName}:transformedReadClause`,
164
+ };
165
+ }
166
+ exports.getObjectLoaderProperties = getObjectLoaderProperties;
167
+ function getTransformedUpdateOp(value, stmt) {
168
+ const schema = getSchema(value);
169
+ if (!schema.patterns) {
170
+ return;
171
+ }
172
+ for (const p of schema.patterns) {
173
+ if (p.transformWrite) {
174
+ return p.transformWrite(stmt);
175
+ }
176
+ }
177
+ }
178
+ exports.getTransformedUpdateOp = getTransformedUpdateOp;
71
179
  // this maps to ActionOperation in ent/action.go
72
180
  var ActionOperation;
73
181
  (function (ActionOperation) {
@@ -15,7 +15,8 @@ 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, nested?: boolean): string | Object;
18
+ format(obj: any): string | Object;
19
+ private validField;
19
20
  valid(obj: any): Promise<boolean>;
20
21
  }
21
22
  export declare function UnionType(options: UnionOptions): UnionField & UnionOptions;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UnionListType = exports.UnionType = exports.UnionField = void 0;
4
4
  const schema_1 = require("./schema");
5
5
  const field_1 = require("./field");
6
+ // used to know which key in the union is valid.
7
+ // maybe there's a better way of doing this eventually
8
+ const KEY = "___valid___key___";
6
9
  class UnionField extends field_1.BaseField {
7
10
  constructor(options) {
8
11
  super();
@@ -19,21 +22,26 @@ class UnionField extends field_1.BaseField {
19
22
  this.type.dbType = schema_1.DBType.JSON;
20
23
  }
21
24
  }
22
- format(obj, nested) {
25
+ format(obj) {
23
26
  if (!(obj instanceof Object)) {
24
27
  throw new Error("valid was not called");
25
28
  }
26
- for (const k in this.options.fields) {
27
- const field = this.options.fields[k];
28
- const fmt = field.format(obj, nested);
29
- if (fmt !== "{}") {
30
- return fmt;
31
- }
29
+ const k = obj[KEY];
30
+ if (k === undefined) {
31
+ throw new Error(`need to call valid first`);
32
32
  }
33
- // TODO need better logic here
34
- // maybe add something ignored to the objec that indicates which key?
35
- // or store in map
36
- throw new Error(`couldn't format union`);
33
+ // now delete it since we don't need it anymore
34
+ delete obj[KEY];
35
+ const field = this.options.fields[k];
36
+ // always nested for now so pass through
37
+ return field.format(obj, true);
38
+ }
39
+ async validField(k, f, obj) {
40
+ const valid = await f.valid(obj);
41
+ return {
42
+ valid,
43
+ key: k,
44
+ };
37
45
  }
38
46
  async valid(obj) {
39
47
  if (!(obj instanceof Object)) {
@@ -42,11 +50,21 @@ class UnionField extends field_1.BaseField {
42
50
  let promises = [];
43
51
  for (const k in this.options.fields) {
44
52
  const field = this.options.fields[k];
45
- promises.push(field.valid(obj));
53
+ promises.push(this.validField(k, field, obj));
46
54
  }
55
+ let lastKey;
56
+ let validCt = 0;
47
57
  const ret = await Promise.all(promises);
48
- // only 1 should be valid
49
- return ret.filter((v) => v).length === 1;
58
+ for (const v of ret) {
59
+ if (v.valid) {
60
+ validCt++;
61
+ lastKey = v.key;
62
+ }
63
+ }
64
+ if (lastKey !== undefined) {
65
+ obj[KEY] = lastKey;
66
+ }
67
+ return validCt == 1;
50
68
  }
51
69
  }
52
70
  exports.UnionField = UnionField;
@@ -24,6 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const glob_1 = __importDefault(require("glob"));
27
+ const json5_1 = __importDefault(require("json5"));
27
28
  const minimist_1 = __importDefault(require("minimist"));
28
29
  const graphql_1 = require("../graphql/graphql");
29
30
  const readline = __importStar(require("readline"));
@@ -53,7 +54,103 @@ async function readInputs() {
53
54
  });
54
55
  });
55
56
  }
56
- async function captureCustom(filePath, filesCsv) {
57
+ function processCustomObjects(l, gqlCapture, input) {
58
+ let m;
59
+ if (input) {
60
+ m = gqlCapture.getCustomInputObjects();
61
+ }
62
+ else {
63
+ m = gqlCapture.getCustomObjects();
64
+ }
65
+ for (const input of l) {
66
+ m.set(input.name, {
67
+ nodeName: input.graphQLName || input.name,
68
+ className: input.name,
69
+ });
70
+ if (input.fields) {
71
+ processCustomFields(input.fields, gqlCapture, input.name);
72
+ }
73
+ }
74
+ }
75
+ function transformArgs(f) {
76
+ return (f.args || []).map((v) => {
77
+ const ret = {
78
+ ...v,
79
+ };
80
+ // duplicated from getType in graphql.ts
81
+ if ((0, graphql_1.isCustomType)(ret.type)) {
82
+ ret.type = v.type.type;
83
+ (0, graphql_1.addCustomType)(v.type);
84
+ }
85
+ // scalar types not supported for now
86
+ ret.tsType = graphql_1.knownAllowedNames.get(v.type);
87
+ return ret;
88
+ });
89
+ }
90
+ function transformResultType(f) {
91
+ return f.resultType
92
+ ? [
93
+ {
94
+ name: "",
95
+ type: f.resultType,
96
+ tsType: graphql_1.knownAllowedNames.get(f.resultType),
97
+ list: f.list,
98
+ nullable: f.nullable,
99
+ },
100
+ ]
101
+ : [];
102
+ }
103
+ function processTopLevel(l, l2) {
104
+ for (const custom of l) {
105
+ l2.push({
106
+ nodeName: custom.class,
107
+ functionName: custom.functionName || custom.name,
108
+ gqlName: custom.graphQLName || custom.name,
109
+ fieldType: custom.fieldType,
110
+ args: transformArgs(custom),
111
+ results: transformResultType(custom),
112
+ });
113
+ }
114
+ }
115
+ function processCustomFields(fields, gqlCapture, nodeName) {
116
+ const m = gqlCapture.getCustomFields();
117
+ let results = [];
118
+ for (const f of fields) {
119
+ results.push({
120
+ nodeName: nodeName,
121
+ gqlName: f.graphQLName || f.name,
122
+ functionName: f.functionName || f.name,
123
+ fieldType: f.fieldType,
124
+ args: transformArgs(f),
125
+ results: transformResultType(f),
126
+ });
127
+ }
128
+ m.set(nodeName, results);
129
+ }
130
+ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
131
+ if (jsonPath !== undefined) {
132
+ let json = json5_1.default.parse(fs.readFileSync(jsonPath, {
133
+ encoding: "utf8",
134
+ }));
135
+ if (json.fields) {
136
+ for (const k in json.fields) {
137
+ processCustomFields(json.fields[k], gqlCapture, k);
138
+ }
139
+ }
140
+ if (json.inputs) {
141
+ processCustomObjects(json.inputs, gqlCapture, true);
142
+ }
143
+ if (json.objects) {
144
+ processCustomObjects(json.objects, gqlCapture);
145
+ }
146
+ if (json.queries) {
147
+ processTopLevel(json.queries, gqlCapture.getCustomQueries());
148
+ }
149
+ if (json.mutations) {
150
+ processTopLevel(json.mutations, gqlCapture.getCustomMutations());
151
+ }
152
+ return;
153
+ }
57
154
  if (filesCsv !== undefined) {
58
155
  let files = filesCsv.split(",");
59
156
  for (let i = 0; i < files.length; i++) {
@@ -153,15 +250,25 @@ async function main() {
153
250
  if (!gqlPath) {
154
251
  throw new Error("could not find graphql path");
155
252
  }
156
- const r = require(gqlPath);
157
- if (!r.GQLCapture) {
158
- throw new Error("could not find GQLCapture in module");
253
+ // use different variable so that we use the correct GQLCapture as needed
254
+ // for local dev, get the one from the file system. otherwise, get the one
255
+ // from node_modules
256
+ let gqlCapture;
257
+ if (process.env.LOCAL_SCRIPT_PATH) {
258
+ const r = require("../graphql/graphql");
259
+ gqlCapture = r.GQLCapture;
260
+ }
261
+ else {
262
+ const r = require(gqlPath);
263
+ if (!r.GQLCapture) {
264
+ throw new Error("could not find GQLCapture in module");
265
+ }
266
+ gqlCapture = r.GQLCapture;
267
+ gqlCapture.enable(true);
159
268
  }
160
- const GQLCapture = r.GQLCapture;
161
- GQLCapture.enable(true);
162
269
  const [inputsRead, _, imports] = await Promise.all([
163
270
  readInputs(),
164
- captureCustom(options.path, options.files),
271
+ captureCustom(options.path, options.files, options.json_path, gqlCapture),
165
272
  parseImports(options.path),
166
273
  ]);
167
274
  const { nodes, nodesMap } = inputsRead;
@@ -172,14 +279,14 @@ async function main() {
172
279
  }
173
280
  return result;
174
281
  }
175
- GQLCapture.resolve(nodes);
176
- let args = fromMap(GQLCapture.getCustomArgs());
177
- let inputs = fromMap(GQLCapture.getCustomInputObjects());
178
- let fields = GQLCapture.getProcessedCustomFields();
179
- let queries = GQLCapture.getProcessedCustomQueries();
180
- let mutations = GQLCapture.getProcessedCustomMutations();
181
- let objects = fromMap(GQLCapture.getCustomObjects());
182
- let customTypes = fromMap(GQLCapture.getCustomTypes());
282
+ gqlCapture.resolve(nodes);
283
+ let args = fromMap(gqlCapture.getCustomArgs());
284
+ let inputs = fromMap(gqlCapture.getCustomInputObjects());
285
+ let fields = gqlCapture.getProcessedCustomFields();
286
+ let queries = gqlCapture.getProcessedCustomQueries();
287
+ let mutations = gqlCapture.getProcessedCustomMutations();
288
+ let objects = fromMap(gqlCapture.getCustomObjects());
289
+ let customTypes = fromMap(gqlCapture.getCustomTypes());
183
290
  let classes = {};
184
291
  let allFiles = {};
185
292
  const buildClasses2 = (args) => {