1 | import {
|
2 | Kind,
|
3 | print,
|
4 | ObjectTypeDefinitionNode,
|
5 | NonNullTypeNode,
|
6 | DirectiveNode,
|
7 | NameNode,
|
8 | OperationTypeNode,
|
9 | FieldDefinitionNode,
|
10 | NamedTypeNode,
|
11 | InputValueDefinitionNode,
|
12 | ValueNode,
|
13 | OperationTypeDefinitionNode,
|
14 | SchemaDefinitionNode,
|
15 | ArgumentNode,
|
16 | ListValueNode,
|
17 | StringValueNode,
|
18 | InputObjectTypeDefinitionNode,
|
19 | DocumentNode,
|
20 | } from 'graphql';
|
21 |
|
22 | const intTypes = [`INTEGER`, `INT`, `SMALLINT`, `TINYINT`, `MEDIUMINT`, `BIGINT`, `BIT`];
|
23 | const floatTypes = [`FLOAT`, `DOUBLE`, `REAL`, `REAL_AS_FLOAT`, `DOUBLE PRECISION`, `DEC`, `DECIMAL`, `FIXED`, `NUMERIC`];
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export function getNonNullType(typeNode: NamedTypeNode): NonNullTypeNode {
|
32 | return {
|
33 | kind: Kind.NON_NULL_TYPE,
|
34 | type: typeNode,
|
35 | };
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | export function getNamedType(name: string): NamedTypeNode {
|
45 | return {
|
46 | kind: Kind.NAMED_TYPE,
|
47 | name: {
|
48 | kind: Kind.NAME,
|
49 | value: name,
|
50 | },
|
51 | };
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | export function getInputValueDefinition(typeNode: NamedTypeNode | NonNullTypeNode, name: string): InputValueDefinitionNode {
|
62 | return {
|
63 | kind: Kind.INPUT_VALUE_DEFINITION,
|
64 | name: {
|
65 | kind: Kind.NAME,
|
66 | value: name,
|
67 | },
|
68 | type: typeNode,
|
69 | };
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | export function getOperationFieldDefinition(
|
82 | name: string,
|
83 | args: InputValueDefinitionNode[],
|
84 | type: NamedTypeNode,
|
85 | directives: ReadonlyArray<DirectiveNode>
|
86 | ): FieldDefinitionNode {
|
87 | return {
|
88 | kind: Kind.FIELD_DEFINITION,
|
89 | name: {
|
90 | kind: Kind.NAME,
|
91 | value: name,
|
92 | },
|
93 | arguments: args,
|
94 | type: type,
|
95 | directives: directives,
|
96 | };
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | export function getFieldDefinition(fieldName: string, type: NonNullTypeNode | NamedTypeNode): FieldDefinitionNode {
|
107 | return {
|
108 | kind: Kind.FIELD_DEFINITION,
|
109 | name: {
|
110 | kind: Kind.NAME,
|
111 | value: fieldName,
|
112 | },
|
113 | type,
|
114 | };
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export function getTypeDefinition(fields: ReadonlyArray<FieldDefinitionNode>, typeName: string): ObjectTypeDefinitionNode {
|
125 | return {
|
126 | kind: Kind.OBJECT_TYPE_DEFINITION,
|
127 | name: {
|
128 | kind: Kind.NAME,
|
129 | value: typeName,
|
130 | },
|
131 | fields: fields,
|
132 | };
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export function getInputTypeDefinition(fields: ReadonlyArray<InputValueDefinitionNode>, typeName: string): InputObjectTypeDefinitionNode {
|
143 | return {
|
144 | kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
145 | name: {
|
146 | kind: Kind.NAME,
|
147 | value: typeName,
|
148 | },
|
149 | fields: fields,
|
150 | };
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | export function getNameNode(name: string): NameNode {
|
160 | return {
|
161 | kind: Kind.NAME,
|
162 | value: name,
|
163 | };
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | export function getListValueNode(values: ReadonlyArray<ValueNode>): ListValueNode {
|
173 | return {
|
174 | kind: Kind.LIST,
|
175 | values: values,
|
176 | };
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | export function getStringValueNode(value: string): StringValueNode {
|
186 | return {
|
187 | kind: Kind.STRING,
|
188 | value: value,
|
189 | };
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | export function getDirectiveNode(mutationName: string): DirectiveNode {
|
199 | return {
|
200 | kind: Kind.DIRECTIVE,
|
201 | name: this.getNameNode('aws_subscribe'),
|
202 | arguments: [this.getArgumentNode(mutationName)],
|
203 | };
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | export function getOperationTypeDefinition(operationType: OperationTypeNode, operation: NamedTypeNode): OperationTypeDefinitionNode {
|
213 | return {
|
214 | kind: Kind.OPERATION_TYPE_DEFINITION,
|
215 | operation: operationType,
|
216 | type: operation,
|
217 | };
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | export function getArgumentNode(argument: string): ArgumentNode {
|
227 | return {
|
228 | kind: Kind.ARGUMENT,
|
229 | name: this.getNameNode('mutations'),
|
230 | value: this.getListValueNode([this.getStringValueNode(argument)]),
|
231 | };
|
232 | }
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | export function getGraphQLTypeFromMySQLType(dbType: string): string {
|
242 | const normalizedType = dbType.toUpperCase().split('(')[0];
|
243 | if (`BOOL` == normalizedType) {
|
244 | return `Boolean`;
|
245 | } else if (`JSON` == normalizedType) {
|
246 | return `AWSJSON`;
|
247 | } else if (`TIME` == normalizedType) {
|
248 | return `AWSTime`;
|
249 | } else if (`DATE` == normalizedType) {
|
250 | return `AWSDate`;
|
251 | } else if (`DATETIME` == normalizedType) {
|
252 | return `AWSDateTime`;
|
253 | } else if (`TIMESTAMP` == normalizedType) {
|
254 | return `AWSTimestamp`;
|
255 | } else if (intTypes.indexOf(normalizedType) > -1) {
|
256 | return `Int`;
|
257 | } else if (floatTypes.indexOf(normalizedType) > -1) {
|
258 | return `Float`;
|
259 | }
|
260 | return `String`;
|
261 | }
|