push type-name into types.go

This commit is contained in:
Ben Kraft
2020-04-10 14:30:56 -07:00
parent 4f02619b0b
commit dfea9bf128
5 changed files with 16 additions and 14 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import (
"github.com/Khan/genql/graphql" "github.com/Khan/genql/graphql"
) )
type getViewerResponse = struct { type getViewerResponse struct {
Viewer struct { Viewer struct {
MyName *string MyName *string
} `json:"viewer"` } `json:"viewer"`
@@ -26,7 +26,7 @@ query getViewer {
return &retval, err return &retval, err
} }
type getUserResponse = struct { type getUserResponse struct {
User *struct { User *struct {
TheirName *string `json:"theirName"` TheirName *string `json:"theirName"`
} `json:"user"` } `json:"user"`
+4 -3
View File
@@ -111,7 +111,9 @@ func fromASTOperation(op *ast.OperationDefinition, schema *ast.Schema) (operatio
} }
} }
typ, err := typeForOperation(op, schema) // TODO: configure ResponseName format
responseName := op.Name + "Response"
typ, err := typeForOperation(responseName, op, schema)
if err != nil { if err != nil {
return operation{}, fmt.Errorf("could not compute return-type for query: %v", err) return operation{}, fmt.Errorf("could not compute return-type for query: %v", err)
} }
@@ -124,8 +126,7 @@ func fromASTOperation(op *ast.OperationDefinition, schema *ast.Schema) (operatio
Body: "\n" + builder.String(), Body: "\n" + builder.String(),
Args: args, Args: args,
// TODO: configure ResponseName format ResponseName: responseName,
ResponseName: op.Name + "Response",
ResponseType: typ, ResponseType: typ,
}, nil }, nil
} }
+1 -1
View File
@@ -9,7 +9,7 @@ import (
) )
{{range .Operations}} {{range .Operations}}
type {{.ResponseName}} = {{.ResponseType}} {{.ResponseType}}
{{.Doc}} {{.Doc}}
func {{.Name}}(ctx context.Context, client *graphql.Client{{range .Args}}, {{.GoName}} {{.GoType}}{{end}}) (*{{.ResponseName}}, error) { func {{.Name}}(ctx context.Context, client *graphql.Client{{range .Args}}, {{.GoName}} {{.GoType}}{{end}}) (*{{.ResponseName}}, error) {
+2 -1
View File
@@ -25,8 +25,9 @@ func (builder *typeBuilder) baseTypeForOperation(operation ast.Operation) *ast.D
} }
} }
func typeForOperation(operation *ast.OperationDefinition, schema *ast.Schema) (string, error) { func typeForOperation(name string, operation *ast.OperationDefinition, schema *ast.Schema) (string, error) {
builder := &typeBuilder{schema: schema} builder := &typeBuilder{schema: schema}
fmt.Fprintf(builder, "type %s ", name)
err := builder.writeTypedef( err := builder.writeTypedef(
builder.baseTypeForOperation(operation.Operation), operation.SelectionSet) builder.baseTypeForOperation(operation.Operation), operation.SelectionSet)
return builder.String(), err return builder.String(), err
+7 -7
View File
@@ -48,7 +48,7 @@ func TestTypeForOperation(t *testing.T) {
}{{ }{{
"SimpleQuery", "SimpleQuery",
`{ user { id } }`, `{ user { id } }`,
`struct{ `type Response struct{
User *struct { User *struct {
Id string ` + "`json:\"id\"`" + ` Id string ` + "`json:\"id\"`" + `
} ` + "`json:\"user\"`" + ` } ` + "`json:\"user\"`" + `
@@ -56,7 +56,7 @@ func TestTypeForOperation(t *testing.T) {
}, { }, {
"QueryWithAlias", "QueryWithAlias",
`{ User: user { ID: id } }`, `{ User: user { ID: id } }`,
`struct{ `type Response struct{
User *struct { User *struct {
ID string ID string
} }
@@ -73,7 +73,7 @@ func TestTypeForOperation(t *testing.T) {
EmailsWithNullsOrNull: emailsWithNullsOrNull EmailsWithNullsOrNull: emailsWithNullsOrNull
} }
}`, }`,
`struct{ `type Response struct{
User *struct { User *struct {
Emails []string Emails []string
EmailsOrNull []string EmailsOrNull []string
@@ -91,7 +91,7 @@ func TestTypeForOperation(t *testing.T) {
} }
} }
}`, }`,
`struct{ `type Response struct{
User *struct { User *struct {
AuthMethods []struct { AuthMethods []struct {
Provider *string Provider *string
@@ -104,7 +104,7 @@ func TestTypeForOperation(t *testing.T) {
for _, test := range tests { for _, test := range tests {
test := test test := test
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
expectedGoType, err := gofmt("type Response " + test.expectedGoType) expectedGoType, err := gofmt(test.expectedGoType)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -118,13 +118,13 @@ func TestTypeForOperation(t *testing.T) {
t.Fatalf("got %v operations, want 1", len(queryDoc.Operations)) t.Fatalf("got %v operations, want 1", len(queryDoc.Operations))
} }
goType, err := typeForOperation(queryDoc.Operations[0], schema) goType, err := typeForOperation("Response", queryDoc.Operations[0], schema)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
// gofmt before comparing. // gofmt before comparing.
goType, err = gofmt("type Response " + goType) goType, err = gofmt(goType)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }