1919import graphql .language .ObjectTypeDefinition ;
2020import graphql .language .ScalarTypeDefinition ;
2121import graphql .language .SchemaDefinition ;
22+ import graphql .language .SchemaExtensionDefinition ;
2223import graphql .language .TypeDefinition ;
2324import graphql .language .UnionTypeDefinition ;
2425import graphql .schema .DefaultGraphqlTypeComparatorRegistry ;
@@ -283,7 +284,7 @@ public Options includeDirectiveDefinitions(boolean flag) {
283284 /**
284285 * This is a Predicate that decides whether a directive definition is printed.
285286 *
286- * @param includeDirectiveDefinition the predicate to decide of a directive defintion is printed
287+ * @param includeDirectiveDefinition the predicate to decide of a directive definition is printed
287288 *
288289 * @return new instance of options
289290 */
@@ -482,7 +483,7 @@ public SchemaPrinter(Options options) {
482483
483484 /**
484485 * This can print an in memory GraphQL IDL document back to a logical schema definition.
485- * If you want to turn a Introspection query result into a Document (and then into a printed
486+ * If you want to turn an Introspection query result into a Document (and then into a printed
486487 * schema) then use {@link graphql.introspection.IntrospectionResultToSchema#createSchemaDefinition(java.util.Map)}
487488 * first to get the {@link graphql.language.Document} and then print that.
488489 *
@@ -773,6 +774,17 @@ private boolean shouldPrintAsAst(TypeDefinition<?> definition) {
773774 return options .isUseAstDefinitions () && definition != null ;
774775 }
775776
777+ /**
778+ * This will return true if the options say to use the AST and we have an AST element
779+ *
780+ * @param definition the AST schema definition
781+ *
782+ * @return true if we should print using AST nodes
783+ */
784+ private boolean shouldPrintAsAst (SchemaDefinition definition ) {
785+ return options .isUseAstDefinitions () && definition != null ;
786+ }
787+
776788 /**
777789 * This will print out a runtime graphql schema element using its contained AST type definition. This
778790 * must be guarded by a called to {@link #shouldPrintAsAst(TypeDefinition)}
@@ -792,6 +804,25 @@ private void printAsAst(PrintWriter out, TypeDefinition<?> definition, List<? ex
792804 out .print ('\n' );
793805 }
794806
807+ /**
808+ * This will print out a runtime graphql schema block using its AST definition. This
809+ * must be guarded by a called to {@link #shouldPrintAsAst(SchemaDefinition)}
810+ *
811+ * @param out the output writer
812+ * @param definition the AST schema definition
813+ * @param extensions a list of schema definition extensions
814+ */
815+ private void printAsAst (PrintWriter out , SchemaDefinition definition , List <SchemaExtensionDefinition > extensions ) {
816+ out .printf ("%s\n " , AstPrinter .printAst (definition ));
817+ if (extensions != null ) {
818+ for (SchemaExtensionDefinition extension : extensions ) {
819+ out .printf ("\n %s\n " , AstPrinter .printAst (extension ));
820+ }
821+ }
822+ out .print ('\n' );
823+ }
824+
825+
795826 private static String printAst (InputValueWithState value , GraphQLInputType type ) {
796827 return AstPrinter .printAst (ValuesResolver .valueToLiteral (value , type , GraphQLContext .getDefault (), Locale .getDefault ()));
797828 }
@@ -803,7 +834,7 @@ private SchemaElementPrinter<GraphQLSchema> schemaPrinter() {
803834 GraphQLObjectType subscriptionType = schema .getSubscriptionType ();
804835
805836 // when serializing a GraphQL schema using the type system language, a
806- // schema definition should be omitted if only uses the default root type names.
837+ // schema definition should be omitted only if it uses the default root type names.
807838 boolean needsSchemaPrinted = options .isIncludeSchemaDefinition ();
808839
809840 if (!needsSchemaPrinted ) {
@@ -819,21 +850,25 @@ private SchemaElementPrinter<GraphQLSchema> schemaPrinter() {
819850 }
820851
821852 if (needsSchemaPrinted ) {
822- if (hasAstDefinitionComments (schema ) || hasDescription (schema )) {
823- out .print (printComments (schema , "" ));
824- }
825- List <GraphQLAppliedDirective > directives = DirectivesUtil .toAppliedDirectives (schema .getSchemaAppliedDirectives (), schema .getSchemaDirectives ());
826- out .format ("schema %s{\n " , directivesString (GraphQLSchemaElement .class , directives ));
827- if (queryType != null ) {
828- out .format (" query: %s\n " , queryType .getName ());
829- }
830- if (mutationType != null ) {
831- out .format (" mutation: %s\n " , mutationType .getName ());
832- }
833- if (subscriptionType != null ) {
834- out .format (" subscription: %s\n " , subscriptionType .getName ());
853+ if (shouldPrintAsAst (schema .getDefinition ())) {
854+ printAsAst (out , schema .getDefinition (), schema .getExtensionDefinitions ());
855+ } else {
856+ if (hasAstDefinitionComments (schema ) || hasDescription (schema )) {
857+ out .print (printComments (schema , "" ));
858+ }
859+ List <GraphQLAppliedDirective > directives = DirectivesUtil .toAppliedDirectives (schema .getSchemaAppliedDirectives (), schema .getSchemaDirectives ());
860+ out .format ("schema %s{\n " , directivesString (GraphQLSchemaElement .class , directives ));
861+ if (queryType != null ) {
862+ out .format (" query: %s\n " , queryType .getName ());
863+ }
864+ if (mutationType != null ) {
865+ out .format (" mutation: %s\n " , mutationType .getName ());
866+ }
867+ if (subscriptionType != null ) {
868+ out .format (" subscription: %s\n " , subscriptionType .getName ());
869+ }
870+ out .format ("}\n \n " );
835871 }
836- out .format ("}\n \n " );
837872 }
838873 };
839874 }
0 commit comments