Skip to content

Commit cc236e3

Browse files
committed
printSchema: correctly print empty description
Motivation: empty description is definetly a corner case but spec allow them so we should correctly print them
1 parent efa97f6 commit cc236e3

File tree

2 files changed

+137
-6
lines changed

2 files changed

+137
-6
lines changed

src/utilities/__tests__/printSchema-test.ts

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,16 +601,147 @@ describe('Type System Printer', () => {
601601
`);
602602
});
603603

604-
it('Prints an empty description', () => {
605-
const schema = buildSingleFieldSchema({
606-
type: GraphQLString,
604+
it('Prints an empty descriptions', () => {
605+
const args = {
606+
someArg: {
607+
description: '',
608+
type: GraphQLString,
609+
},
610+
anotherArg: {
611+
description: '',
612+
type: GraphQLString,
613+
},
614+
};
615+
616+
const fields = {
617+
someField: {
618+
description: '',
619+
type: GraphQLString,
620+
args,
621+
},
622+
anotherField: {
623+
description: '',
624+
type: GraphQLString,
625+
args,
626+
},
627+
};
628+
629+
const queryType = new GraphQLObjectType({
630+
name: 'Query',
631+
description: '',
632+
fields,
633+
});
634+
635+
const scalarType = new GraphQLScalarType({
636+
name: 'SomeScalar',
637+
description: '',
638+
});
639+
640+
const interfaceType = new GraphQLInterfaceType({
641+
name: 'SomeInterface',
642+
description: '',
643+
fields,
644+
});
645+
646+
const unionType = new GraphQLUnionType({
647+
name: 'SomeUnion',
648+
description: '',
649+
types: [queryType],
650+
});
651+
652+
const enumType = new GraphQLEnumType({
653+
name: 'SomeEnum',
654+
description: '',
655+
values: {
656+
SOME_VALUE: { description: '' },
657+
ANOTHER_VALUE: { description: '' },
658+
},
659+
});
660+
661+
const someDirecive = new GraphQLDirective({
662+
name: 'someDirective',
663+
description: '',
664+
args,
665+
locations: [DirectiveLocation.QUERY],
666+
});
667+
668+
const schema = new GraphQLSchema({
607669
description: '',
670+
query: queryType,
671+
types: [scalarType, interfaceType, unionType, enumType],
672+
directives: [someDirecive],
608673
});
609674

610675
expectPrintedSchema(schema).to.equal(dedent`
676+
""""""
677+
schema {
678+
query: Query
679+
}
680+
681+
""""""
682+
directive @someDirective(
683+
""""""
684+
someArg: String
685+
686+
""""""
687+
anotherArg: String
688+
) on QUERY
689+
690+
""""""
691+
scalar SomeScalar
692+
693+
""""""
694+
interface SomeInterface {
695+
""""""
696+
someField(
697+
""""""
698+
someArg: String
699+
700+
""""""
701+
anotherArg: String
702+
): String
703+
704+
""""""
705+
anotherField(
706+
""""""
707+
someArg: String
708+
709+
""""""
710+
anotherArg: String
711+
): String
712+
}
713+
714+
""""""
715+
union SomeUnion = Query
716+
717+
""""""
611718
type Query {
612719
""""""
613-
singleField: String
720+
someField(
721+
""""""
722+
someArg: String
723+
724+
""""""
725+
anotherArg: String
726+
): String
727+
728+
""""""
729+
anotherField(
730+
""""""
731+
someArg: String
732+
733+
""""""
734+
anotherArg: String
735+
): String
736+
}
737+
738+
""""""
739+
enum SomeEnum {
740+
""""""
741+
SOME_VALUE
742+
743+
""""""
744+
ANOTHER_VALUE
614745
}
615746
`);
616747
});

src/utilities/printSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
8282

8383
// Only print a schema definition if there is a description or if it should
8484
// not be omitted because of having default type names.
85-
if (schema.description || !hasDefaultRootOperationTypes(schema)) {
85+
if (schema.description != null || !hasDefaultRootOperationTypes(schema)) {
8686
return (
8787
printDescription(schema) +
8888
'schema {\n' +
@@ -234,7 +234,7 @@ function printArgs(
234234
}
235235

236236
// If every arg does not have a description, print them on one line.
237-
if (args.every((arg) => !arg.description)) {
237+
if (args.every((arg) => arg.description == null)) {
238238
return '(' + args.map(printInputValue).join(', ') + ')';
239239
}
240240

0 commit comments

Comments
 (0)