UNPKG

7.44 kBPlain TextView Raw
1import { Kind, print, ObjectTypeDefinitionNode, NonNullTypeNode, DirectiveNode, NameNode,
2 OperationTypeNode, FieldDefinitionNode, NamedTypeNode, InputValueDefinitionNode, ValueNode,
3 OperationTypeDefinitionNode, SchemaDefinitionNode, ArgumentNode, ListValueNode, StringValueNode,
4 InputObjectTypeDefinitionNode, DocumentNode} from 'graphql'
5
6const intTypes = [`INTEGER`, `INT`, `SMALLINT`, `TINYINT`, `MEDIUMINT`, `BIGINT`, `BIT`]
7const floatTypes = [`FLOAT`, `DOUBLE`, `REAL`, `REAL_AS_FLOAT`, `DOUBLE PRECISION`, `DEC`, `DECIMAL`, `FIXED`, `NUMERIC`]
8
9
10/**
11 * Creates a non-null type, which is a node wrapped around another type that simply defines it is non-nullable.
12 *
13 * @param typeNode the type to be marked as non-nullable.
14 * @returns a non-null wrapper around the provided type.
15 */
16 export function getNonNullType(typeNode: NamedTypeNode): NonNullTypeNode {
17 return {
18 kind: Kind.NON_NULL_TYPE,
19 type: typeNode
20 }
21}
22
23/**
24 * Creates a named type for the schema.
25 *
26 * @param name the name of the type.
27 * @returns a named type with the provided name.
28 */
29export 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 * Creates an input value definition for the schema.
41 *
42 * @param typeNode the type of the input node.
43 * @param name the name of the input.
44 * @returns an input value definition node with the provided type and name.
45 */
46export 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 * Creates an operation field definition for the schema.
59 *
60 * @param name the name of the operation.
61 * @param args the arguments for the operation.
62 * @param type the type of the operation.
63 * @param directives the directives (if any) applied to this field. In this context, only subscriptions will have this.
64 * @returns an operation field definition with the provided name, args, type, and optionally directives.
65 */
66export 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 * Creates a field definition node for the schema.
81 *
82 * @param fieldName the name of the field to be created.
83 * @param type the type of the field to be created.
84 * @returns a field definition node with the provided name and type.
85 */
86export 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 * Creates a type definition node for the schema.
99 *
100 * @param fields the field set to be included in the type.
101 * @param typeName the name of the type.
102 * @returns a type definition node defined by the provided fields and name.
103 */
104export 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 * Creates an input type definition node for the schema.
117 *
118 * @param fields the fields in the input type.
119 * @param typeName the name of the input type
120 * @returns an input type definition node defined by the provided fields and
121 */
122export 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 * Creates a name node for the schema.
135 *
136 * @param name the name of the name node.
137 * @returns the name node defined by the provided name.
138 */
139export function getNameNode(name: string): NameNode {
140 return {
141 kind: Kind.NAME,
142 value: name
143 }
144}
145
146/**
147 * Creates a list value node for the schema.
148 *
149 * @param values the list of values to be in the list node.
150 * @returns a list value node containing the provided values.
151 */
152export function getListValueNode(values: ReadonlyArray<ValueNode>): ListValueNode {
153 return {
154 kind: Kind.LIST,
155 values: values
156 }
157}
158
159/**
160 * Creates a simple string value node for the schema.
161 *
162 * @param value the value to be set in the string value node.
163 * @returns a fleshed-out string value node.
164 */
165export function getStringValueNode(value: string): StringValueNode {
166 return {
167 kind: Kind.STRING,
168 value: value
169 }
170}
171
172/**
173 * Creates a directive node for a subscription in the schema.
174 *
175 * @param mutationName the name of the mutation the subscription directive is for.
176 * @returns a directive node defining the subscription.
177 */
178export 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 * Creates an operation type definition (subscription, query, mutation) for the schema.
188 *
189 * @param operationType the type node defining the operation type.
190 * @param operation the named type node defining the operation type.
191 */
192export 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 * Creates an argument node for a subscription directive within the schema.
202 *
203 * @param argument the argument string.
204 * @returns the argument node.
205 */
206export 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 * Given the DB type for a column, make a best effort to select the appropriate GraphQL type for
216 * the corresponding field.
217 *
218 * @param dbType the SQL column type.
219 * @returns the GraphQL field type.
220 */
221export 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