-
Notifications
You must be signed in to change notification settings - Fork 46
/
ModelRenderer.ts
214 lines (191 loc) · 6.15 KB
/
ModelRenderer.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { ObjectType, WarthogModel, FieldResolver } from '../model'
import Debug from 'debug'
import { GeneratorContext } from './SourcesGenerator'
import { buildFieldContext } from './field-context'
import * as utils from './utils'
import { GraphQLEnumType } from 'graphql'
import { AbstractRenderer } from './AbstractRenderer'
import { withEnum } from './enum-context'
import { camelCase } from 'lodash'
import { getRelationType } from '../model/Relation'
const debug = Debug('qnode-cli:model-renderer')
export class ModelRenderer extends AbstractRenderer {
private objType: ObjectType
constructor(
model: WarthogModel,
objType: ObjectType,
context: GeneratorContext = {}
) {
super(model, context)
this.objType = objType
}
withInterfaceProp(): GeneratorContext {
return {
isInterface: this.objType.isInterface,
}
}
withInterfaces(): GeneratorContext {
if (
utils.hasInterfaces(this.objType) &&
this.objType.interfaces !== undefined
) {
return {
interfaces: [utils.withNames(this.objType.interfaces[0])],
}
}
return {}
}
withSubclasses(): GeneratorContext {
if (this.objType.isInterface !== true) {
return {}
}
const subclasses: GeneratorContext[] = []
this.model
.getSubclasses(this.objType.name)
.map((o) => subclasses.push(utils.withNames(o)))
return {
subclasses,
}
}
withEnums(): GeneratorContext {
// we need to have a state to render exports only once
const referncedEnums = new Set<GraphQLEnumType>()
this.objType.fields.map((f) => {
if (f.isEnum()) referncedEnums.add(this.model.lookupEnum(f.type))
})
const enums: GeneratorContext[] = []
for (const e of referncedEnums) {
enums.push(withEnum(e))
}
return {
enums,
}
}
withFields(): GeneratorContext {
const fields: GeneratorContext[] = []
utils
.ownFields(this.objType)
.map((f) => fields.push(buildFieldContext(f, this.objType)))
return {
fields,
}
}
withDescription(): GeneratorContext {
return {
description: this.objType.description || undefined,
}
}
withHasProps(): GeneratorContext {
const has: GeneratorContext = {}
for (const field of this.objType.fields) {
let ct = field.columnType()
if (ct === 'numeric' || ct === 'decimal') ct = 'numeric'
has[ct] = true
}
has.array = this.objType.fields.some((f) => f.isArray())
has.enum = this.objType.fields.some((f) => f.isEnum())
has.union = this.objType.fields.some((f) => f.isUnion())
debug(`ObjectType has: ${JSON.stringify(has, null, 2)}`)
return {
has,
}
}
withImportProps(): GeneratorContext {
const relatedEntityImports: Set<string> = new Set()
const variantImports = new Set<string>()
this.objType.fields.forEach((f) => {
if (f.isUnion()) {
variantImports.add(
`import { ${f.type} } from '../variants/variants.model';\n`
)
}
if (f.relation) {
const { columnType } = f.relation
// Check if it is not a self reference so we don't add the object to import list
if (columnType !== this.objType.name) {
relatedEntityImports.add(utils.generateEntityImport(columnType))
}
}
})
return {
relatedEntityImports: Array.from(relatedEntityImports.values()),
variantImports: Array.from(variantImports),
}
}
withFieldResolvers(): GeneratorContext {
const fieldResolvers: FieldResolver[] = []
const fieldResolverImports: Set<string> = new Set()
const entityName = this.objType.name
for (const f of this.objType.fields) {
if (!f.relation) continue
const returnTypeFunc = f.relation.columnType
fieldResolvers.push({
returnTypeFunc,
rootArgType: entityName,
fieldName: f.name,
rootArgName: 'r', // disable utils.camelCase(entityName) could be a reverved ts/js keyword ie `class`
returnType: utils.generateResolverReturnType(returnTypeFunc, f.isList),
relatedTsProp: f.relation.relatedTsProp,
relationType: getRelationType(f.relation),
tableName: returnTypeFunc.toLowerCase(),
})
if (f.type !== this.objType.name) {
fieldResolverImports.add(utils.generateEntityImport(returnTypeFunc))
fieldResolverImports.add(
utils.generateEntityServiceImport(returnTypeFunc)
)
}
}
const imports = Array.from(fieldResolverImports.values())
// If there is at least one field resolver then add typeorm to imports
if (imports.length) {
imports.push(
`import { getConnection, getRepository, In, Not } from 'typeorm';
import _ from 'lodash';
`
)
}
return {
fieldResolvers,
fieldResolverImports: imports,
crossFilters: !!fieldResolvers.length,
}
}
/**
* Provides variant names for the fields that have union type, we need variant names for the union
* in order to fetch the data for variant relations and it is used by service.mst template
* @returns GeneratorContext
*/
withVariantNames(): GeneratorContext {
const variantNames = new Set<string>()
const fieldVariantMap: { field: string; type: string }[] = []
for (const field of this.objType.fields.filter((f) => f.isUnion())) {
const union = this.model.lookupUnion(field.type)
for (const type of union.types) {
type.fields.forEach((f) => {
if (f.isEntity()) {
variantNames.add(type.name)
fieldVariantMap.push({ field: camelCase(f.name), type: type.name })
}
})
}
}
return { variantNames: Array.from(variantNames), fieldVariantMap }
}
transform(): GeneratorContext {
return {
...this.context, // this.getGeneratedFolderRelativePath(objType.name),
...this.withFields(),
...this.withEnums(),
...this.withInterfaces(),
...this.withInterfaceProp(),
...this.withHasProps(),
...this.withSubclasses(),
...this.withDescription(),
...this.withImportProps(),
...this.withFieldResolvers(),
...utils.withNames(this.objType),
...this.withVariantNames(),
}
}
}