Skip to content

Commit

Permalink
Merge pull request 99designs#780 from zdebra/master
Browse files Browse the repository at this point in the history
fixed generating a description to golang comments for enum type
  • Loading branch information
vektah authored Jul 8, 2019
2 parents 1c5824d + 3d7a113 commit 9019ee3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugin/modelgen/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{{- end}}

{{ range $enum := .Enums }}
{{ with .Description|go }} {{.|prefixLines "// "}} {{end}}
{{ with .Description }} {{.|prefixLines "// "}} {{end}}
type {{.Name|go }} string
const (
{{- range $value := .Values}}
Expand Down
17 changes: 17 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package modelgen

import (
"go/parser"
"go/token"
"io/ioutil"
"strings"
"testing"

"github.com/99designs/gqlgen/codegen/config"
Expand All @@ -19,10 +22,24 @@ func TestModelGeneration(t *testing.T) {
require.True(t, cfg.Models.UserDefined("MissingEnum"))
require.True(t, cfg.Models.UserDefined("MissingUnion"))
require.True(t, cfg.Models.UserDefined("MissingInterface"))
require.True(t, cfg.Models.UserDefined("TypeWithDescription"))
require.True(t, cfg.Models.UserDefined("EnumWithDescription"))
require.True(t, cfg.Models.UserDefined("InterfaceWithDescription"))
require.True(t, cfg.Models.UserDefined("UnionWithDescription"))

t.Run("no pointer pointers", func(t *testing.T) {
generated, err := ioutil.ReadFile("./out/generated.go")
require.NoError(t, err)
require.NotContains(t, string(generated), "**")
})

t.Run("description is generated", func(t *testing.T) {
node, err := parser.ParseFile(token.NewFileSet(), "./out/generated.go", nil, parser.ParseComments)
require.NoError(t, err)
for _, commentGroup := range node.Comments {
text := commentGroup.Text()
words := strings.Split(text, " ")
require.True(t, len(words) > 1, "expected description %q to have more than one word", text)
}
})
}
59 changes: 59 additions & 0 deletions plugin/modelgen/out/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ interface ExistingInterface {

union ExistingUnion = MissingTypeNotNull | MissingTypeNullable | ExistingType

"TypeWithDescription is a type with a description"
type TypeWithDescription {
name: String
}

"EnumWithDescription is an enum with a description"
enum EnumWithDescription {
CAT
DOG
}

"InterfaceWithDescription is an interface with a description"
interface InterfaceWithDescription {
name: String
}

"UnionWithDescription is an union with a description"
union UnionWithDescription = TypeWithDescription | ExistingType

0 comments on commit 9019ee3

Please sign in to comment.