Skip to content

Commit ebec940

Browse files
printSchema: Fix printing of empty types (#1707)
1 parent 34b7ced commit ebec940

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

src/utilities/__tests__/schemaPrinter-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,31 @@ describe('Type System Printer', () => {
438438
`);
439439
});
440440

441+
it('Prints empty types', () => {
442+
const Schema = new GraphQLSchema({
443+
types: [
444+
new GraphQLEnumType({ name: 'SomeEnum', values: {} }),
445+
new GraphQLInputObjectType({ name: 'SomeInputObject', fields: {} }),
446+
new GraphQLInterfaceType({ name: 'SomeInterface', fields: {} }),
447+
new GraphQLObjectType({ name: 'SomeObject', fields: {} }),
448+
new GraphQLUnionType({ name: 'SomeUnion', types: [] }),
449+
],
450+
});
451+
452+
const output = printForTest(Schema);
453+
expect(output).to.equal(dedent`
454+
enum SomeEnum
455+
456+
input SomeInputObject
457+
458+
interface SomeInterface
459+
460+
type SomeObject
461+
462+
union SomeUnion
463+
`);
464+
});
465+
441466
it('Prints custom directives', () => {
442467
const CustomDirective = new GraphQLDirective({
443468
name: 'customDirective',

src/utilities/schemaPrinter.js

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -189,82 +189,67 @@ function printObject(type: GraphQLObjectType, options): string {
189189
: '';
190190
return (
191191
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)
196194
);
197195
}
198196

199197
function printInterface(type: GraphQLInterfaceType, options): string {
200198
return (
201199
printDescription(options, type) +
202-
`interface ${type.name} {\n` +
203-
printFields(options, type) +
204-
'\n' +
205-
'}'
200+
`interface ${type.name}` +
201+
printFields(options, type)
206202
);
207203
}
208204

209205
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;
214209
}
215210

216211
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()
228214
.map(
229215
(value, i) =>
230216
printDescription(options, value, ' ', !i) +
231217
' ' +
232218
value.name +
233219
printDeprecated(value),
234-
)
235-
.join('\n');
220+
);
221+
222+
return (
223+
printDescription(options, type) + `enum ${type.name}` + printBlock(values)
224+
);
236225
}
237226

238227
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+
);
240232
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)
251234
);
252235
}
253236

254237
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}' : '';
268253
}
269254

270255
function printArgs(options, args, indentation = '') {

0 commit comments

Comments
 (0)