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 3534ac2

Browse files
author
Mark
committed
NoRelation fragments to prevent recursive fragment spreads > cannot spread fragment error
1 parent 6d2f8da commit 3534ac2

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

‎src/GenerateFragments.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,38 @@ export class GenerateFragments {
209209
return {
210210
name,
211211
fragment: `fragment ${name} on ${name} {
212-
${Object.keys(fields)
213-
.map(field => {
212+
${Object.keys(fields).map(field => {
214213
return this.printField(field, fields[field], ast);
215214
})
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.
217235
// Remove those from the output by returning null from printField.
218236
.filter(field => field != null)
219237
.join(this.indentedLine(1))}
220238
}
221239
`
222240
};
241+
223242
});
243+
224244
if (generator === 'js'){
225245
return `// THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
226246
// DO NOT EDIT THIS FILE DIRECTLY
@@ -230,7 +250,15 @@ ${definitions
230250
export const ${name}Fragment = \`${fragment}\`
231251
`,
232252
)
233-
.join("")}`;
253+
.join("")}
254+
${noRelations
255+
.map(
256+
({ name, fragment }) => `
257+
export const ${name}NoRelationFragment = \`${fragment}\`
258+
`,
259+
)
260+
.join("")}
261+
`;
234262
}
235263
return `# THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
236264
# DO NOT EDIT THIS FILE DIRECTLY
@@ -239,11 +267,15 @@ ${definitions
239267
.map(({ name, fragment }) => `
240268
${fragment}`)
241269
.join("")}
270+
${noRelations
271+
.map(({ name, fragment }) => `
272+
${fragment}`)
273+
.join("")}
242274
`
243275

244276
}
245277

246-
private printField(fieldName, field, ast: GraphQLSchema, indent = 1) {
278+
private printField(fieldName, field, ast: GraphQLSchema, noRelation=false,indent = 1) {
247279
let constructorName =
248280
field.type.constructor.name && field.type.constructor.name;
249281
if (constructorName === "Object")
@@ -257,6 +289,7 @@ ${fragment}`)
257289
(field.astNode.type.type.type && field.astNode.type.type.type) || null;
258290
constructorName = ast.getType(field.name.value).constructor.name;
259291
if (field === null) return null;
292+
// if(noRelation) return null
260293
}
261294

262295
if (constructorName === "GraphQLNonNull" || field.kind === "NonNullType") {
@@ -282,6 +315,7 @@ ${fragment}`)
282315
}
283316

284317
if (constructorName === "GraphQLObjectType") {
318+
if(noRelation) return null
285319
let typeName = null;
286320
// if(field.name !== undefined)
287321
typeName =
@@ -293,7 +327,7 @@ ${fragment}`)
293327
" {" +
294328
this.indentedLine(indent + 1) +
295329
"..." +
296-
typeName +
330+
`${typeName}NoRelation` +
297331
this.indentedLine(indent) +
298332
"}"
299333
);

0 commit comments

Comments
(0)

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