diff --git a/README.md b/README.md index 643a136..ea4bb94 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,6 @@ TODO(benkraft): Figure out how to get GitHub Actions to run the example -- it ne Query structures to support: - unions - fragments -- `__typename` Config options: - get schema via HTTP (perhaps even via GraphQL introspection) diff --git a/generate/testdata/TypeName.graphql b/generate/testdata/TypeName.graphql new file mode 100644 index 0000000..1cde75c --- /dev/null +++ b/generate/testdata/TypeName.graphql @@ -0,0 +1,6 @@ +{ + user { + __typename + id + } +} diff --git a/generate/testdata/TypeName.graphql.go b/generate/testdata/TypeName.graphql.go new file mode 100644 index 0000000..2ac33e8 --- /dev/null +++ b/generate/testdata/TypeName.graphql.go @@ -0,0 +1,6 @@ +type Response struct { + User *struct { + Typename *string `json:"__typename"` + Id string `json:"id"` + } `json:"user"` +} \ No newline at end of file diff --git a/generate/types_test.go b/generate/types_test.go index aa59f98..bcff5d9 100644 --- a/generate/types_test.go +++ b/generate/types_test.go @@ -1,9 +1,11 @@ package generate import ( + "errors" "fmt" "go/format" "io/ioutil" + "os" "path/filepath" "sort" "strings" @@ -15,9 +17,13 @@ import ( const dataDir = "testdata" -func readFile(t *testing.T, filename string) string { +func readFile(t *testing.T, filename string, allowNotExist bool) string { + t.Helper() data, err := ioutil.ReadFile(filepath.Join(dataDir, filename)) if err != nil { + if allowNotExist && errors.Is(err, os.ErrNotExist) { + return "" + } t.Fatal(err) } return string(data) @@ -49,7 +55,7 @@ func TestTypeForOperation(t *testing.T) { t.Fatal(err) } - schemaText := readFile(t, "schema.graphql") + schemaText := readFile(t, "schema.graphql", false) for _, file := range files { graphqlFilename := file.Name() @@ -59,7 +65,7 @@ func TestTypeForOperation(t *testing.T) { goFilename := graphqlFilename + ".go" t.Run(graphqlFilename, func(t *testing.T) { - expectedGoType, err := gofmt(readFile(t, goFilename)) + expectedGoType, err := gofmt(readFile(t, goFilename, update)) if err != nil { t.Fatal(err) } @@ -71,7 +77,7 @@ func TestTypeForOperation(t *testing.T) { } queryDoc, graphqlListError := gqlparser.LoadQuery( - schema, readFile(t, graphqlFilename)) + schema, readFile(t, graphqlFilename, false)) if graphqlListError != nil { t.Fatal(graphqlListError) } @@ -148,7 +154,7 @@ func TestTypeForInputType(t *testing.T) { }, }} - schemaText := readFile(t, "schema.graphql") + schemaText := readFile(t, "schema.graphql", false) for _, test := range tests { test := test diff --git a/generate/util.go b/generate/util.go index 7f0b8aa..6368b85 100644 --- a/generate/util.go +++ b/generate/util.go @@ -21,11 +21,11 @@ func changeFirst(s string, f func(rune) rune) string { } func lowerFirst(s string) string { - return changeFirst(s, unicode.ToLower) + return changeFirst(strings.TrimLeft(s, "_"), unicode.ToLower) } func upperFirst(s string) string { - return changeFirst(s, unicode.ToUpper) + return changeFirst(strings.TrimLeft(s, "_"), unicode.ToUpper) } func goConstName(s string) string {