@@ -70,6 +70,26 @@ func getMapValueString(m map[string]interface{}, key string) string {
7070 }
7171 return ""
7272}
73+ func getDescription (raw interface {}) string {
74+ var desc string
75+
76+ switch node := raw .(type ) {
77+ case ast.DescribableNode :
78+ if sval := node .GetDescription (); sval != nil {
79+ desc = sval .Value
80+ }
81+ case map [string ]interface {}:
82+ desc = getMapValueString (node , "Description.Value" )
83+ }
84+ if desc != "" {
85+ sep := ""
86+ if strings .ContainsRune (desc , '\n' ) {
87+ sep = "\n "
88+ }
89+ desc = join ([]string {`"""` , desc , `"""` }, sep )
90+ }
91+ return desc
92+ }
7393
7494func toSliceString (slice interface {}) []string {
7595 if slice == nil {
@@ -506,6 +526,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
506526 fmt .Sprintf ("%v" , node .Name ),
507527 join (directives , " " ),
508528 }, " " )
529+ if desc := getDescription (node ); desc != "" {
530+ str = fmt .Sprintf ("%s\n %s" , desc , str )
531+ }
509532 return visitor .ActionUpdate , str
510533 case map [string ]interface {}:
511534 name := getMapValueString (node , "Name" )
@@ -518,6 +541,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
518541 name ,
519542 join (directives , " " ),
520543 }, " " )
544+ if desc := getDescription (node ); desc != "" {
545+ str = fmt .Sprintf ("%s\n %s" , desc , str )
546+ }
521547 return visitor .ActionUpdate , str
522548 }
523549 return visitor .ActionNoChange , nil
@@ -539,6 +565,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
539565 join (directives , " " ),
540566 block (fields ),
541567 }, " " )
568+ if desc := getDescription (node ); desc != "" {
569+ str = fmt .Sprintf ("%s\n %s" , desc , str )
570+ }
542571 return visitor .ActionUpdate , str
543572 case map [string ]interface {}:
544573 name := getMapValueString (node , "Name" )
@@ -555,6 +584,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
555584 join (directives , " " ),
556585 block (fields ),
557586 }, " " )
587+ if desc := getDescription (node ); desc != "" {
588+ str = fmt .Sprintf ("%s\n %s" , desc , str )
589+ }
558590 return visitor .ActionUpdate , str
559591 }
560592 return visitor .ActionNoChange , nil
@@ -569,7 +601,23 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
569601 for _ , directive := range node .Directives {
570602 directives = append (directives , fmt .Sprintf ("%v" , directive .Name ))
571603 }
572- str := name + wrap ("(" , join (args , ", " ), ")" ) + ": " + ttype + wrap (" " , join (directives , " " ), "" )
604+ hasArgDesc := false
605+ for _ , arg := range node .Arguments {
606+ if arg .Description != nil && arg .Description .Value != "" {
607+ hasArgDesc = true
608+ break
609+ }
610+ }
611+ var argsStr string
612+ if hasArgDesc {
613+ argsStr = wrap ("(" , indent ("\n " + join (args , "\n " )), "\n )" )
614+ } else {
615+ argsStr = wrap ("(" , join (args , ", " ), ")" )
616+ }
617+ str := name + argsStr + ": " + ttype + wrap (" " , join (directives , " " ), "" )
618+ if desc := getDescription (node ); desc != "" {
619+ str = fmt .Sprintf ("\n %s\n %s" , desc , str )
620+ }
573621 return visitor .ActionUpdate , str
574622 case map [string ]interface {}:
575623 name := getMapValueString (node , "Name" )
@@ -579,7 +627,23 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
579627 for _ , directive := range getMapSliceValue (node , "Directives" ) {
580628 directives = append (directives , fmt .Sprintf ("%v" , directive ))
581629 }
582- str := name + wrap ("(" , join (args , ", " ), ")" ) + ": " + ttype + wrap (" " , join (directives , " " ), "" )
630+ hasArgDesc := false
631+ for _ , arg := range args {
632+ if strings .HasPrefix (strings .TrimSpace (arg ), `"""` ) {
633+ hasArgDesc = true
634+ break
635+ }
636+ }
637+ var argsStr string
638+ if hasArgDesc {
639+ argsStr = wrap ("(" , indent ("\n " + join (args , "\n " )), "\n )" )
640+ } else {
641+ argsStr = wrap ("(" , join (args , ", " ), ")" )
642+ }
643+ str := name + argsStr + ": " + ttype + wrap (" " , join (directives , " " ), "" )
644+ if desc := getDescription (node ); desc != "" {
645+ str = fmt .Sprintf ("\n %s\n %s" , desc , str )
646+ }
583647 return visitor .ActionUpdate , str
584648 }
585649 return visitor .ActionNoChange , nil
@@ -599,7 +663,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
599663 wrap ("= " , defaultValue , "" ),
600664 join (directives , " " ),
601665 }, " " )
602-
666+ if desc := getDescription (node ); desc != "" {
667+ str = fmt .Sprintf ("\n %s\n %s" , desc , str )
668+ }
603669 return visitor .ActionUpdate , str
604670 case map [string ]interface {}:
605671 name := getMapValueString (node , "Name" )
@@ -614,6 +680,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
614680 wrap ("= " , defaultValue , "" ),
615681 join (directives , " " ),
616682 }, " " )
683+ if desc := getDescription (node ); desc != "" {
684+ str = fmt .Sprintf ("\n %s\n %s" , desc , str )
685+ }
617686 return visitor .ActionUpdate , str
618687 }
619688 return visitor .ActionNoChange , nil
@@ -633,6 +702,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
633702 join (directives , " " ),
634703 block (fields ),
635704 }, " " )
705+ if desc := getDescription (node ); desc != "" {
706+ str = fmt .Sprintf ("%s\n %s" , desc , str )
707+ }
636708 return visitor .ActionUpdate , str
637709 case map [string ]interface {}:
638710 name := getMapValueString (node , "Name" )
@@ -647,6 +719,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
647719 join (directives , " " ),
648720 block (fields ),
649721 }, " " )
722+ if desc := getDescription (node ); desc != "" {
723+ str = fmt .Sprintf ("%s\n %s" , desc , str )
724+ }
650725 return visitor .ActionUpdate , str
651726 }
652727 return visitor .ActionNoChange , nil
@@ -666,6 +741,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
666741 join (directives , " " ),
667742 "= " + join (types , " | " ),
668743 }, " " )
744+ if desc := getDescription (node ); desc != "" {
745+ str = fmt .Sprintf ("%s\n %s" , desc , str )
746+ }
669747 return visitor .ActionUpdate , str
670748 case map [string ]interface {}:
671749 name := getMapValueString (node , "Name" )
@@ -680,6 +758,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
680758 join (directives , " " ),
681759 "= " + join (types , " | " ),
682760 }, " " )
761+ if desc := getDescription (node ); desc != "" {
762+ str = fmt .Sprintf ("%s\n %s" , desc , str )
763+ }
683764 return visitor .ActionUpdate , str
684765 }
685766 return visitor .ActionNoChange , nil
@@ -699,6 +780,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
699780 join (directives , " " ),
700781 block (values ),
701782 }, " " )
783+ if desc := getDescription (node ); desc != "" {
784+ str = fmt .Sprintf ("%s\n %s" , desc , str )
785+ }
702786 return visitor .ActionUpdate , str
703787 case map [string ]interface {}:
704788 name := getMapValueString (node , "Name" )
@@ -713,6 +797,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
713797 join (directives , " " ),
714798 block (values ),
715799 }, " " )
800+ if desc := getDescription (node ); desc != "" {
801+ str = fmt .Sprintf ("%s\n %s" , desc , str )
802+ }
716803 return visitor .ActionUpdate , str
717804 }
718805 return visitor .ActionNoChange , nil
@@ -729,6 +816,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
729816 name ,
730817 join (directives , " " ),
731818 }, " " )
819+ if desc := getDescription (node ); desc != "" {
820+ str = fmt .Sprintf ("\n %s\n %s" , desc , str )
821+ }
732822 return visitor .ActionUpdate , str
733823 case map [string ]interface {}:
734824 name := getMapValueString (node , "Name" )
@@ -740,6 +830,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
740830 name ,
741831 join (directives , " " ),
742832 }, " " )
833+ if desc := getDescription (node ); desc != "" {
834+ str = fmt .Sprintf ("\n %s\n %s" , desc , str )
835+ }
743836 return visitor .ActionUpdate , str
744837 }
745838 return visitor .ActionNoChange , nil
@@ -759,6 +852,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
759852 join (directives , " " ),
760853 block (fields ),
761854 }, " " )
855+ if desc := getDescription (node ); desc != "" {
856+ str = fmt .Sprintf ("%s\n %s" , desc , str )
857+ }
762858 return visitor .ActionUpdate , str
763859 case map [string ]interface {}:
764860 name := getMapValueString (node , "Name" )
@@ -773,6 +869,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
773869 join (directives , " " ),
774870 block (fields ),
775871 }, " " )
872+ if desc := getDescription (node ); desc != "" {
873+ str = fmt .Sprintf ("%s\n %s" , desc , str )
874+ }
776875 return visitor .ActionUpdate , str
777876 }
778877 return visitor .ActionNoChange , nil
@@ -793,15 +892,46 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
793892 "DirectiveDefinition" : func (p visitor.VisitFuncParams ) (string , interface {}) {
794893 switch node := p .Node .(type ) {
795894 case * ast.DirectiveDefinition :
796- args := wrap ("(" , join (toSliceString (node .Arguments ), ", " ), ")" )
797- str := fmt .Sprintf ("directive @%v%v on %v" , node .Name , args , join (toSliceString (node .Locations ), " | " ))
895+ args := toSliceString (node .Arguments )
896+ hasArgDesc := false
897+ for _ , arg := range node .Arguments {
898+ if arg .Description != nil && arg .Description .Value != "" {
899+ hasArgDesc = true
900+ break
901+ }
902+ }
903+ var argsStr string
904+ if hasArgDesc {
905+ argsStr = wrap ("(" , indent ("\n " + join (args , "\n " )), "\n )" )
906+ } else {
907+ argsStr = wrap ("(" , join (args , ", " ), ")" )
908+ }
909+ str := fmt .Sprintf ("directive @%v%v on %v" , node .Name , argsStr , join (toSliceString (node .Locations ), " | " ))
910+ if desc := getDescription (node ); desc != "" {
911+ str = fmt .Sprintf ("%s\n %s" , desc , str )
912+ }
798913 return visitor .ActionUpdate , str
799914 case map [string ]interface {}:
800915 name := getMapValueString (node , "Name" )
801916 locations := toSliceString (getMapValue (node , "Locations" ))
802917 args := toSliceString (getMapValue (node , "Arguments" ))
803- argsStr := wrap ("(" , join (args , ", " ), ")" )
918+ hasArgDesc := false
919+ for _ , arg := range args {
920+ if strings .HasPrefix (strings .TrimSpace (arg ), `"""` ) {
921+ hasArgDesc = true
922+ break
923+ }
924+ }
925+ var argsStr string
926+ if hasArgDesc {
927+ argsStr = wrap ("(" , indent ("\n " + join (args , "\n " )), "\n )" )
928+ } else {
929+ argsStr = wrap ("(" , join (args , ", " ), ")" )
930+ }
804931 str := fmt .Sprintf ("directive @%v%v on %v" , name , argsStr , join (locations , " | " ))
932+ if desc := getDescription (node ); desc != "" {
933+ str = fmt .Sprintf ("%s\n %s" , desc , str )
934+ }
805935 return visitor .ActionUpdate , str
806936 }
807937 return visitor .ActionNoChange , nil
0 commit comments