@@ -189,82 +189,67 @@ function printObject(type: GraphQLObjectType, options): string {
189
189
: '' ;
190
190
return (
191
191
printDescription ( options , type ) +
192
- `type ${ type . name } ${ implementedInterfaces } {\n` +
193
- printFields ( options , type ) +
194
- '\n' +
195
- '}'
192
+ `type ${ type . name } ${ implementedInterfaces } ` +
193
+ printFields ( options , type )
196
194
) ;
197
195
}
198
196
199
197
function printInterface ( type : GraphQLInterfaceType , options ) : string {
200
198
return (
201
199
printDescription ( options , type ) +
202
- `interface ${ type . name } {\n` +
203
- printFields ( options , type ) +
204
- '\n' +
205
- '}'
200
+ `interface ${ type . name } ` +
201
+ printFields ( options , type )
206
202
) ;
207
203
}
208
204
209
205
function printUnion ( type : GraphQLUnionType , options ) : string {
210
- return (
211
- printDescription ( options , type ) +
212
- `union ${ type . name } = ${ type . getTypes ( ) . join ( ' | ' ) } `
213
- ) ;
206
+ const types = type . getTypes ( ) ;
207
+ const possibleTypes = types . length ? ' = ' + types . join ( ' | ' ) : '' ;
208
+ return printDescription ( options , type ) + 'union ' + type . name + possibleTypes ;
214
209
}
215
210
216
211
function printEnum ( type : GraphQLEnumType , options ) : string {
217
- return (
218
- printDescription ( options , type ) +
219
- `enum ${ type . name } {\n` +
220
- printEnumValues ( type . getValues ( ) , options ) +
221
- '\n' +
222
- '}'
223
- ) ;
224
- }
225
-
226
- function printEnumValues ( values , options ) : string {
227
- return values
212
+ const values = type
213
+ . getValues ( )
228
214
. map (
229
215
( value , i ) =>
230
216
printDescription ( options , value , ' ' , ! i ) +
231
217
' ' +
232
218
value . name +
233
219
printDeprecated ( value ) ,
234
- )
235
- . join ( '\n' ) ;
220
+ ) ;
221
+
222
+ return (
223
+ printDescription ( options , type ) + `enum ${ type . name } ` + printBlock ( values )
224
+ ) ;
236
225
}
237
226
238
227
function printInputObject ( type : GraphQLInputObjectType , options ) : string {
239
- const fields = objectValues ( type . getFields ( ) ) ;
228
+ const fields = objectValues ( type . getFields ( ) ) . map (
229
+ ( f , i ) =>
230
+ printDescription ( options , f , ' ' , ! i ) + ' ' + printInputValue ( f ) ,
231
+ ) ;
240
232
return (
241
- printDescription ( options , type ) +
242
- `input ${ type . name } {\n` +
243
- fields
244
- . map (
245
- ( f , i ) =>
246
- printDescription ( options , f , ' ' , ! i ) + ' ' + printInputValue ( f ) ,
247
- )
248
- . join ( '\n' ) +
249
- '\n' +
250
- '}'
233
+ printDescription ( options , type ) + `input ${ type . name } ` + printBlock ( fields )
251
234
) ;
252
235
}
253
236
254
237
function printFields ( options , type ) {
255
- const fields = objectValues ( type . getFields ( ) ) ;
256
- return fields
257
- . map (
258
- ( f , i ) =>
259
- printDescription ( options , f , ' ' , ! i ) +
260
- ' ' +
261
- f . name +
262
- printArgs ( options , f . args , ' ' ) +
263
- ': ' +
264
- String ( f . type ) +
265
- printDeprecated ( f ) ,
266
- )
267
- . join ( '\n' ) ;
238
+ const fields = objectValues ( type . getFields ( ) ) . map (
239
+ ( f , i ) =>
240
+ printDescription ( options , f , ' ' , ! i ) +
241
+ ' ' +
242
+ f . name +
243
+ printArgs ( options , f . args , ' ' ) +
244
+ ': ' +
245
+ String ( f . type ) +
246
+ printDeprecated ( f ) ,
247
+ ) ;
248
+ return printBlock ( fields ) ;
249
+ }
250
+
251
+ function printBlock ( items ) {
252
+ return items . length !== 0 ? ' {\n' + items . join ( '\n' ) + '\n}' : '' ;
268
253
}
269
254
270
255
function printArgs ( options , args , indentation = '' ) {
0 commit comments