@@ -209,18 +209,38 @@ export class GenerateFragments {
209
209
return {
210
210
name,
211
211
fragment : `fragment ${ name } on ${ name } {
212
- ${ Object . keys ( fields )
213
- . map ( field => {
212
+ ${ Object . keys ( fields ) . map ( field => {
214
213
return this . printField ( field , fields [ field ] , ast ) ;
215
214
} )
216
- // Some fields should not be printed, ie. fields with arguments.
215
+ . filter ( field => field != null )
216
+ . join ( this . indentedLine ( 1 ) ) }
217
+ }
218
+ `
219
+ } ;
220
+
221
+ } ) ;
222
+
223
+ const noRelations = typeNames . map ( typeName => {
224
+ const type : any = ast . getType ( typeName ) ;
225
+ const { name } = type ;
226
+
227
+ const fields : GraphQLFieldMap < any , any > = type . getFields ( ) ;
228
+ return {
229
+ name,
230
+ fragment : `fragment ${ name } NoRelation on ${ name } {
231
+ ${ Object . keys ( fields ) . map ( field => {
232
+ return this . printField ( field , fields [ field ] , ast , true ) ;
233
+ } )
234
+ // Some fields should not be printed, ie. fields with relations.
217
235
// Remove those from the output by returning null from printField.
218
236
. filter ( field => field != null )
219
237
. join ( this . indentedLine ( 1 ) ) }
220
238
}
221
239
`
222
240
} ;
241
+
223
242
} ) ;
243
+
224
244
if ( generator === 'js' ) {
225
245
return `// THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
226
246
// DO NOT EDIT THIS FILE DIRECTLY
@@ -230,7 +250,15 @@ ${definitions
230
250
export const ${ name } Fragment = \`${ fragment } \`
231
251
` ,
232
252
)
233
- . join ( "" ) } `;
253
+ . join ( "" ) }
254
+ ${ noRelations
255
+ . map (
256
+ ( { name, fragment } ) => `
257
+ export const ${ name } NoRelationFragment = \`${ fragment } \`
258
+ ` ,
259
+ )
260
+ . join ( "" ) }
261
+ ` ;
234
262
}
235
263
return `# THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
236
264
# DO NOT EDIT THIS FILE DIRECTLY
@@ -239,11 +267,15 @@ ${definitions
239
267
. map ( ( { name, fragment } ) => `
240
268
${ fragment } `)
241
269
. join ( "" ) }
270
+ ${ noRelations
271
+ . map ( ( { name, fragment } ) => `
272
+ ${ fragment } `)
273
+ . join ( "" ) }
242
274
`
243
275
244
276
}
245
277
246
- private printField ( fieldName , field , ast : GraphQLSchema , indent = 1 ) {
278
+ private printField ( fieldName , field , ast : GraphQLSchema , noRelation = false , indent = 1 ) {
247
279
let constructorName =
248
280
field . type . constructor . name && field . type . constructor . name ;
249
281
if ( constructorName === "Object" )
@@ -257,6 +289,7 @@ ${fragment}`)
257
289
( field . astNode . type . type . type && field . astNode . type . type . type ) || null ;
258
290
constructorName = ast . getType ( field . name . value ) . constructor . name ;
259
291
if ( field === null ) return null ;
292
+ // if(noRelation) return null
260
293
}
261
294
262
295
if ( constructorName === "GraphQLNonNull" || field . kind === "NonNullType" ) {
@@ -282,6 +315,7 @@ ${fragment}`)
282
315
}
283
316
284
317
if ( constructorName === "GraphQLObjectType" ) {
318
+ if ( noRelation ) return null
285
319
let typeName = null ;
286
320
// if(field.name !== undefined)
287
321
typeName =
@@ -293,7 +327,7 @@ ${fragment}`)
293
327
" {" +
294
328
this . indentedLine ( indent + 1 ) +
295
329
"..." +
296
- typeName +
330
+ ` ${ typeName } NoRelation` +
297
331
this . indentedLine ( indent ) +
298
332
"}"
299
333
) ;
0 commit comments