forked from unlight/prisma-nestjs-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output-type.ts
176 lines (153 loc) · 5.88 KB
/
output-type.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
import { ok } from 'assert';
import JSON5 from 'json5';
import { castArray } from 'lodash';
import { ClassDeclarationStructure, StructureKind } from 'ts-morph';
import { getGraphqlImport } from '../helpers/get-graphql-import';
import { getOutputTypeName } from '../helpers/get-output-type-name';
import { getPropertyType } from '../helpers/get-property-type';
import { ImportDeclarationMap } from '../helpers/import-declaration-map';
import { propertyStructure } from '../helpers/property-structure';
import { EventArguments, OutputType } from '../types';
const nestjsGraphql = '@nestjs/graphql';
export function outputType(outputType: OutputType, args: EventArguments) {
const { getSourceFile, models, eventEmitter, fieldSettings, getModelName } = args;
const importDeclarations = new ImportDeclarationMap();
const fileType = 'output';
const modelName = getModelName(outputType.name) || '';
const model = models.get(modelName);
const isAggregateOutput =
model &&
/(?:Count|Avg|Sum|Min|Max)AggregateOutputType$/.test(outputType.name) &&
String(outputType.name).startsWith(model.name);
const isCountOutput =
model?.name && outputType.name === `${model.name}CountOutputType`;
// Get rid of bogus suffixes
outputType.name = getOutputTypeName(outputType.name);
if (isAggregateOutput) {
eventEmitter.emitSync('AggregateOutput', { ...args, outputType });
}
const sourceFile = getSourceFile({
name: outputType.name,
type: fileType,
});
const classStructure: ClassDeclarationStructure = {
kind: StructureKind.Class,
isExported: true,
name: outputType.name,
decorators: [
{
name: 'ObjectType',
arguments: [],
},
],
properties: [],
};
importDeclarations.add('Field', nestjsGraphql);
importDeclarations.add('ObjectType', nestjsGraphql);
for (const field of outputType.fields) {
const { location, isList, type } = field.outputType;
const outputTypeName = getOutputTypeName(String(type));
const settings = isCountOutput
? undefined
: model && fieldSettings.get(model.name)?.get(field.name);
const propertySettings = settings?.getPropertyType({
name: outputType.name,
output: true,
});
const isCustomsApplicable =
outputTypeName === model?.fields.find(f => f.name === field.name)?.type;
field.outputType.type = outputTypeName;
const propertyType = castArray(
propertySettings?.name ||
getPropertyType({
location,
type: outputTypeName,
}),
);
const property = propertyStructure({
name: field.name,
isNullable: field.isNullable,
propertyType,
isList,
});
classStructure.properties?.push(property);
if (propertySettings) {
importDeclarations.create({ ...propertySettings });
}
// Get graphql type
let graphqlType: string;
const shouldHideField = settings?.shouldHideField({
name: outputType.name,
output: true,
});
const fieldType = settings?.getFieldType({
name: outputType.name,
output: true,
});
if (fieldType && isCustomsApplicable && !shouldHideField) {
graphqlType = fieldType.name;
importDeclarations.create({ ...fieldType });
} else {
const graphqlImport = getGraphqlImport({
sourceFile,
fileType,
location,
isId: false,
typeName: outputTypeName,
getSourceFile,
});
graphqlType = graphqlImport.name;
if (
graphqlImport.specifier &&
!importDeclarations.has(graphqlImport.name) &&
((graphqlImport.name !== outputType.name && !shouldHideField) ||
(shouldHideField && propertyType[0] === graphqlImport.name))
) {
importDeclarations.set(graphqlImport.name, {
namedImports: [{ name: graphqlImport.name }],
moduleSpecifier: graphqlImport.specifier,
});
}
}
ok(property.decorators, 'property.decorators is undefined');
if (shouldHideField) {
importDeclarations.add('HideField', nestjsGraphql);
property.decorators.push({ name: 'HideField', arguments: [] });
} else {
// Generate `@Field()` decorator
property.decorators.push({
name: 'Field',
arguments: [
isList ? `() => [${graphqlType}]` : `() => ${graphqlType}`,
JSON5.stringify({
nullable: Boolean(field.isNullable),
}),
],
});
if (isCustomsApplicable) {
for (const options of settings || []) {
if (!options.output || options.kind !== 'Decorator') {
continue;
}
property.decorators.push({
name: options.name,
arguments: options.arguments as string[],
});
ok(
options.from,
"Missed 'from' part in configuration or field setting",
);
importDeclarations.create(options);
}
}
}
eventEmitter.emitSync('ClassProperty', property, {
location,
isList,
propertyType,
});
}
sourceFile.set({
statements: [...importDeclarations.toStatements(), classStructure],
});
}