test, and clean up, support for __typename

This commit is contained in:
Ben Kraft
2020-07-15 17:35:47 -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: Query structures to support:
- unions - unions
- fragments - fragments
- `__typename`
Config options: Config options:
- get schema via HTTP (perhaps even via GraphQL introspection) - 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 package generate
import ( import (
"errors"
"fmt" "fmt"
"go/format" "go/format"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
@@ -15,9 +17,13 @@ import (
const dataDir = "testdata" 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)) data, err := ioutil.ReadFile(filepath.Join(dataDir, filename))
if err != nil { if err != nil {
if allowNotExist && errors.Is(err, os.ErrNotExist) {
return ""
}
t.Fatal(err) t.Fatal(err)
} }
return string(data) return string(data)
@@ -49,7 +55,7 @@ func TestTypeForOperation(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
schemaText := readFile(t, "schema.graphql") schemaText := readFile(t, "schema.graphql", false)
for _, file := range files { for _, file := range files {
graphqlFilename := file.Name() graphqlFilename := file.Name()
@@ -59,7 +65,7 @@ func TestTypeForOperation(t *testing.T) {
goFilename := graphqlFilename + ".go" goFilename := graphqlFilename + ".go"
t.Run(graphqlFilename, func(t *testing.T) { t.Run(graphqlFilename, func(t *testing.T) {
expectedGoType, err := gofmt(readFile(t, goFilename)) expectedGoType, err := gofmt(readFile(t, goFilename, update))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -71,7 +77,7 @@ func TestTypeForOperation(t *testing.T) {
} }
queryDoc, graphqlListError := gqlparser.LoadQuery( queryDoc, graphqlListError := gqlparser.LoadQuery(
schema, readFile(t, graphqlFilename)) schema, readFile(t, graphqlFilename, false))
if graphqlListError != nil { if graphqlListError != nil {
t.Fatal(graphqlListError) 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 { for _, test := range tests {
test := test test := test
+2 -2
View File
@@ -21,11 +21,11 @@ func changeFirst(s string, f func(rune) rune) string {
} }
func lowerFirst(s string) string { func lowerFirst(s string) string {
return changeFirst(s, unicode.ToLower) return changeFirst(strings.TrimLeft(s, "_"), unicode.ToLower)
} }
func upperFirst(s string) string { func upperFirst(s string) string {
return changeFirst(s, unicode.ToUpper) return changeFirst(strings.TrimLeft(s, "_"), unicode.ToUpper)
} }
func goConstName(s string) string { func goConstName(s string) string {