1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const graphql_1 = require("graphql");
|
12 | const RelationalDBSchemaTransformerUtils_1 = require("./RelationalDBSchemaTransformerUtils");
|
13 | const RelationalDBParsingException_1 = require("./RelationalDBParsingException");
|
14 | const graphql_transformer_common_1 = require("graphql-transformer-common");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class TableContext {
|
21 | constructor(typeDefinition, createDefinition, updateDefinition, primaryKeyField, primaryKeyType, stringFieldList, intFieldList) {
|
22 | this.tableTypeDefinition = typeDefinition;
|
23 | this.tableKeyField = primaryKeyField;
|
24 | this.createTypeDefinition = createDefinition;
|
25 | this.updateTypeDefinition = updateDefinition;
|
26 | this.tableKeyFieldType = primaryKeyType;
|
27 | this.stringFieldList = stringFieldList;
|
28 | this.intFieldList = intFieldList;
|
29 | }
|
30 | }
|
31 | exports.TableContext = TableContext;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | class TemplateContext {
|
40 | constructor(schemaDoc, typePrimaryKeyMap, stringFieldMap, intFieldMap) {
|
41 | this.schemaDoc = schemaDoc;
|
42 | this.typePrimaryKeyMap = typePrimaryKeyMap;
|
43 | this.stringFieldMap = stringFieldMap;
|
44 | this.intFieldMap = intFieldMap;
|
45 | }
|
46 | }
|
47 | exports.default = TemplateContext;
|
48 | class RelationalDBSchemaTransformer {
|
49 | constructor(dbReader, database) {
|
50 | this.introspectDatabaseSchema = () => __awaiter(this, void 0, void 0, function* () {
|
51 |
|
52 | let tableNames = null;
|
53 | try {
|
54 | tableNames = yield this.dbReader.listTables();
|
55 | }
|
56 | catch (err) {
|
57 | throw new RelationalDBParsingException_1.RelationalDBParsingException(`Failed to list tables in ${this.database}`, err.stack);
|
58 | }
|
59 | let typeContexts = new Array();
|
60 | let types = new Array();
|
61 | let pkeyMap = new Map();
|
62 | let stringFieldMap = new Map();
|
63 | let intFieldMap = new Map();
|
64 | for (const tableName of tableNames) {
|
65 | let type = null;
|
66 | try {
|
67 | type = yield this.dbReader.describeTable(tableName);
|
68 | }
|
69 | catch (err) {
|
70 | throw new RelationalDBParsingException_1.RelationalDBParsingException(`Failed to describe table ${tableName}`, err.stack);
|
71 | }
|
72 |
|
73 |
|
74 | if (type.tableKeyField) {
|
75 | typeContexts.push(type);
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | types.push(type.createTypeDefinition);
|
82 |
|
83 | types.push(type.tableTypeDefinition);
|
84 |
|
85 | types.push(type.updateTypeDefinition);
|
86 |
|
87 | stringFieldMap.set(tableName, type.stringFieldList);
|
88 | intFieldMap.set(tableName, type.intFieldList);
|
89 | pkeyMap.set(tableName, type.tableKeyField);
|
90 | }
|
91 | else {
|
92 | console.warn(`Skipping table ${type.tableTypeDefinition.name.value} because it does not have a single PRIMARY KEY.`);
|
93 | }
|
94 | }
|
95 |
|
96 | types.push(this.getMutations(typeContexts));
|
97 | types.push(this.getQueries(typeContexts));
|
98 | types.push(this.getSubscriptions(typeContexts));
|
99 | types.push(this.getSchemaType());
|
100 | let context = this.dbReader.hydrateTemplateContext(new TemplateContext({ kind: graphql_1.Kind.DOCUMENT,
|
101 | definitions: types }, pkeyMap, stringFieldMap, intFieldMap));
|
102 | return context;
|
103 | });
|
104 | this.dbReader = dbReader;
|
105 | this.database = database;
|
106 | }
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 | getSchemaType() {
|
113 | return {
|
114 | kind: graphql_1.Kind.SCHEMA_DEFINITION,
|
115 | operationTypes: [
|
116 | RelationalDBSchemaTransformerUtils_1.getOperationTypeDefinition('query', RelationalDBSchemaTransformerUtils_1.getNamedType('Query')),
|
117 | RelationalDBSchemaTransformerUtils_1.getOperationTypeDefinition('mutation', RelationalDBSchemaTransformerUtils_1.getNamedType('Mutation')),
|
118 | RelationalDBSchemaTransformerUtils_1.getOperationTypeDefinition('subscription', RelationalDBSchemaTransformerUtils_1.getNamedType('Subscription'))
|
119 | ]
|
120 | };
|
121 | }
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | getMutations(types) {
|
130 | const fields = [];
|
131 | for (const typeContext of types) {
|
132 | const type = typeContext.tableTypeDefinition;
|
133 | fields.push(RelationalDBSchemaTransformerUtils_1.getOperationFieldDefinition(`delete${graphql_transformer_common_1.toUpper(type.name.value)}`, [RelationalDBSchemaTransformerUtils_1.getInputValueDefinition(RelationalDBSchemaTransformerUtils_1.getNonNullType(RelationalDBSchemaTransformerUtils_1.getNamedType(typeContext.tableKeyFieldType)), typeContext.tableKeyField)], RelationalDBSchemaTransformerUtils_1.getNamedType(`${type.name.value}`), null));
|
134 | fields.push(RelationalDBSchemaTransformerUtils_1.getOperationFieldDefinition(`create${graphql_transformer_common_1.toUpper(type.name.value)}`, [RelationalDBSchemaTransformerUtils_1.getInputValueDefinition(RelationalDBSchemaTransformerUtils_1.getNonNullType(RelationalDBSchemaTransformerUtils_1.getNamedType(`Create${type.name.value}Input`)), `create${type.name.value}Input`)], RelationalDBSchemaTransformerUtils_1.getNamedType(`${type.name.value}`), null));
|
135 | fields.push(RelationalDBSchemaTransformerUtils_1.getOperationFieldDefinition(`update${graphql_transformer_common_1.toUpper(type.name.value)}`, [RelationalDBSchemaTransformerUtils_1.getInputValueDefinition(RelationalDBSchemaTransformerUtils_1.getNonNullType(RelationalDBSchemaTransformerUtils_1.getNamedType(`Update${type.name.value}Input`)), `update${type.name.value}Input`)], RelationalDBSchemaTransformerUtils_1.getNamedType(`${type.name.value}`), null));
|
136 | }
|
137 | return RelationalDBSchemaTransformerUtils_1.getTypeDefinition(fields, 'Mutation');
|
138 | }
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | getSubscriptions(types) {
|
147 | const fields = [];
|
148 | for (const typeContext of types) {
|
149 | const type = typeContext.tableTypeDefinition;
|
150 | fields.push(RelationalDBSchemaTransformerUtils_1.getOperationFieldDefinition(`onCreate${graphql_transformer_common_1.toUpper(type.name.value)}`, [], RelationalDBSchemaTransformerUtils_1.getNamedType(`${type.name.value}`), [RelationalDBSchemaTransformerUtils_1.getDirectiveNode(`create${graphql_transformer_common_1.toUpper(type.name.value)}`)]));
|
151 | }
|
152 | return RelationalDBSchemaTransformerUtils_1.getTypeDefinition(fields, 'Subscription');
|
153 | }
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | getQueries(types) {
|
162 | const fields = [];
|
163 | for (const typeContext of types) {
|
164 | const type = typeContext.tableTypeDefinition;
|
165 | fields.push(RelationalDBSchemaTransformerUtils_1.getOperationFieldDefinition(`get${graphql_transformer_common_1.toUpper(type.name.value)}`, [RelationalDBSchemaTransformerUtils_1.getInputValueDefinition(RelationalDBSchemaTransformerUtils_1.getNonNullType(RelationalDBSchemaTransformerUtils_1.getNamedType(typeContext.tableKeyFieldType)), typeContext.tableKeyField)], RelationalDBSchemaTransformerUtils_1.getNamedType(`${type.name.value}`), null));
|
166 | fields.push(RelationalDBSchemaTransformerUtils_1.getOperationFieldDefinition(`list${graphql_transformer_common_1.toUpper(type.name.value)}s`, [], RelationalDBSchemaTransformerUtils_1.getNamedType(`[${type.name.value}]`), null));
|
167 | }
|
168 | return RelationalDBSchemaTransformerUtils_1.getTypeDefinition(fields, 'Query');
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | getConnectionType(tableName) {
|
177 | return RelationalDBSchemaTransformerUtils_1.getTypeDefinition([
|
178 | RelationalDBSchemaTransformerUtils_1.getFieldDefinition('items', RelationalDBSchemaTransformerUtils_1.getNamedType(`[${tableName}]`)),
|
179 | RelationalDBSchemaTransformerUtils_1.getFieldDefinition('nextToken', RelationalDBSchemaTransformerUtils_1.getNamedType('String'))
|
180 | ], `${tableName}Connection`);
|
181 | }
|
182 | }
|
183 | exports.RelationalDBSchemaTransformer = RelationalDBSchemaTransformer;
|
184 |
|
\ | No newline at end of file |