1818import java .io .PrintWriter ;
1919import java .io .StringWriter ;
2020import java .util .ArrayList ;
21+ import java .util .Arrays ;
2122import java .util .Comparator ;
2223import java .util .LinkedHashMap ;
2324import java .util .List ;
2425import java .util .Map ;
2526import java .util .Stack ;
27+ import java .util .stream .Stream ;
2628
2729/**
2830 * This can print an in memory GraphQL schema back to a logical schema definition
@@ -58,7 +60,6 @@ public static Options defaultOptions() {
5860 * This will allow you to include introspection types that are contained in a schema
5961 *
6062 * @param flag whether to include them
61- *
6263 * @return options
6364 */
6465 public Options includeIntrospectionTypes (boolean flag ) {
@@ -69,7 +70,6 @@ public Options includeIntrospectionTypes(boolean flag) {
6970 * This will allow you to include scalar types that are contained in a schema
7071 *
7172 * @param flag whether to include them
72- *
7373 * @return options
7474 */
7575 public Options includeScalarTypes (boolean flag ) {
@@ -99,7 +99,6 @@ public SchemaPrinter(Options options) {
9999 * This can print an in memory GraphQL schema back to a logical schema definition
100100 *
101101 * @param schema the schema in play
102- *
103102 * @return the logical schema definition
104103 */
105104 public String print (GraphQLSchema schema ) {
@@ -192,6 +191,7 @@ private TypePrinter<GraphQLObjectType> objectPrinter() {
192191 if (isIntrospectionType (type )) {
193192 return ;
194193 }
194+ printComments (out , type );
195195 out .format ("type %s {\n " , type .getName ());
196196 type .getFieldDefinitions ().forEach (fd ->
197197 out .format (" %s%s : %s\n " ,
@@ -330,4 +330,12 @@ private void printType(PrintWriter out, GraphQLType type) {
330330 TypePrinter <Object > printer = printer (type .getClass ());
331331 printer .print (out , type );
332332 }
333+
334+ void printComments (PrintWriter out , GraphQLObjectType graphQLType ) {
335+ if (graphQLType .getDescription () == null ) {
336+ return ;
337+ }
338+ Stream <String > stream = Arrays .stream (graphQLType .getDescription ().split ("\n " ));
339+ stream .map (s -> "#" + s + "\n " ).forEach (out ::write );
340+ }
333341}
0 commit comments