|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 | 8 | "strings"
|
| 9 | + "text/template" |
9 | 10 | "unsafe"
|
10 | 11 |
|
11 | 12 | "github.com/Code-Hex/gqldoc/internal/introspection"
|
@@ -50,40 +51,120 @@ func (m *Config) MakeLinkFromType(typ *introspection.Type) (string, error) {
|
50 | 51 | }
|
51 | 52 |
|
52 | 53 | func (m *Config) Render(s *introspection.Schema) error {
|
| 54 | + readmeContent := &README{} |
| 55 | + |
53 | 56 | if s.QueryType != nil {
|
54 | 57 | if err := m.renderQuery(s); err != nil {
|
55 | 58 | return errors.WithStack(err)
|
56 | 59 | }
|
| 60 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 61 | + Type: "Query", |
| 62 | + Link: "queries.md", |
| 63 | + }) |
57 | 64 | }
|
58 | 65 | if s.MutationType != nil {
|
59 | 66 | if err := m.renderMutation(s); err != nil {
|
60 | 67 | return errors.WithStack(err)
|
61 | 68 | }
|
| 69 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 70 | + Type: "Mutations", |
| 71 | + Link: "mutations.md", |
| 72 | + }) |
62 | 73 | }
|
63 | 74 | if len(s.Types) > 0 {
|
64 | 75 | types := make(map[introspection.TypeKind][]*introspection.Types, len(s.Types))
|
65 | 76 | for _, typ := range s.Types {
|
66 | 77 | types[typ.Kind] = append(types[typ.Kind], typ)
|
67 | 78 | }
|
68 |
| - if err := m.renderObjects(types[introspection.OBJECT]); err != nil { |
69 |
| - return errors.WithStack(err) |
| 79 | + if typs := types[introspection.OBJECT]; len(typs) > 0 { |
| 80 | + if err := m.renderObjects(typs); err != nil { |
| 81 | + return errors.WithStack(err) |
| 82 | + } |
| 83 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 84 | + Type: "Objects", |
| 85 | + Link: "objects.md", |
| 86 | + }) |
70 | 87 | }
|
71 |
| - if err := m.renderInterfaces(types[introspection.INTERFACE]); err != nil { |
72 |
| - return errors.WithStack(err) |
| 88 | + if typs := types[introspection.INTERFACE]; len(typs) > 0 { |
| 89 | + if err := m.renderInterfaces(typs); err != nil { |
| 90 | + return errors.WithStack(err) |
| 91 | + } |
| 92 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 93 | + Type: "Interfaces", |
| 94 | + Link: "interfaces.md", |
| 95 | + }) |
73 | 96 | }
|
74 |
| - if err := m.renderEnums(types[introspection.ENUM]); err != nil { |
75 |
| - return errors.WithStack(err) |
| 97 | + if typs := types[introspection.ENUM]; len(typs) > 0 { |
| 98 | + if err := m.renderEnums(typs); err != nil { |
| 99 | + return errors.WithStack(err) |
| 100 | + } |
| 101 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 102 | + Type: "Enums", |
| 103 | + Link: "enums.md", |
| 104 | + }) |
76 | 105 | }
|
77 |
| - if err := m.renderUnions(types[introspection.UNION]); err != nil { |
78 |
| - return errors.WithStack(err) |
| 106 | + if typs := types[introspection.UNION]; len(typs) > 0 { |
| 107 | + if err := m.renderUnions(typs); err != nil { |
| 108 | + return errors.WithStack(err) |
| 109 | + } |
| 110 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 111 | + Type: "Unions", |
| 112 | + Link: "unions.md", |
| 113 | + }) |
79 | 114 | }
|
80 |
| - if err := m.renderInputObjects(types[introspection.INPUT_OBJECT]); err != nil { |
81 |
| - return errors.WithStack(err) |
| 115 | + if typs := types[introspection.INPUT_OBJECT]; len(typs) > 0 { |
| 116 | + if err := m.renderInputObjects(typs); err != nil { |
| 117 | + return errors.WithStack(err) |
| 118 | + } |
| 119 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 120 | + Type: "Input objects", |
| 121 | + Link: "input_objects.md", |
| 122 | + }) |
82 | 123 | }
|
83 |
| - if err := m.renderScalars(types[introspection.SCALAR]); err != nil { |
84 |
| - return errors.WithStack(err) |
| 124 | + if typs := types[introspection.SCALAR]; len(typs) > 0 { |
| 125 | + if err := m.renderScalars(typs); err != nil { |
| 126 | + return errors.WithStack(err) |
| 127 | + } |
| 128 | + readmeContent.Items = append(readmeContent.Items, &READMEItem{ |
| 129 | + Type: "Scalars", |
| 130 | + Link: "scalars.md", |
| 131 | + }) |
85 | 132 | }
|
86 | 133 | }
|
87 | 134 |
|
| 135 | + if err := m.renderREADME(readmeContent); err != nil { |
| 136 | + return errors.WithStack(err) |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +//go:embed README.md |
| 143 | +var readme string |
| 144 | + |
| 145 | +type README struct { |
| 146 | + Items []*READMEItem |
| 147 | +} |
| 148 | + |
| 149 | +type READMEItem struct { |
| 150 | + Type string |
| 151 | + Link string |
| 152 | +} |
| 153 | + |
| 154 | +func (m *Config) renderREADME(r *README) error { |
| 155 | + f, err := m.Create("README.md") |
| 156 | + if err != nil { |
| 157 | + return errors.WithStack(err) |
| 158 | + } |
| 159 | + defer f.Close() |
| 160 | + |
| 161 | + t, err := template.New("README").Parse(readme) |
| 162 | + if err != nil { |
| 163 | + return errors.WithStack(err) |
| 164 | + } |
| 165 | + |
| 166 | + if err := t.Execute(f, r); err != nil { |
| 167 | + return errors.WithStack(err) |
| 168 | + } |
88 | 169 | return nil
|
89 | 170 | }
|
0 commit comments