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