total rewrite to interface handling; not complete but it compiles

This commit is contained in:
Ben Kraft
2020-07-16 13:28:43 -07:00
parent af4a765a32
commit cf7136ca65
33 changed files with 712 additions and 173 deletions
+16 -38
View File
@@ -55,8 +55,6 @@ func TestTypeForOperation(t *testing.T) {
t.Fatal(err)
}
schemaText := readFile(t, "schema.graphql", false)
for _, file := range files {
graphqlFilename := file.Name()
if graphqlFilename == "schema.graphql" || !strings.HasSuffix(graphqlFilename, ".graphql") {
@@ -65,45 +63,25 @@ func TestTypeForOperation(t *testing.T) {
goFilename := graphqlFilename + ".go"
t.Run(graphqlFilename, func(t *testing.T) {
expectedGoType, err := gofmt(readFile(t, goFilename, update))
expectedGoCode, err := gofmt(readFile(t, goFilename, update))
if err != nil {
t.Fatal(err)
}
schema, graphqlError := gqlparser.LoadSchema(
&ast.Source{Name: "test schema", Input: schemaText})
if graphqlError != nil {
t.Fatal(graphqlError)
}
queryDoc, graphqlListError := gqlparser.LoadQuery(
schema, readFile(t, graphqlFilename, false))
if graphqlListError != nil {
t.Fatal(graphqlListError)
}
if len(queryDoc.Operations) != 1 {
t.Fatalf("got %v operations, want 1", len(queryDoc.Operations))
}
g := newGenerator(&Config{Package: "test_package"}, schema)
err = g.addOperation(queryDoc.Operations[0])
goCode, err := Generate(&Config{
Schema: filepath.Join("testdata", "schema.graphql"),
Queries: filepath.Join("testdata", graphqlFilename),
Package: "test",
})
if err != nil {
t.Error(err)
t.Fatal(err)
}
// gofmt before comparing.
goType, err := gofmt(g.Types())
if err != nil {
t.Error(err)
}
if goType != expectedGoType {
t.Errorf("got:\n%v\nwant:\n%v\n", goType, expectedGoType)
if string(goCode) != expectedGoCode {
t.Errorf("got:\n%v\nwant:\n%v\n", string(goCode), expectedGoCode)
if update {
t.Log("Updating testdata dir to match")
err = ioutil.WriteFile(
filepath.Join(dataDir, goFilename), []byte(goType), 0644)
err = ioutil.WriteFile(filepath.Join(dataDir, goFilename), goCode, 0644)
if err != nil {
t.Errorf("Unable to update testdata dir: %v", err)
}
@@ -138,18 +116,18 @@ func TestTypeForInputType(t *testing.T) {
}, {
`DefinedType`,
`UserQueryInput`,
`*userQueryInput`,
`*UserQueryInput`,
[]string{
`type role string
`type Role string
const (
studentRole role = "STUDENT"
teacherRole role = "TEACHER"
StudentRole Role = "STUDENT"
TeacherRole Role = "TEACHER"
)`,
`type userQueryInput struct {
`type UserQueryInput struct {
Email *string ` + "`json:\"email\"`" + `
Name *string ` + "`json:\"name\"`" + `
Id *string ` + "`json:\"id\"`" + `
Role *role ` + "`json:\"role\"`" + `
Role *Role ` + "`json:\"role\"`" + `
}`,
},
}}