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 0234ac1

Browse files
Fixed NoNesting bug introduced with empty fragments fix in #4
1 parent cda5279 commit 0234ac1

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-cli-generate-fragments",
3-
"version": "1.3.0",
3+
"version": "1.4.1-beta",
44
"description": "Plugin for graphql-cli to generate fragments",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

‎src/GenerateFragments.ts

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -218,68 +218,48 @@ export class GenerateFragments {
218218
const type: any = ast.getType(typeName);
219219
const { name } = type;
220220

221-
const fields: GraphQLFieldMap<any, any> = type.getFields();
221+
const fields = this.generateFragments(type, ast);
222+
if(fields.length === 0) return null
222223
return {
223224
name,
224225
fragment: `fragment ${name} on ${name} {
225-
${Object.keys(fields)
226-
.map(field => {
227-
return this.printField(field, fields[field], ast);
228-
})
229-
.filter(field => field != null)
230-
.join(this.indentedLine(1))}
226+
${fields.join(this.indentedLine(1))}
231227
}
232228
`
233229
};
234-
});
230+
}).filter(frag=>frag!=null);
235231

236232
const noRelationsFragments = typeNames.map(typeName => {
237233
const type: any = ast.getType(typeName);
238234
const { name } = type;
239235

240-
const fields: GraphQLFieldMap<any, any> = type.getFields();
236+
const fields = this.generateFragments(type, ast, this.fragmentType.NO_RELATIONS);
237+
if(fields.length === 0) return null
238+
241239
return {
242240
name,
243241
fragment: `fragment ${name}${
244242
this.fragmentType.NO_RELATIONS
245243
} on ${name} {
246-
${Object.keys(fields)
247-
.map(field => {
248-
return this.printField(
249-
field,
250-
fields[field],
251-
ast,
252-
this.fragmentType.NO_RELATIONS
253-
);
254-
})
255-
// Some fields should not be printed, ie. fields with relations.
256-
// Remove those from the output by returning null from printField.
257-
.filter(field => field != null)
258-
.join(this.indentedLine(1))}
244+
${fields.join(this.indentedLine(1))}
259245
}
260246
`
261247
};
262-
});
248+
}).filter(frag=>frag!=null);
263249
const deepFragments = typeNames.map(typeName => {
264250
const type: any = ast.getType(typeName);
265251
const { name } = type;
266252

267-
const fields: GraphQLFieldMap<any, any> = type.getFields();
253+
const fields = this.generateFragments(type, ast, this.fragmentType.DEEP);
254+
if(fields.length === 0) return null
268255
return {
269256
name,
270257
fragment: `fragment ${name}${this.fragmentType.DEEP} on ${name} {
271-
${Object.keys(fields)
272-
.map(field => {
273-
return this.printField(field, fields[field], ast, this.fragmentType.DEEP);
274-
})
275-
// Some fields should not be printed, ie. fields with relations.
276-
// Remove those from the output by returning null from printField.
277-
.filter(field => field != null)
278-
.join(this.indentedLine(1))}
258+
${fields.join(this.indentedLine(1))}
279259
}
280260
`
281261
};
282-
});
262+
}).filter(frag=>frag!=null);
283263

284264
if (generator === "js") {
285265
return `// THIS FILE HAS BEEN AUTO-GENERATED BY "graphql-cli-generate-fragments"
@@ -344,11 +324,24 @@ ${fragment}`
344324
`;
345325
}
346326

327+
328+
329+
private generateFragments(type: any, ast: GraphQLSchema, fragmentType = this.fragmentType.DEFAULT) {
330+
const fields: GraphQLFieldMap<any, any> = type.getFields();
331+
const fragmentFields = Object.keys(fields)
332+
.map(field => {
333+
return this.printField(field, fields[field], ast, fragmentType);
334+
})
335+
// Some fields should not be printed, ie. fields with relations.
336+
// Remove those from the output by returning null from printField.
337+
.filter(field => field != null);
338+
return fragmentFields;
339+
}
347340
private printField(
348341
fieldName,
349342
field,
350343
ast: GraphQLSchema,
351-
fragmentType=this.fragmentType.DEFAULT,
344+
fragmentType,
352345
indent = 1
353346
) {
354347
let constructorName =
@@ -393,7 +386,7 @@ ${fragment}`
393386
}
394387

395388
if (constructorName === "GraphQLObjectType") {
396-
// if (fragmentType === this.fragmentType.NO_RELATIONS) return null;
389+
if (fragmentType === this.fragmentType.NO_RELATIONS) return null;
397390
let typeName = null;
398391
// if(field.name !== undefined)
399392
typeName =
@@ -550,3 +543,5 @@ ${fragment}`
550543

551544
private projectDisplayName = () => chalk.green(this.projectName);
552545
}
546+
547+

‎tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"preserveConstEnums": true,
4-
"strictNullChecks": true,
4+
"strictNullChecks": false,
55
"sourceMap": true,
66
"target": "es5",
77
"outDir": "dist",
@@ -10,13 +10,14 @@
1010
"es2017",
1111
"esnext.asynciterable",
1212
"dom"
13-
]
13+
],
1414
},
1515
"exclude": [
1616
"node_modules",
1717
"dist"
1818
],
1919
"include": [
2020
"./src/**/*.ts"
21-
]
21+
],
22+
2223
}

0 commit comments

Comments
(0)

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