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"
)
type getViewerResponse = struct {
type getViewerResponse struct {
Viewer struct {
MyName *string
} `json:"viewer"`
@@ -26,7 +26,7 @@ query getViewer {
return &retval, err
}
type getUserResponse = struct {
type getUserResponse struct {
User *struct {
TheirName *string `json:"theirName"`
} `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 {
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(),
Args: args,
// TODO: configure ResponseName format
ResponseName: op.Name + "Response",
ResponseName: responseName,
ResponseType: typ,
}, nil
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
)
{{range .Operations}}
type {{.ResponseName}} = {{.ResponseType}}
{{.ResponseType}}
{{.Doc}}
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}
fmt.Fprintf(builder, "type %s ", name)
err := builder.writeTypedef(
builder.baseTypeForOperation(operation.Operation), operation.SelectionSet)
return builder.String(), err
+7 -7
View File
@@ -48,7 +48,7 @@ func TestTypeForOperation(t *testing.T) {
}{{
"SimpleQuery",
`{ user { id } }`,
`struct{
`type Response struct{
User *struct {
Id string ` + "`json:\"id\"`" + `
} ` + "`json:\"user\"`" + `
@@ -56,7 +56,7 @@ func TestTypeForOperation(t *testing.T) {
}, {
"QueryWithAlias",
`{ User: user { ID: id } }`,
`struct{
`type Response struct{
User *struct {
ID string
}
@@ -73,7 +73,7 @@ func TestTypeForOperation(t *testing.T) {
EmailsWithNullsOrNull: emailsWithNullsOrNull
}
}`,
`struct{
`type Response struct{
User *struct {
Emails []string
EmailsOrNull []string
@@ -91,7 +91,7 @@ func TestTypeForOperation(t *testing.T) {
}
}
}`,
`struct{
`type Response struct{
User *struct {
AuthMethods []struct {
Provider *string
@@ -104,7 +104,7 @@ func TestTypeForOperation(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
expectedGoType, err := gofmt("type Response " + test.expectedGoType)
expectedGoType, err := gofmt(test.expectedGoType)
if err != nil {
t.Fatal(err)
}
@@ -118,13 +118,13 @@ func TestTypeForOperation(t *testing.T) {
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 {
t.Error(err)
}
// gofmt before comparing.
goType, err = gofmt("type Response " + goType)
goType, err = gofmt(goType)
if err != nil {
t.Error(err)
}