This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
RelationshipMetadataBuilder.ts
332 lines (263 loc) · 12.5 KB
/
RelationshipMetadataBuilder.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* eslint-disable max-lines */
import { GraphQLObjectType, GraphQLField, isObjectType, GraphQLScalarType, GraphQLOutputType, GraphQLNonNull, GraphQLList, getNamedType } from 'graphql';
import { parseMetadata } from 'graphql-metadata';
import { isModelType } from '../crud';
import { transformForeignKeyName } from '../db';
import { hasListType } from '../utils/hasListType';
import { parseRelationshipAnnotation, relationshipFieldDescriptionTemplate, relationshipOneToOneFieldDescriptionTemplate } from './relationshipHelpers';
export interface FieldRelationshipMetadata {
kind: 'oneToMany' | 'oneToOne' | 'manyToOne'
owner: GraphQLObjectType
ownerField: GraphQLField<any, any>
relationType: GraphQLObjectType
relationFieldName: string
relationFieldType?: GraphQLScalarType
relationForeignKey?: string
}
export interface RelationshipAnnotation {
kind: 'oneToMany' | 'oneToOne' | 'manyToOne'
field?: string
key?: string
}
/**
* Builds relationship context for entire data model.
* Performs validation on relationship fields and metadata
* Dynamically creates relationship fields and maps values to data layer.
*/
export class RelationshipMetadataBuilder {
private modelTypes: GraphQLObjectType[];
private relationships: FieldRelationshipMetadata[];
public constructor(modelTypes: GraphQLObjectType[]) {
this.relationships = [];
this.modelTypes = modelTypes;
}
/**
* Builds relationship context for entire data model
* Generates fields and anotations
*/
public build() {
for (const modelType of this.modelTypes) {
this.buildModelRelationshipContext(modelType);
}
}
/**
* Get all relationships
*/
public getRelationships() {
return this.relationships;
}
/**
* Get all relationships where the model is the parent.
* @param modelName
*/
public getModelRelationships(modelName: string): FieldRelationshipMetadata[] {
return this.relationships.filter((relationship: FieldRelationshipMetadata) => relationship.owner.name === modelName);
}
/**
* Collects relationship information for a model based on relationship field annotations
* and pushes to list of all relationships in data model.
*
* @param modelType
*/
private buildModelRelationshipContext(modelType: GraphQLObjectType) {
const fields = Object.values(modelType.getFields());
for (let field of fields) {
const annotation = parseRelationshipAnnotation(field.description);
if (!annotation) {
continue;
}
this.validateRelationshipField(modelType.name, field, annotation);
const relationType = getNamedType(field.type) as GraphQLObjectType;
let relationField = relationType.getFields()[annotation.field];
if (annotation.kind === 'oneToMany') {
field = this.updateOneToManyField(field, annotation.field, annotation.key)
if (!relationField) {
relationField = this.createManyToOneField(annotation.field, modelType, field.name, annotation.key);
} else {
relationField = this.updateManyToOneField(relationField, field.name, annotation.key);
}
// Avoid reparsing if annotation key defined
const oneToManyAnnotation = annotation.key ? annotation : parseRelationshipAnnotation(field.description);
const manyToOneAnnotation = parseRelationshipAnnotation(relationField.description);
this.addOneToMany(modelType, field, oneToManyAnnotation, manyToOneAnnotation);
this.addManyToOne(relationType, relationField, manyToOneAnnotation);
} else if (annotation.kind === 'manyToOne') {
if (!relationField) {
relationField = this.createOneToManyField(annotation.field, modelType, field.name, annotation.key);
}
const oneToManyAnnotation = parseRelationshipAnnotation(relationField.description);
this.addManyToOne(modelType, field, annotation);
this.addOneToMany(relationType, relationField, oneToManyAnnotation, annotation);
} else if (annotation.kind === 'oneToOne') {
field = this.updateOneToOneField(field, annotation.key);
// avoid reparsing if annotation key defined
const oneToOneAnnotation = annotation.key ? annotation : parseRelationshipAnnotation(field.description)
this.addOneToOne(modelType, field, oneToOneAnnotation);
}
}
}
private createOneToManyField(fieldName: string, baseType: GraphQLOutputType, relationFieldName: string, columnName?: string): GraphQLField<any, any> {
const columnField = columnName || transformForeignKeyName(relationFieldName);
const fieldDescription = relationshipFieldDescriptionTemplate('oneToMany', relationFieldName, columnField);
const fieldType = GraphQLNonNull(GraphQLList(baseType));
return {
name: fieldName,
description: fieldDescription,
type: fieldType,
args: [],
extensions: [],
isDeprecated: false,
deprecationReason: undefined
}
}
private createManyToOneField(fieldName: string, baseType: GraphQLOutputType, relationFieldName: string, columnName?: string): GraphQLField<any, any> {
const columnField = columnName || transformForeignKeyName(fieldName);
const fieldDescription = relationshipFieldDescriptionTemplate('manyToOne', relationFieldName, columnField);
return {
name: fieldName,
description: fieldDescription,
type: baseType,
args: [],
extensions: [],
isDeprecated: false,
deprecationReason: undefined
}
}
private updateOneToManyField(field: GraphQLField<any, any>, relationFieldName: string, columnName?: string): GraphQLField<any, any> {
if (!columnName) {
const columnField = transformForeignKeyName(relationFieldName);
const fieldDescription = relationshipFieldDescriptionTemplate('oneToMany', relationFieldName, columnField);
const oldDescription = field.description ? `\n${field.description}` : '';
return {
...field,
description: `${fieldDescription}${oldDescription}`
}
}
return field;
}
private updateManyToOneField(field: GraphQLField<any, any>, relationFieldName: string, columnName?: string): GraphQLField<any, any> {
const manyToOneMetadata = parseMetadata("manyToOne", field.description);
if (!manyToOneMetadata || !manyToOneMetadata.key) {
const columnField = columnName || transformForeignKeyName(field.name);
const fieldDescription = relationshipFieldDescriptionTemplate('manyToOne', relationFieldName, columnField)
const oldDescription = field.description ? `\n${field.description}` : '';
return {
...field,
description: `${fieldDescription}${oldDescription}`
}
}
return field;
}
private updateOneToOneField(field: GraphQLField<any, any>, columnName?: string): GraphQLField<any, any> {
if (!columnName) {
const columnField = transformForeignKeyName(field.name);
const fieldDescription = relationshipOneToOneFieldDescriptionTemplate('oneToOne', columnField);
const oldDescription = field.description ? `\n${field.description}` : '';
return {
...field,
description: `${fieldDescription}${oldDescription}`
}
}
return field;
}
private addOneToMany(ownerType: GraphQLObjectType, field: GraphQLField<any, any>, oneToManyAnnotation: RelationshipAnnotation, corresspondingManyToOneMetadata: RelationshipAnnotation) {
this.validateOneToManyRelationship(ownerType.name, field, oneToManyAnnotation, corresspondingManyToOneMetadata);
// skip - relationship with key annotation will be generated at later stage
if (!oneToManyAnnotation.key) {
return;
}
const relationType = getNamedType(field.type) as GraphQLObjectType;
const oneToMany: FieldRelationshipMetadata = {
kind: 'oneToMany',
owner: ownerType,
ownerField: field,
relationType,
relationFieldName: oneToManyAnnotation.field,
relationForeignKey: oneToManyAnnotation.key
};
this.relationships.push(oneToMany);
}
private addManyToOne(ownerType: GraphQLObjectType, field: GraphQLField<any, any>, manyToOneAnnotation: RelationshipAnnotation) {
this.validateManyToOneField(ownerType.name, field, manyToOneAnnotation);
// skip - relationship with key annotation will be generated at later stage
if (!manyToOneAnnotation.key) {
return;
}
const relationType = getNamedType(field.type) as GraphQLObjectType;
const manyToOne: FieldRelationshipMetadata = {
kind: 'manyToOne',
owner: ownerType,
ownerField: field,
relationType,
relationFieldName: manyToOneAnnotation.field,
relationForeignKey: manyToOneAnnotation.key
}
this.relationships.push(manyToOne);
}
private addOneToOne(ownerType: GraphQLObjectType, field: GraphQLField<any, any>, oneToOneAnnotation: RelationshipAnnotation) {
this.validateOneToOneRelationship(ownerType.name, field, oneToOneAnnotation);
// skip - relationship with key mapping will be generated at a later stage
if (!oneToOneAnnotation.key) {
return;
}
const relationType = getNamedType(field.type) as GraphQLObjectType;
const oneToOne: FieldRelationshipMetadata = {
kind: 'oneToOne',
owner: ownerType,
ownerField: field,
relationType,
relationFieldName: oneToOneAnnotation.field,
relationForeignKey: oneToOneAnnotation.key
};
this.relationships.push(oneToOne);
}
private validateOneToManyRelationship(modelName: string, field: GraphQLField<any, any>, oneToManyMetadata: RelationshipAnnotation, corresspondingManyToOneMetadata: RelationshipAnnotation) {
this.validateRelationshipField(modelName, field, oneToManyMetadata);
if (oneToManyMetadata.kind !== 'oneToMany') {
throw new Error(`${modelName}.${field.name} should be a @oneToMany field, but has a @${oneToManyMetadata.kind} annotation`);
}
const relationModelType = getNamedType(field.type) as GraphQLObjectType;
const relationField = relationModelType.getFields()[oneToManyMetadata.field];
// field will be generated, no need to validate
if (!relationField) {
return;
}
if (hasListType(relationField.type)) {
throw new Error(`${relationModelType.name}.${relationField.name} is a list type, but should be '${relationField.name}: ${modelName}'.`)
}
const relationFieldBaseType = getNamedType(relationField.type);
if (!isObjectType(relationFieldBaseType) || relationFieldBaseType.name !== modelName) {
throw new Error(`${modelName}.${field.name} relationship field maps to ${relationModelType.name}.${relationField.name} (${relationFieldBaseType.name} type) which should be ${modelName} type.`);
}
if (oneToManyMetadata?.key !== corresspondingManyToOneMetadata?.key) {
throw new Error(`${modelName}.${field.name} and ${relationModelType.name}.${relationField.name} 'key' annotations are different. Ensure both are the same, or remove one so that it can be generated.`);
}
}
private validateManyToOneField(modelName: string, field: GraphQLField<any, any>, manyToOneAnnotation: RelationshipAnnotation) {
this.validateRelationshipField(modelName, field, manyToOneAnnotation);
if (manyToOneAnnotation.kind !== 'manyToOne') {
throw new Error(`${modelName}.${field.name} should be a @manyToOne field, but has a @${manyToOneAnnotation.kind} annotation`);
}
}
private validateOneToOneRelationship(modelName: string, field: GraphQLField<any, any>, oneToOneAnnotation: RelationshipAnnotation) {
this.validateRelationshipField(modelName, field, oneToOneAnnotation);
if (oneToOneAnnotation.kind !== 'oneToOne') {
throw new Error(`${modelName}.${field.name} should be a @oneToOne field, but has a ${oneToOneAnnotation.kind} annotation`);
}
if (hasListType(field.type)) {
throw new Error(`${modelName}.${field.name} is a list type, but should be an object.`)
}
}
private validateRelationshipField(modelName: string, field: GraphQLField<any, any>, relationshipAnnotation: RelationshipAnnotation) {
if (!relationshipAnnotation) {
throw new Error(`${modelName}.${field.name} is missing a relationship annotation.`)
}
const fieldBaseType = getNamedType(field.type);
if (!isObjectType(fieldBaseType)) {
throw new Error(`${modelName}.${field.name} is marked as a relationship field, but has type ${fieldBaseType.name}. Relationship fields must be object types.`);
}
if (!isModelType(fieldBaseType)) {
throw new Error(`${modelName}.${field.name} is marked as a relationship field, but type ${fieldBaseType.name} is missing the @model annotation.`);
}
}
}