Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce304d5

Browse files
author
Mark
committed
Added "DeepNesting" fragments if there are no recursive fragments spreading
1 parent 3534ac2 commit ce304d5

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

‎src/GenerateFragments.ts

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ export class GenerateFragments {
169169
return line;
170170
}
171171

172+
private fragmentType = {
173+
DEFAULT: "",
174+
NO_RELATIONS: "NoNesting",
175+
DEEP: "DeepNesting",
176+
};
177+
172178
private makeFragments(schemaContents: string, generator: string) {
173179
const document: DocumentNode = parse(schemaContents, { noLocation: true });
174180
const ast: GraphQLSchema = buildASTSchema(document);
@@ -199,9 +205,10 @@ export class GenerateFragments {
199205
: 1
200206
);
201207

208+
202209
// console.log(typeNames)
203210

204-
const definitions = typeNames.map(typeName => {
211+
const standardFragments = typeNames.map(typeName => {
205212
const type: any = ast.getType(typeName);
206213
const { name } = type;
207214

@@ -220,16 +227,36 @@ export class GenerateFragments {
220227

221228
});
222229

223-
const noRelations = typeNames.map(typeName => {
230+
const noRelationsFragments = typeNames.map(typeName => {
224231
const type: any = ast.getType(typeName);
225232
const { name } = type;
226233

227234
const fields: GraphQLFieldMap<any, any> = type.getFields();
228235
return {
229236
name,
230-
fragment: `fragment ${name}NoRelation on ${name} {
237+
fragment: `fragment ${name}${this.fragmentType.NO_RELATIONS} on ${name} {
231238
${Object.keys(fields).map(field => {
232-
return this.printField(field, fields[field], ast, true);
239+
return this.printField(field, fields[field], ast, this.fragmentType.NO_RELATIONS);
240+
})
241+
// Some fields should not be printed, ie. fields with relations.
242+
// Remove those from the output by returning null from printField.
243+
.filter(field => field != null)
244+
.join(this.indentedLine(1))}
245+
}
246+
`
247+
};
248+
249+
});
250+
const deepFragments = typeNames.map(typeName => {
251+
const type: any = ast.getType(typeName);
252+
const { name } = type;
253+
254+
const fields: GraphQLFieldMap<any, any> = type.getFields();
255+
return {
256+
name,
257+
fragment: `fragment ${name}${this.fragmentType.DEEP} on ${name} {
258+
${Object.keys(fields).map(field => {
259+
return this.printField(field, fields[field], ast, this.fragmentType.DEEP);
233260
})
234261
// Some fields should not be printed, ie. fields with relations.
235262
// Remove those from the output by returning null from printField.
@@ -244,38 +271,62 @@ export class GenerateFragments {
244271
if (generator === 'js'){
245272
return `// THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
246273
// DO NOT EDIT THIS FILE DIRECTLY
247-
${definitions
274+
${standardFragments
248275
.map(
249276
({ name, fragment }) => `
250277
export const ${name}Fragment = \`${fragment}\`
251278
`,
252279
)
253280
.join("")}
254-
${noRelations
281+
${noRelationsFragments
255282
.map(
256283
({ name, fragment }) => `
257-
export const ${name}NoRelationFragment = \`${fragment}\`
284+
export const ${name}${this.fragmentType.NO_RELATIONS}Fragment = \`${fragment}\`
258285
`,
259286
)
260287
.join("")}
288+
${deepFragments
289+
.map(
290+
({ name, fragment }) => `
291+
export const ${name}${this.fragmentType.DEEP}Fragment = \`${fragment}\`
292+
`,
293+
)
294+
.join("")}
261295
`;
262296
}
263297
return `# THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
264298
# DO NOT EDIT THIS FILE DIRECTLY
265299
266-
${definitions
300+
# Standard Fragments
301+
# Nested fragments will spread one layer deep
302+
303+
${standardFragments
267304
.map(({ name, fragment }) => `
268305
${fragment}`)
269306
.join("")}
270-
${noRelations
307+
308+
# No Relational objects
309+
# No nested fragments
310+
311+
${noRelationsFragments
312+
.map(({ name, fragment }) => `
313+
${fragment}`)
314+
.join("")}
315+
316+
# Deeply nested Fragments
317+
# Will include n nested fragments
318+
# If there is a recursive relation you will receive a
319+
# "Cannot spread fragment within itself" error when using
320+
321+
${deepFragments
271322
.map(({ name, fragment }) => `
272323
${fragment}`)
273324
.join("")}
274325
`
275326

276327
}
277328

278-
private printField(fieldName, field, ast: GraphQLSchema, noRelation = false, indent = 1) {
329+
private printField(fieldName, field, ast: GraphQLSchema, fragmentType = this.fragmentType.DEFAULT, indent = 1) {
279330
let constructorName =
280331
field.type.constructor.name && field.type.constructor.name;
281332
if (constructorName === "Object")
@@ -315,7 +366,7 @@ ${fragment}`)
315366
}
316367

317368
if (constructorName === "GraphQLObjectType") {
318-
if(noRelation) return null
369+
if(fragmentType===this.fragmentType.NO_RELATIONS) return null
319370
let typeName = null;
320371
// if(field.name !== undefined)
321372
typeName =
@@ -327,7 +378,7 @@ ${fragment}`)
327378
" {" +
328379
this.indentedLine(indent + 1) +
329380
"..." +
330-
`${typeName}NoRelation` +
381+
`${(fragmentType===this.fragmentType.DEFAULT)&&typeName+this.fragmentType.NO_RELATIONS||typeName+this.fragmentType.DEFAULT}` +
331382
this.indentedLine(indent) +
332383
"}"
333384
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /