@snowtop/ent 0.1.0-alpha1 → 0.1.0-alpha13
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/action/action.d.ts +17 -13
- package/action/executor.d.ts +2 -2
- package/action/experimental_action.d.ts +4 -4
- package/action/experimental_action.js +1 -1
- package/action/orchestrator.d.ts +24 -9
- package/action/orchestrator.js +150 -39
- package/action/privacy.d.ts +2 -2
- package/core/base.d.ts +6 -2
- package/core/base.js +16 -0
- package/core/clause.d.ts +24 -3
- package/core/clause.js +246 -5
- package/core/config.d.ts +18 -0
- package/core/config.js +17 -0
- package/core/db.d.ts +3 -3
- package/core/db.js +2 -0
- package/core/ent.d.ts +2 -4
- package/core/ent.js +72 -25
- package/core/loaders/assoc_edge_loader.d.ts +1 -1
- package/core/loaders/assoc_edge_loader.js +5 -4
- package/core/loaders/index_loader.js +1 -0
- package/core/loaders/object_loader.d.ts +7 -2
- package/core/loaders/object_loader.js +59 -4
- package/core/privacy.d.ts +1 -1
- package/core/privacy.js +3 -0
- package/core/viewer.d.ts +1 -0
- package/core/viewer.js +4 -0
- package/graphql/builtins/connection.js +3 -3
- package/graphql/builtins/edge.js +2 -2
- package/graphql/builtins/node.js +1 -1
- package/graphql/graphql.d.ts +3 -2
- package/graphql/graphql.js +24 -23
- package/graphql/index.d.ts +1 -0
- package/graphql/index.js +3 -1
- package/graphql/mutations/union.d.ts +2 -0
- package/graphql/mutations/union.js +35 -0
- package/graphql/node_resolver.d.ts +0 -1
- package/graphql/query/connection_type.js +6 -6
- package/graphql/query/page_info.js +4 -4
- package/graphql/query/shared_assoc_test.js +2 -2
- package/graphql/scalars/time.d.ts +1 -1
- package/index.d.ts +16 -1
- package/index.js +18 -5
- package/package.json +3 -3
- package/parse_schema/parse.d.ts +21 -4
- package/parse_schema/parse.js +79 -8
- package/schema/base_schema.d.ts +36 -1
- package/schema/base_schema.js +48 -2
- package/schema/field.d.ts +1 -1
- package/schema/field.js +11 -3
- package/schema/index.d.ts +4 -2
- package/schema/index.js +10 -1
- package/schema/schema.d.ts +66 -4
- package/schema/schema.js +138 -5
- package/schema/struct_field.d.ts +17 -0
- package/schema/struct_field.js +102 -0
- package/schema/union_field.d.ts +23 -0
- package/schema/union_field.js +79 -0
- package/scripts/custom_graphql.js +122 -15
- package/scripts/move_generated.d.ts +1 -0
- package/scripts/move_generated.js +142 -0
- package/scripts/read_schema.js +15 -1
- package/scripts/transform_code.d.ts +1 -0
- package/scripts/transform_code.js +113 -0
- package/scripts/transform_schema.js +190 -123
- package/testutils/builder.d.ts +27 -18
- package/testutils/builder.js +82 -10
- package/testutils/context/test_context.d.ts +2 -2
- package/testutils/context/test_context.js +7 -1
- package/testutils/db/test_db.d.ts +2 -1
- package/testutils/db/test_db.js +13 -4
- package/testutils/ent-graphql-tests/index.d.ts +2 -0
- package/testutils/ent-graphql-tests/index.js +26 -17
- package/testutils/fake_data/fake_contact.d.ts +4 -8
- package/testutils/fake_data/fake_contact.js +15 -19
- package/testutils/fake_data/fake_event.d.ts +4 -8
- package/testutils/fake_data/fake_event.js +21 -25
- package/testutils/fake_data/fake_user.d.ts +5 -9
- package/testutils/fake_data/fake_user.js +23 -27
- package/testutils/fake_data/test_helpers.js +1 -1
- package/tsc/ast.d.ts +21 -0
- package/tsc/ast.js +154 -0
- package/tsc/compilerOptions.d.ts +6 -0
- package/tsc/compilerOptions.js +40 -1
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,15 +43,31 @@ var DBType;
|
|
|
28
43
|
function isSchema(value) {
|
|
29
44
|
return value.fields !== undefined;
|
|
30
45
|
}
|
|
31
|
-
function
|
|
32
|
-
let schema;
|
|
46
|
+
function getSchema(value) {
|
|
33
47
|
if (isSchema(value)) {
|
|
34
|
-
|
|
48
|
+
return value;
|
|
35
49
|
}
|
|
36
50
|
else {
|
|
37
|
-
|
|
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) {
|
|
58
|
+
if (Array.isArray(fields)) {
|
|
59
|
+
for (const field of fields) {
|
|
60
|
+
const name = field.name;
|
|
61
|
+
if (!name) {
|
|
62
|
+
throw new Error(`name required`);
|
|
63
|
+
}
|
|
64
|
+
if (field.getDerivedFields !== undefined) {
|
|
65
|
+
addFields(field.getDerivedFields(name));
|
|
66
|
+
}
|
|
67
|
+
m.set(name, field);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
40
71
|
for (const name in fields) {
|
|
41
72
|
const field = fields[name];
|
|
42
73
|
if (field.getDerivedFields !== undefined) {
|
|
@@ -55,6 +86,108 @@ function getFields(value) {
|
|
|
55
86
|
return m;
|
|
56
87
|
}
|
|
57
88
|
exports.getFields = getFields;
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated should only be used by tests
|
|
91
|
+
*/
|
|
92
|
+
function getStorageKey(field, fieldName) {
|
|
93
|
+
return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
|
|
94
|
+
}
|
|
95
|
+
exports.getStorageKey = getStorageKey;
|
|
96
|
+
// returns a mapping of storage key to field privacy
|
|
97
|
+
function getFieldsWithPrivacy(value, fieldMap) {
|
|
98
|
+
const schema = getSchema(value);
|
|
99
|
+
function addFields(fields) {
|
|
100
|
+
if (Array.isArray(fields)) {
|
|
101
|
+
for (const field of fields) {
|
|
102
|
+
const name = field.name;
|
|
103
|
+
if (!field.name) {
|
|
104
|
+
throw new Error(`name required`);
|
|
105
|
+
}
|
|
106
|
+
if (field.getDerivedFields !== undefined) {
|
|
107
|
+
addFields(field.getDerivedFields(name));
|
|
108
|
+
}
|
|
109
|
+
if (field.privacyPolicy) {
|
|
110
|
+
let privacyPolicy;
|
|
111
|
+
if (typeof field.privacyPolicy === "function") {
|
|
112
|
+
privacyPolicy = field.privacyPolicy();
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
privacyPolicy = field.privacyPolicy;
|
|
116
|
+
}
|
|
117
|
+
const info = fieldMap[name];
|
|
118
|
+
if (!info) {
|
|
119
|
+
throw new Error(`field with name ${name} not passed in fieldMap`);
|
|
120
|
+
}
|
|
121
|
+
m.set(info.dbCol, privacyPolicy);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (const name in fields) {
|
|
126
|
+
const field = fields[name];
|
|
127
|
+
if (field.getDerivedFields !== undefined) {
|
|
128
|
+
addFields(field.getDerivedFields(name));
|
|
129
|
+
}
|
|
130
|
+
if (field.privacyPolicy) {
|
|
131
|
+
let privacyPolicy;
|
|
132
|
+
if (typeof field.privacyPolicy === "function") {
|
|
133
|
+
privacyPolicy = field.privacyPolicy();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
privacyPolicy = field.privacyPolicy;
|
|
137
|
+
}
|
|
138
|
+
const info = fieldMap[name];
|
|
139
|
+
if (!info) {
|
|
140
|
+
throw new Error(`field with name ${name} not passed in fieldMap`);
|
|
141
|
+
}
|
|
142
|
+
m.set(info.dbCol, privacyPolicy);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
let m = new Map();
|
|
147
|
+
if (schema.patterns) {
|
|
148
|
+
for (const pattern of schema.patterns) {
|
|
149
|
+
addFields(pattern.fields);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
addFields(schema.fields);
|
|
153
|
+
return m;
|
|
154
|
+
}
|
|
155
|
+
exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
|
|
156
|
+
function getTransformedReadClause(value) {
|
|
157
|
+
const schema = getSchema(value);
|
|
158
|
+
if (!schema.patterns) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
for (const p of schema.patterns) {
|
|
162
|
+
// e.g. discarded_at, deleted_at, etc
|
|
163
|
+
if (p.transformRead) {
|
|
164
|
+
// return clause.Eq('deleted_at', null);
|
|
165
|
+
return p.transformRead();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
exports.getTransformedReadClause = getTransformedReadClause;
|
|
171
|
+
function getObjectLoaderProperties(value, tableName) {
|
|
172
|
+
return {
|
|
173
|
+
clause: () => getTransformedReadClause(value),
|
|
174
|
+
instanceKey: `${tableName}:transformedReadClause`,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
exports.getObjectLoaderProperties = getObjectLoaderProperties;
|
|
178
|
+
function getTransformedUpdateOp(value, stmt) {
|
|
179
|
+
const schema = getSchema(value);
|
|
180
|
+
if (!schema.patterns) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
for (const p of schema.patterns) {
|
|
184
|
+
if (p.transformWrite) {
|
|
185
|
+
return p.transformWrite(stmt);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
exports.getTransformedUpdateOp = getTransformedUpdateOp;
|
|
58
191
|
// this maps to ActionOperation in ent/action.go
|
|
59
192
|
var ActionOperation;
|
|
60
193
|
(function (ActionOperation) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseField, ListField } from "./field";
|
|
2
|
+
import { FieldOptions, Field, Type, FieldMap } from "./schema";
|
|
3
|
+
export interface StructOptions extends FieldOptions {
|
|
4
|
+
tsType: string;
|
|
5
|
+
fields: FieldMap;
|
|
6
|
+
graphQLType?: string;
|
|
7
|
+
jsonNotJSONB?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare class StructField extends BaseField implements Field {
|
|
10
|
+
private options;
|
|
11
|
+
type: Type;
|
|
12
|
+
constructor(options: StructOptions);
|
|
13
|
+
format(obj: any, nested?: boolean): string | Object;
|
|
14
|
+
valid(obj: any): Promise<boolean>;
|
|
15
|
+
}
|
|
16
|
+
export declare function StructType(options: StructOptions): StructField & StructOptions;
|
|
17
|
+
export declare function StructListType(options: StructOptions): ListField;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StructListType = exports.StructType = exports.StructField = void 0;
|
|
4
|
+
const field_1 = require("./field");
|
|
5
|
+
const schema_1 = require("./schema");
|
|
6
|
+
const camel_case_1 = require("camel-case");
|
|
7
|
+
class StructField extends field_1.BaseField {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
super();
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.type = {
|
|
12
|
+
dbType: schema_1.DBType.JSONB,
|
|
13
|
+
};
|
|
14
|
+
this.type.subFields = options.fields;
|
|
15
|
+
this.type.type = options.tsType;
|
|
16
|
+
this.type.graphQLType = options.graphQLType || options.tsType;
|
|
17
|
+
if (options.jsonNotJSONB) {
|
|
18
|
+
this.type.dbType = schema_1.DBType.JSON;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
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) {
|
|
25
|
+
if (!(obj instanceof Object)) {
|
|
26
|
+
throw new Error("valid was not called");
|
|
27
|
+
}
|
|
28
|
+
let ret = {};
|
|
29
|
+
for (const k in this.options.fields) {
|
|
30
|
+
// 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;
|
|
37
|
+
}
|
|
38
|
+
if (val === undefined) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const field = this.options.fields[k];
|
|
42
|
+
if (field.format) {
|
|
43
|
+
// indicate nested so this isn't JSON stringified
|
|
44
|
+
ret[dbKey] = field.format(val, true);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
ret[dbKey] = val;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// don't json.stringify if nested
|
|
51
|
+
if (nested) {
|
|
52
|
+
return ret;
|
|
53
|
+
}
|
|
54
|
+
return JSON.stringify(ret);
|
|
55
|
+
}
|
|
56
|
+
async valid(obj) {
|
|
57
|
+
if (!(obj instanceof Object)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
let promises = [];
|
|
61
|
+
// TODO probably need to support optional fields...
|
|
62
|
+
let valid = true;
|
|
63
|
+
for (const k in this.options.fields) {
|
|
64
|
+
const field = this.options.fields[k];
|
|
65
|
+
// 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;
|
|
72
|
+
}
|
|
73
|
+
if (val === undefined || val === null) {
|
|
74
|
+
// nullable, nothing to do here
|
|
75
|
+
if (field.nullable) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
valid = false;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
if (!field.valid) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
promises.push(field.valid(val));
|
|
85
|
+
}
|
|
86
|
+
if (!valid) {
|
|
87
|
+
return valid;
|
|
88
|
+
}
|
|
89
|
+
const ret = await Promise.all(promises);
|
|
90
|
+
return ret.every((v) => v);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.StructField = StructField;
|
|
94
|
+
function StructType(options) {
|
|
95
|
+
let result = new StructField(options);
|
|
96
|
+
return Object.assign(result, options);
|
|
97
|
+
}
|
|
98
|
+
exports.StructType = StructType;
|
|
99
|
+
function StructListType(options) {
|
|
100
|
+
return new field_1.ListField(StructType(options), options);
|
|
101
|
+
}
|
|
102
|
+
exports.StructListType = StructListType;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StructField } from "./struct_field";
|
|
2
|
+
import { FieldOptions, Type } from "./schema";
|
|
3
|
+
import { BaseField, ListField } from "./field";
|
|
4
|
+
export declare type StructMap = {
|
|
5
|
+
[key: string]: StructField;
|
|
6
|
+
};
|
|
7
|
+
export interface UnionOptions extends FieldOptions {
|
|
8
|
+
tsType: string;
|
|
9
|
+
fields: StructMap;
|
|
10
|
+
graphQLType?: string;
|
|
11
|
+
jsonNotJSONB?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class UnionField extends BaseField implements FieldOptions {
|
|
14
|
+
private options;
|
|
15
|
+
type: Type;
|
|
16
|
+
m: Map<Object, string>;
|
|
17
|
+
constructor(options: UnionOptions);
|
|
18
|
+
format(obj: any): string | Object;
|
|
19
|
+
private validField;
|
|
20
|
+
valid(obj: any): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export declare function UnionType(options: UnionOptions): UnionField & UnionOptions;
|
|
23
|
+
export declare function UnionListType(options: UnionOptions): ListField;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnionListType = exports.UnionType = exports.UnionField = void 0;
|
|
4
|
+
const schema_1 = require("./schema");
|
|
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___";
|
|
9
|
+
class UnionField extends field_1.BaseField {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super();
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.type = {
|
|
14
|
+
dbType: schema_1.DBType.JSONB,
|
|
15
|
+
};
|
|
16
|
+
this.m = new Map();
|
|
17
|
+
this.type.unionFields = options.fields;
|
|
18
|
+
this.type.type = options.tsType;
|
|
19
|
+
this.type.graphQLType = options.graphQLType || options.tsType;
|
|
20
|
+
// TODO should throw if not nested?
|
|
21
|
+
if (options.jsonNotJSONB) {
|
|
22
|
+
this.type.dbType = schema_1.DBType.JSON;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
format(obj) {
|
|
26
|
+
if (!(obj instanceof Object)) {
|
|
27
|
+
throw new Error("valid was not called");
|
|
28
|
+
}
|
|
29
|
+
const k = obj[KEY];
|
|
30
|
+
if (k === undefined) {
|
|
31
|
+
throw new Error(`need to call valid first`);
|
|
32
|
+
}
|
|
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
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async valid(obj) {
|
|
47
|
+
if (!(obj instanceof Object)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
let promises = [];
|
|
51
|
+
for (const k in this.options.fields) {
|
|
52
|
+
const field = this.options.fields[k];
|
|
53
|
+
promises.push(this.validField(k, field, obj));
|
|
54
|
+
}
|
|
55
|
+
let lastKey;
|
|
56
|
+
let validCt = 0;
|
|
57
|
+
const ret = await Promise.all(promises);
|
|
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;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.UnionField = UnionField;
|
|
71
|
+
function UnionType(options) {
|
|
72
|
+
let result = new UnionField(options);
|
|
73
|
+
return Object.assign(result, options);
|
|
74
|
+
}
|
|
75
|
+
exports.UnionType = UnionType;
|
|
76
|
+
function UnionListType(options) {
|
|
77
|
+
return new field_1.ListField(UnionType(options), options);
|
|
78
|
+
}
|
|
79
|
+
exports.UnionListType = UnionListType;
|
|
@@ -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
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
176
|
-
let args = fromMap(
|
|
177
|
-
let inputs = fromMap(
|
|
178
|
-
let fields =
|
|
179
|
-
let queries =
|
|
180
|
-
let mutations =
|
|
181
|
-
let objects = fromMap(
|
|
182
|
-
let customTypes = fromMap(
|
|
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) => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|