1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const changeCase = require("change-case");
|
4 | const Handlebars = require("handlebars");
|
5 | const enum_matcher_1 = require("./enums/enum-matcher");
|
6 | const template = `/**
|
7 | * Autogenerated interface, DO NOT MODIFY
|
8 | */
|
9 | /* tslint:disable */
|
10 |
|
11 | import { {{imports}} } from "graphql";
|
12 | {{{extraImportStr}}}
|
13 |
|
14 | const {{name}}TypeFields = {
|
15 | {{fields}}
|
16 | };
|
17 |
|
18 | const {{name}}InputType = new GraphQLInputObjectType({
|
19 | fields: {{name}}TypeFields,
|
20 | name: "{{name}}Input"
|
21 | });
|
22 |
|
23 | const {{name}}Type = new GraphQLObjectType({
|
24 | fields: {{name}}TypeFields,
|
25 | name: "{{name}}"
|
26 | });
|
27 |
|
28 | export { {{name}}Type, {{name}}InputType, {{name}}TypeFields };
|
29 | `;
|
30 | const GRAPH_QL_DATE_TIME = "GraphQLDateTime";
|
31 | const GRAPH_QL_STRING = "GraphQLString";
|
32 | class GraphQlBuilder {
|
33 | constructor(schema) {
|
34 | this.schema = schema;
|
35 | this.matcher = new enum_matcher_1.EnumMatcher();
|
36 | this.compiledTemplate = Handlebars.compile(template);
|
37 | this.mysqlTypes = {
|
38 | blob: "string",
|
39 | bigint: "int",
|
40 | char: "string",
|
41 | date: "datetime",
|
42 | enum: "enum",
|
43 | datetime: "datetime",
|
44 | decimal: "float",
|
45 | double: "float",
|
46 | float: "float",
|
47 | int: "int",
|
48 | longblob: "string",
|
49 | longtext: "string",
|
50 | mediumtext: "string",
|
51 | set: "string",
|
52 | smallint: "int",
|
53 | text: "string",
|
54 | timestamp: "datetime",
|
55 | tinyint: "boolean",
|
56 | varchar: "string"
|
57 | };
|
58 | }
|
59 | renderTs(table, tableClass) {
|
60 | let stdTypes = new Set(["GraphQLObjectType", "GraphQLInputObjectType"]);
|
61 | let extraImports = new Set();
|
62 | Object.keys(table).forEach(colName => {
|
63 | var _a, _b, _c;
|
64 | const column = table[colName];
|
65 | if ((_a = column.enumValues) === null || _a === void 0 ? void 0 : _a.length) {
|
66 | const eh = this.matcher.run(this.schema, column, tableClass.tableName);
|
67 | const importTable = (_b = eh) === null || _b === void 0 ? void 0 : _b.table;
|
68 | const importColumn = (_c = eh) === null || _c === void 0 ? void 0 : _c.field;
|
69 | extraImports.add(this.importTableStatement(importTable, [importColumn]));
|
70 | return;
|
71 | }
|
72 | const qlType = this.toGraphType(column.type);
|
73 | if (qlType === GRAPH_QL_DATE_TIME) {
|
74 | extraImports.add('import { GraphQLDateTime } from "graphql-iso-date";');
|
75 | }
|
76 | else {
|
77 | stdTypes.add(qlType);
|
78 | }
|
79 | });
|
80 | const imports = [...stdTypes].sort().join(", ");
|
81 | const extraImportStr = [...extraImports].join("\n");
|
82 | const rows = Object.keys(table).map(colName => this.buildTypeRow(table[colName], tableClass));
|
83 | const fields = rows.join(", \n \t\t");
|
84 | const name = changeCase.pascalCase(tableClass.tableName);
|
85 | const t = this.compiledTemplate({ fields, name, imports, extraImportStr });
|
86 | return t;
|
87 | }
|
88 | importTableStatement(tableName, columnNames) {
|
89 | const PTABLE = changeCase.paramCase(tableName);
|
90 | const vars = columnNames.map(c => changeCase.pascalCase(c) + "Enum").join(", ");
|
91 | return `import { ${vars} } from "./enums/${PTABLE}-ql-enums.generated";`;
|
92 | }
|
93 | toGraphType(mysql) {
|
94 | const s = this.mysqlTypes[mysql];
|
95 | switch (s) {
|
96 | case "string":
|
97 | return GRAPH_QL_STRING;
|
98 | case "float":
|
99 | return "GraphQLFloat";
|
100 | case "datetime":
|
101 | return GRAPH_QL_DATE_TIME;
|
102 | case "int":
|
103 | return "GraphQLInt";
|
104 | case "boolean":
|
105 | return "GraphQLBoolean";
|
106 | default:
|
107 | throw "unknown type " + mysql;
|
108 | }
|
109 | }
|
110 | buildTypeRow(column, tableClass) {
|
111 | var _a, _b;
|
112 | let graphType = "";
|
113 | if ((_a = column.enumValues) === null || _a === void 0 ? void 0 : _a.length) {
|
114 | const eh = this.matcher.run(this.schema, column, tableClass.tableName);
|
115 | const importColumn = (_b = eh) === null || _b === void 0 ? void 0 : _b.field;
|
116 | graphType = changeCase.pascalCase(importColumn) + "Enum";
|
117 | }
|
118 | else {
|
119 | graphType = this.toGraphType(column.type);
|
120 | }
|
121 | return `${column.field}: { type: ${graphType} }`;
|
122 | }
|
123 | }
|
124 | exports.GraphQlBuilder = GraphQlBuilder;
|
125 |
|
\ | No newline at end of file |