@@ -70,6 +70,22 @@ 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+ desc = fmt .Sprintf (`"""%s"""` , desc )
86+ }
87+ return desc
88+ }
7389
7490func toSliceString (slice interface {}) []string {
7591 if slice == nil {
@@ -506,6 +522,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
506522 fmt .Sprintf ("%v" , node .Name ),
507523 join (directives , " " ),
508524 }, " " )
525+ if desc := getDescription (node ); desc != "" {
526+ str = join ([]string {desc , str }, "\n " )
527+ }
509528 return visitor .ActionUpdate , str
510529 case map [string ]interface {}:
511530 name := getMapValueString (node , "Name" )
@@ -518,6 +537,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
518537 name ,
519538 join (directives , " " ),
520539 }, " " )
540+ if desc := getDescription (node ); desc != "" {
541+ str = join ([]string {desc , str }, "\n " )
542+ }
521543 return visitor .ActionUpdate , str
522544 }
523545 return visitor .ActionNoChange , nil
@@ -539,6 +561,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
539561 join (directives , " " ),
540562 block (fields ),
541563 }, " " )
564+ if desc := getDescription (node ); desc != "" {
565+ str = join ([]string {desc , str }, "\n " )
566+ }
542567 return visitor .ActionUpdate , str
543568 case map [string ]interface {}:
544569 name := getMapValueString (node , "Name" )
@@ -555,6 +580,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
555580 join (directives , " " ),
556581 block (fields ),
557582 }, " " )
583+ if desc := getDescription (node ); desc != "" {
584+ str = join ([]string {desc , str }, "\n " )
585+ }
558586 return visitor .ActionUpdate , str
559587 }
560588 return visitor .ActionNoChange , nil
@@ -570,6 +598,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
570598 directives = append (directives , fmt .Sprintf ("%v" , directive .Name ))
571599 }
572600 str := name + wrap ("(" , join (args , ", " ), ")" ) + ": " + ttype + wrap (" " , join (directives , " " ), "" )
601+ if desc := getDescription (node ); desc != "" {
602+ str = join ([]string {desc , str }, "\n " )
603+ }
573604 return visitor .ActionUpdate , str
574605 case map [string ]interface {}:
575606 name := getMapValueString (node , "Name" )
@@ -580,6 +611,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
580611 directives = append (directives , fmt .Sprintf ("%v" , directive ))
581612 }
582613 str := name + wrap ("(" , join (args , ", " ), ")" ) + ": " + ttype + wrap (" " , join (directives , " " ), "" )
614+ if desc := getDescription (node ); desc != "" {
615+ str = join ([]string {desc , str }, "\n " )
616+ }
583617 return visitor .ActionUpdate , str
584618 }
585619 return visitor .ActionNoChange , nil
@@ -599,7 +633,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
599633 wrap ("= " , defaultValue , "" ),
600634 join (directives , " " ),
601635 }, " " )
602-
636+ if desc := getDescription (node ); desc != "" {
637+ str = join ([]string {desc , str }, " " )
638+ }
603639 return visitor .ActionUpdate , str
604640 case map [string ]interface {}:
605641 name := getMapValueString (node , "Name" )
@@ -614,6 +650,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
614650 wrap ("= " , defaultValue , "" ),
615651 join (directives , " " ),
616652 }, " " )
653+ if desc := getDescription (node ); desc != "" {
654+ str = join ([]string {desc , str }, " " )
655+ }
617656 return visitor .ActionUpdate , str
618657 }
619658 return visitor .ActionNoChange , nil
@@ -633,6 +672,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
633672 join (directives , " " ),
634673 block (fields ),
635674 }, " " )
675+ if desc := getDescription (node ); desc != "" {
676+ str = join ([]string {desc , str }, "\n " )
677+ }
636678 return visitor .ActionUpdate , str
637679 case map [string ]interface {}:
638680 name := getMapValueString (node , "Name" )
@@ -647,6 +689,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
647689 join (directives , " " ),
648690 block (fields ),
649691 }, " " )
692+ if desc := getDescription (node ); desc != "" {
693+ str = join ([]string {desc , str }, "\n " )
694+ }
650695 return visitor .ActionUpdate , str
651696 }
652697 return visitor .ActionNoChange , nil
@@ -666,6 +711,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
666711 join (directives , " " ),
667712 "= " + join (types , " | " ),
668713 }, " " )
714+ if desc := getDescription (node ); desc != "" {
715+ str = join ([]string {desc , str }, "\n " )
716+ }
669717 return visitor .ActionUpdate , str
670718 case map [string ]interface {}:
671719 name := getMapValueString (node , "Name" )
@@ -680,6 +728,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
680728 join (directives , " " ),
681729 "= " + join (types , " | " ),
682730 }, " " )
731+ if desc := getDescription (node ); desc != "" {
732+ str = join ([]string {desc , str }, "\n " )
733+ }
683734 return visitor .ActionUpdate , str
684735 }
685736 return visitor .ActionNoChange , nil
@@ -699,6 +750,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
699750 join (directives , " " ),
700751 block (values ),
701752 }, " " )
753+ if desc := getDescription (node ); desc != "" {
754+ str = join ([]string {desc , str }, "\n " )
755+ }
702756 return visitor .ActionUpdate , str
703757 case map [string ]interface {}:
704758 name := getMapValueString (node , "Name" )
@@ -713,6 +767,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
713767 join (directives , " " ),
714768 block (values ),
715769 }, " " )
770+ if desc := getDescription (node ); desc != "" {
771+ str = join ([]string {desc , str }, "\n " )
772+ }
716773 return visitor .ActionUpdate , str
717774 }
718775 return visitor .ActionNoChange , nil
@@ -729,6 +786,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
729786 name ,
730787 join (directives , " " ),
731788 }, " " )
789+ if desc := getDescription (node ); desc != "" {
790+ str = join ([]string {desc , str }, "\n " )
791+ }
732792 return visitor .ActionUpdate , str
733793 case map [string ]interface {}:
734794 name := getMapValueString (node , "Name" )
@@ -740,6 +800,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
740800 name ,
741801 join (directives , " " ),
742802 }, " " )
803+ if desc := getDescription (node ); desc != "" {
804+ str = join ([]string {desc , str }, "\n " )
805+ }
743806 return visitor .ActionUpdate , str
744807 }
745808 return visitor .ActionNoChange , nil
@@ -759,6 +822,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
759822 join (directives , " " ),
760823 block (fields ),
761824 }, " " )
825+ if desc := getDescription (node ); desc != "" {
826+ str = join ([]string {desc , str }, "\n " )
827+ }
762828 return visitor .ActionUpdate , str
763829 case map [string ]interface {}:
764830 name := getMapValueString (node , "Name" )
@@ -773,6 +839,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
773839 join (directives , " " ),
774840 block (fields ),
775841 }, " " )
842+ if desc := getDescription (node ); desc != "" {
843+ str = join ([]string {desc , str }, "\n " )
844+ }
776845 return visitor .ActionUpdate , str
777846 }
778847 return visitor .ActionNoChange , nil
@@ -782,10 +851,16 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
782851 case * ast.TypeExtensionDefinition :
783852 definition := fmt .Sprintf ("%v" , node .Definition )
784853 str := "extend " + definition
854+ if desc := getDescription (node ); desc != "" {
855+ str = join ([]string {desc , str }, "\n " )
856+ }
785857 return visitor .ActionUpdate , str
786858 case map [string ]interface {}:
787859 definition := getMapValueString (node , "Definition" )
788860 str := "extend " + definition
861+ if desc := getDescription (node ); desc != "" {
862+ str = join ([]string {desc , str }, "\n " )
863+ }
789864 return visitor .ActionUpdate , str
790865 }
791866 return visitor .ActionNoChange , nil
@@ -795,13 +870,19 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
795870 case * ast.DirectiveDefinition :
796871 args := wrap ("(" , join (toSliceString (node .Arguments ), ", " ), ")" )
797872 str := fmt .Sprintf ("directive @%v%v on %v" , node .Name , args , join (toSliceString (node .Locations ), " | " ))
873+ if desc := getDescription (node ); desc != "" {
874+ str = join ([]string {desc , str }, "\n " )
875+ }
798876 return visitor .ActionUpdate , str
799877 case map [string ]interface {}:
800878 name := getMapValueString (node , "Name" )
801879 locations := toSliceString (getMapValue (node , "Locations" ))
802880 args := toSliceString (getMapValue (node , "Arguments" ))
803881 argsStr := wrap ("(" , join (args , ", " ), ")" )
804882 str := fmt .Sprintf ("directive @%v%v on %v" , name , argsStr , join (locations , " | " ))
883+ if desc := getDescription (node ); desc != "" {
884+ str = join ([]string {desc , str }, "\n " )
885+ }
805886 return visitor .ActionUpdate , str
806887 }
807888 return visitor .ActionNoChange , nil
0 commit comments