Skip to content

Commit

Permalink
push type-name into types.go
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminjkraft committed Apr 10, 2020
1 parent 4f02619 commit dfea9bf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions example/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion generate/operation.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions generate/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func TestTypeForOperation(t *testing.T) {
}{{
"SimpleQuery",
`{ user { id } }`,
`struct{
`type Response struct{
User *struct {
Id string ` + "`json:\"id\"`" + `
} ` + "`json:\"user\"`" + `
}`,
}, {
"QueryWithAlias",
`{ User: user { ID: id } }`,
`struct{
`type Response struct{
User *struct {
ID string
}
Expand All @@ -73,7 +73,7 @@ func TestTypeForOperation(t *testing.T) {
EmailsWithNullsOrNull: emailsWithNullsOrNull
}
}`,
`struct{
`type Response struct{
User *struct {
Emails []string
EmailsOrNull []string
Expand All @@ -91,7 +91,7 @@ func TestTypeForOperation(t *testing.T) {
}
}
}`,
`struct{
`type Response struct{
User *struct {
AuthMethods []struct {
Provider *string
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit dfea9bf

Please sign in to comment.