test, and clean up, support for __typename

This commit is contained in:
Ben Kraft
2020-07-15 12:29:29 -07:00
parent cf6f57f5f1
commit d089ab1afb
5 changed files with 25 additions and 8 deletions
-1
View File
@@ -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)
+6
View File
@@ -0,0 +1,6 @@
{
user {
__typename
id
}
}
+6
View File
@@ -0,0 +1,6 @@
type Response struct {
User *struct {
Typename *string `json:"__typename"`
Id string `json:"id"`
} `json:"user"`
}
+11 -5
View File
@@ -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
+2 -2
View File
@@ -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 {