-
Notifications
You must be signed in to change notification settings - Fork 0
/
TsTypeGenerator.py
50 lines (45 loc) · 1.82 KB
/
TsTypeGenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class TsTypeGenerator:
def __init__(self,tbn='TableName',listOfParam=[],allString=True):
self.tp = self.typeParam(listOfParam,allString)
self.mainType = \
'/* add description here */\n' + \
'const '+tbn+'Type = new GraphQLObjectType({\n' + \
' name: \''+tbn+'\',\n' + \
' fields: () => ({\n' + \
' id: { type: new GraphQLNonNull(GraphQLID) },\n' + \
' created: {\n' + \
' type: new GraphQLNonNull(GraphQLString),\n' + \
' resolve: ({ created }) =>\n' + \
' created && typeof created !== \'string\'\n' + \
' ? created.toISOString()\n' + \
' : created,\n' + \
' },\n' + \
self.tp + \
' }),\n' + \
'});\n\n'
self.connectionType = self.mainType.replace(tbn,tbn+'Connection')
self.wholeContent = \
'/* add header here */\n' + \
'import {\n' + \
' GraphQLID,\n' + \
' GraphQLNonNull,\n' + \
' GraphQLObjectType,\n' + \
' GraphQLString,\n' + \
'} from \'graphql\';\n\n' + \
self.mainType + \
self.connectionType + \
'export { '+tbn+'ConnectionType };\n' + \
'export default '+tbn+'Type;\n'
def typeParam(self,listOfParam,allString):
s = ''
if allString:
for item in listOfParam:
s += ' '+str(item)+': {\n' + \
' type: GraphQLString,\n' + \
' description: \'\',\n' + \
' },\n'
return s
if __name__ == '__main__':
tst = TsTypeGenerator('MarketingActivity',['title','person'])
f = open('MAT.ts','w')
f.write(tst.wholeContent)