1
1
import { ok } from 'assert' ;
2
2
import JSON5 from 'json5' ;
3
- import { castArray , last } from 'lodash' ;
3
+ import { castArray } from 'lodash' ;
4
4
import { ClassDeclarationStructure , StructureKind } from 'ts-morph' ;
5
5
6
+ import { getEnumName } from '../helpers/get-enum-name' ;
6
7
import { getGraphqlImport } from '../helpers/get-graphql-import' ;
7
8
import { getOutputTypeName } from '../helpers/get-output-type-name' ;
8
9
import { getPropertyType } from '../helpers/get-property-type' ;
@@ -13,7 +14,7 @@ import { EventArguments, OutputType } from '../types';
13
14
const nestjsGraphql = '@nestjs/graphql' ;
14
15
15
16
export function outputType ( outputType : OutputType , args : EventArguments ) {
16
- const { getSourceFile , models , eventEmitter, fieldSettings, getModelName, config } =
17
+ const { config , eventEmitter, fieldSettings, getModelName, getSourceFile , models } =
17
18
args ;
18
19
const importDeclarations = new ImportDeclarationMap ( ) ;
19
20
@@ -42,23 +43,23 @@ export function outputType(outputType: OutputType, args: EventArguments) {
42
43
} ) ;
43
44
44
45
const classStructure : ClassDeclarationStructure = {
45
- kind : StructureKind . Class ,
46
- isExported : true ,
47
- name : outputType . name ,
48
46
decorators : [
49
47
{
50
- name : 'ObjectType' ,
51
48
arguments : [ ] ,
49
+ name : 'ObjectType' ,
52
50
} ,
53
51
] ,
52
+ isExported : true ,
53
+ kind : StructureKind . Class ,
54
+ name : outputType . name ,
54
55
properties : [ ] ,
55
56
} ;
56
57
57
58
importDeclarations . add ( 'Field' , nestjsGraphql ) ;
58
59
importDeclarations . add ( 'ObjectType' , nestjsGraphql ) ;
59
60
60
61
for ( const field of outputType . fields ) {
61
- const { location , isList , type } = field . outputType ;
62
+ const { isList , location , type } = field . outputType ;
62
63
const outputTypeName = getOutputTypeName ( String ( type ) ) ;
63
64
const settings = isCountOutput
64
65
? undefined
@@ -67,6 +68,7 @@ export function outputType(outputType: OutputType, args: EventArguments) {
67
68
name : outputType . name ,
68
69
output : true ,
69
70
} ) ;
71
+
70
72
const isCustomsApplicable =
71
73
outputTypeName === model ?. fields . find ( f => f . name === field . name ) ?. type ;
72
74
@@ -81,11 +83,11 @@ export function outputType(outputType: OutputType, args: EventArguments) {
81
83
) ;
82
84
83
85
const property = propertyStructure ( {
84
- name : field . name ,
85
- isNullable : field . isNullable ,
86
86
hasQuestionToken : isCountOutput ? true : undefined ,
87
- propertyType,
88
87
isList,
88
+ isNullable : field . isNullable ,
89
+ name : field . name ,
90
+ propertyType,
89
91
} ) ;
90
92
91
93
classStructure . properties ?. push ( property ) ;
@@ -122,19 +124,17 @@ export function outputType(outputType: OutputType, args: EventArguments) {
122
124
} else {
123
125
const graphqlImport = getGraphqlImport ( {
124
126
config,
125
- sourceFile,
126
127
fileType,
127
- location ,
128
+ getSourceFile ,
128
129
isId : false ,
130
+ location,
131
+ sourceFile,
129
132
typeName : outputTypeName ,
130
- getSourceFile,
131
133
} ) ;
134
+ const referenceName =
135
+ location === 'enumTypes' ? getEnumName ( propertyType [ 0 ] ) : propertyType [ 0 ] ;
132
136
133
137
graphqlType = graphqlImport . name ;
134
- let referenceName = propertyType [ 0 ] ;
135
- if ( location === 'enumTypes' ) {
136
- referenceName = last ( referenceName . split ( ' ' ) ) as string ;
137
- }
138
138
139
139
if (
140
140
graphqlImport . specifier &&
@@ -143,8 +143,8 @@ export function outputType(outputType: OutputType, args: EventArguments) {
143
143
( shouldHideField && referenceName === graphqlImport . name ) )
144
144
) {
145
145
importDeclarations . set ( graphqlImport . name , {
146
- namedImports : [ { name : graphqlImport . name } ] ,
147
146
moduleSpecifier : graphqlImport . specifier ,
147
+ namedImports : [ { name : graphqlImport . name } ] ,
148
148
} ) ;
149
149
}
150
150
}
@@ -153,18 +153,18 @@ export function outputType(outputType: OutputType, args: EventArguments) {
153
153
154
154
if ( shouldHideField ) {
155
155
importDeclarations . add ( 'HideField' , nestjsGraphql ) ;
156
- property . decorators . push ( { name : 'HideField' , arguments : [ ] } ) ;
156
+ property . decorators . push ( { arguments : [ ] , name : 'HideField' } ) ;
157
157
} else {
158
158
// Generate `@Field()` decorator
159
159
property . decorators . push ( {
160
- name : 'Field' ,
161
160
arguments : [
162
161
isList ? `() => [${ graphqlType } ]` : `() => ${ graphqlType } ` ,
163
162
JSON5 . stringify ( {
164
163
...settings ?. fieldArguments ( ) ,
165
164
nullable : Boolean ( field . isNullable ) ,
166
165
} ) ,
167
166
] ,
167
+ name : 'Field' ,
168
168
} ) ;
169
169
170
170
if ( isCustomsApplicable ) {
@@ -176,8 +176,8 @@ export function outputType(outputType: OutputType, args: EventArguments) {
176
176
true
177
177
) {
178
178
property . decorators . push ( {
179
- name : options . name ,
180
179
arguments : options . arguments as string [ ] ,
180
+ name : options . name ,
181
181
} ) ;
182
182
ok ( options . from , "Missed 'from' part in configuration or field setting" ) ;
183
183
importDeclarations . create ( options ) ;
@@ -187,8 +187,8 @@ export function outputType(outputType: OutputType, args: EventArguments) {
187
187
}
188
188
189
189
eventEmitter . emitSync ( 'ClassProperty' , property , {
190
- location,
191
190
isList,
191
+ location,
192
192
propertyType,
193
193
} ) ;
194
194
}
0 commit comments