Skip to content

Commit de170ae

Browse files
committed
preliminary description support... er, well, awareness
1 parent 199d20b commit de170ae

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

language/printer/printer.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

7490
func 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

language/printer/schema_printer_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ func TestSchemaPrinter_PrintsKitchenSink(t *testing.T) {
5858
mutation: MutationType
5959
}
6060
61+
"""object description"""
6162
type Foo implements Bar & Baz {
63+
"""one field description"""
6264
one: Type
6365
two(argument: InputType!): Type
64-
three(argument: InputType, other: String): Int
66+
three(argument: InputType, """arg description""" other: String): Int
67+
"""four field description"""
6568
four(argument: String = "string"): String
6669
five(argument: [String] = ["string", "string"]): String
6770
six(argument: InputType = {key: "value"}): Type
@@ -71,6 +74,9 @@ type AnnotatedObject @onObject(arg: "value") {
7174
annotatedField(arg: Type = "default" @onArg): Type @onField
7275
}
7376
77+
"""interface description
78+
79+
multiple lines"""
7480
interface Bar {
7581
one: Type
7682
four(argument: String = "string"): String
@@ -82,14 +88,18 @@ interface AnnotatedInterface @onInterface {
8288
8389
union Feed = Story | Article | Advert
8490
91+
"""union description"""
8592
union AnnotatedUnion @onUnion = A | B
8693
94+
"""scalar description"""
8795
scalar CustomScalar
8896
8997
scalar AnnotatedScalar @onScalar
9098
99+
"""enum description"""
91100
enum Site {
92101
DESKTOP
102+
"""enum member description"""
93103
MOBILE
94104
}
95105
@@ -98,8 +108,10 @@ enum AnnotatedEnum @onEnum {
98108
OTHER_VALUE
99109
}
100110
111+
"""input description"""
101112
input InputType {
102113
key: String!
114+
"""input value description"""
103115
answer: Int = 42
104116
}
105117
@@ -115,6 +127,7 @@ extend type Foo @onType {}
115127
116128
type NoFields {}
117129
130+
"""directive description"""
118131
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
119132
120133
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

schema-kitchen-sink.graphql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ schema {
55
mutation: MutationType
66
}
77

8+
"""
9+
object description
10+
"""
811
type Foo implements Bar & Baz {
12+
"""one field description"""
913
one: Type
1014
two(argument: InputType!): Type
11-
three(argument: InputType, other: String): Int
15+
three(argument: InputType, """arg description""" other: String): Int
16+
"""four field description"""
1217
four(argument: String = "string"): String
1318
five(argument: [String] = ["string", "string"]): String
1419
six(argument: InputType = {key: "value"}): Type
@@ -18,6 +23,9 @@ type AnnotatedObject @onObject(arg: "value") {
1823
annotatedField(arg: Type = "default" @onArg): Type @onField
1924
}
2025

26+
"""interface description
27+
28+
multiple lines"""
2129
interface Bar {
2230
one: Type
2331
four(argument: String = "string"): String
@@ -29,14 +37,18 @@ interface AnnotatedInterface @onInterface {
2937

3038
union Feed = Story | Article | Advert
3139

40+
"""union description"""
3241
union AnnotatedUnion @onUnion = A | B
3342

43+
"""scalar description"""
3444
scalar CustomScalar
3545

3646
scalar AnnotatedScalar @onScalar
3747

48+
"""enum description"""
3849
enum Site {
3950
DESKTOP
51+
"""enum member description"""
4052
MOBILE
4153
}
4254

@@ -45,8 +57,10 @@ enum AnnotatedEnum @onEnum {
4557
OTHER_VALUE
4658
}
4759

60+
"""input description"""
4861
input InputType {
4962
key: String!
63+
"""input value description"""
5064
answer: Int = 42
5165
}
5266

@@ -62,6 +76,7 @@ extend type Foo @onType {}
6276

6377
type NoFields {}
6478

79+
"""directive description"""
6580
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
6681

6782
directive @include(if: Boolean!)

0 commit comments

Comments
 (0)