UNPKG

7.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const graphql_1 = require("graphql");
4const intTypes = [`INTEGER`, `INT`, `SMALLINT`, `TINYINT`, `MEDIUMINT`, `BIGINT`, `BIT`];
5const floatTypes = [`FLOAT`, `DOUBLE`, `REAL`, `REAL_AS_FLOAT`, `DOUBLE PRECISION`, `DEC`, `DECIMAL`, `FIXED`, `NUMERIC`];
6/**
7 * Creates a non-null type, which is a node wrapped around another type that simply defines it is non-nullable.
8 *
9 * @param typeNode the type to be marked as non-nullable.
10 * @returns a non-null wrapper around the provided type.
11 */
12function getNonNullType(typeNode) {
13 return {
14 kind: graphql_1.Kind.NON_NULL_TYPE,
15 type: typeNode
16 };
17}
18exports.getNonNullType = getNonNullType;
19/**
20 * Creates a named type for the schema.
21 *
22 * @param name the name of the type.
23 * @returns a named type with the provided name.
24 */
25function getNamedType(name) {
26 return {
27 kind: graphql_1.Kind.NAMED_TYPE,
28 name: {
29 kind: graphql_1.Kind.NAME,
30 value: name
31 }
32 };
33}
34exports.getNamedType = getNamedType;
35/**
36 * Creates an input value definition for the schema.
37 *
38 * @param typeNode the type of the input node.
39 * @param name the name of the input.
40 * @returns an input value definition node with the provided type and name.
41 */
42function getInputValueDefinition(typeNode, name) {
43 return {
44 kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
45 name: {
46 kind: graphql_1.Kind.NAME,
47 value: name
48 },
49 type: typeNode
50 };
51}
52exports.getInputValueDefinition = getInputValueDefinition;
53/**
54 * Creates an operation field definition for the schema.
55 *
56 * @param name the name of the operation.
57 * @param args the arguments for the operation.
58 * @param type the type of the operation.
59 * @param directives the directives (if any) applied to this field. In this context, only subscriptions will have this.
60 * @returns an operation field definition with the provided name, args, type, and optionally directives.
61 */
62function getOperationFieldDefinition(name, args, type, directives) {
63 return {
64 kind: graphql_1.Kind.FIELD_DEFINITION,
65 name: {
66 kind: graphql_1.Kind.NAME,
67 value: name
68 },
69 arguments: args,
70 type: type,
71 directives: directives
72 };
73}
74exports.getOperationFieldDefinition = getOperationFieldDefinition;
75/**
76 * Creates a field definition node for the schema.
77 *
78 * @param fieldName the name of the field to be created.
79 * @param type the type of the field to be created.
80 * @returns a field definition node with the provided name and type.
81 */
82function getFieldDefinition(fieldName, type) {
83 return {
84 kind: graphql_1.Kind.FIELD_DEFINITION,
85 name: {
86 kind: graphql_1.Kind.NAME,
87 value: fieldName
88 },
89 type
90 };
91}
92exports.getFieldDefinition = getFieldDefinition;
93/**
94 * Creates a type definition node for the schema.
95 *
96 * @param fields the field set to be included in the type.
97 * @param typeName the name of the type.
98 * @returns a type definition node defined by the provided fields and name.
99 */
100function getTypeDefinition(fields, typeName) {
101 return {
102 kind: graphql_1.Kind.OBJECT_TYPE_DEFINITION,
103 name: {
104 kind: graphql_1.Kind.NAME,
105 value: typeName
106 },
107 fields: fields
108 };
109}
110exports.getTypeDefinition = getTypeDefinition;
111/**
112 * Creates an input type definition node for the schema.
113 *
114 * @param fields the fields in the input type.
115 * @param typeName the name of the input type
116 * @returns an input type definition node defined by the provided fields and
117 */
118function getInputTypeDefinition(fields, typeName) {
119 return {
120 kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
121 name: {
122 kind: graphql_1.Kind.NAME,
123 value: typeName
124 },
125 fields: fields
126 };
127}
128exports.getInputTypeDefinition = getInputTypeDefinition;
129/**
130 * Creates a name node for the schema.
131 *
132 * @param name the name of the name node.
133 * @returns the name node defined by the provided name.
134 */
135function getNameNode(name) {
136 return {
137 kind: graphql_1.Kind.NAME,
138 value: name
139 };
140}
141exports.getNameNode = getNameNode;
142/**
143 * Creates a list value node for the schema.
144 *
145 * @param values the list of values to be in the list node.
146 * @returns a list value node containing the provided values.
147 */
148function getListValueNode(values) {
149 return {
150 kind: graphql_1.Kind.LIST,
151 values: values
152 };
153}
154exports.getListValueNode = getListValueNode;
155/**
156 * Creates a simple string value node for the schema.
157 *
158 * @param value the value to be set in the string value node.
159 * @returns a fleshed-out string value node.
160 */
161function getStringValueNode(value) {
162 return {
163 kind: graphql_1.Kind.STRING,
164 value: value
165 };
166}
167exports.getStringValueNode = getStringValueNode;
168/**
169 * Creates a directive node for a subscription in the schema.
170 *
171 * @param mutationName the name of the mutation the subscription directive is for.
172 * @returns a directive node defining the subscription.
173 */
174function getDirectiveNode(mutationName) {
175 return {
176 kind: graphql_1.Kind.DIRECTIVE,
177 name: this.getNameNode('aws_subscribe'),
178 arguments: [this.getArgumentNode(mutationName)]
179 };
180}
181exports.getDirectiveNode = getDirectiveNode;
182/**
183 * Creates an operation type definition (subscription, query, mutation) for the schema.
184 *
185 * @param operationType the type node defining the operation type.
186 * @param operation the named type node defining the operation type.
187 */
188function getOperationTypeDefinition(operationType, operation) {
189 return {
190 kind: graphql_1.Kind.OPERATION_TYPE_DEFINITION,
191 operation: operationType,
192 type: operation
193 };
194}
195exports.getOperationTypeDefinition = getOperationTypeDefinition;
196/**
197 * Creates an argument node for a subscription directive within the schema.
198 *
199 * @param argument the argument string.
200 * @returns the argument node.
201 */
202function getArgumentNode(argument) {
203 return {
204 kind: graphql_1.Kind.ARGUMENT,
205 name: this.getNameNode('mutations'),
206 value: this.getListValueNode([this.getStringValueNode(argument)])
207 };
208}
209exports.getArgumentNode = getArgumentNode;
210/**
211 * Given the DB type for a column, make a best effort to select the appropriate GraphQL type for
212 * the corresponding field.
213 *
214 * @param dbType the SQL column type.
215 * @returns the GraphQL field type.
216 */
217function getGraphQLTypeFromMySQLType(dbType) {
218 const normalizedType = dbType.toUpperCase().split("(")[0];
219 if (`BOOL` == normalizedType) {
220 return `Boolean`;
221 }
222 else if (`JSON` == normalizedType) {
223 return `AWSJSON`;
224 }
225 else if (`TIME` == normalizedType) {
226 return `AWSTime`;
227 }
228 else if (`DATE` == normalizedType) {
229 return `AWSDate`;
230 }
231 else if (`DATETIME` == normalizedType) {
232 return `AWSDateTime`;
233 }
234 else if (`TIMESTAMP` == normalizedType) {
235 return `AWSTimestamp`;
236 }
237 else if (intTypes.indexOf(normalizedType) > -1) {
238 return `Int`;
239 }
240 else if (floatTypes.indexOf(normalizedType) > -1) {
241 return `Float`;
242 }
243 return `String`;
244}
245exports.getGraphQLTypeFromMySQLType = getGraphQLTypeFromMySQLType;
246//# sourceMappingURL=RelationalDBSchemaTransformerUtils.js.map
\No newline at end of file