add basic query struct, support 1.12

This commit is contained in:
Ben Kraft
2019-12-23 19:35:38 -05:00
parent a23b0e3756
commit efa9ccd122
5 changed files with 65 additions and 10 deletions
+38 -1
View File
@@ -1,5 +1,42 @@
package generate
import "github.com/vektah/gqlparser"
import (
"fmt"
"os"
"text/template"
"github.com/vektah/gqlparser"
)
var _ = gqlparser.LoadSchema
// TODO: package template into the binary using one of those asset thingies
const tmplFilename = "generate/operation.go.tmpl"
type TemplateParams struct {
// The name of the package into which to generate the operation-helpers.
PackageName string
// The type-name for the operation's response type.
ResponseName string
// The body of the operation's response type (e.g. struct { ... }).
ResponseType string
// The documentation for the operation, from GraphQL.
OperationDoc string
// The name of the operation, from GraphQL.
OperationName string
// The endpoint to which to send queries.
Endpoint string
// The body of the operation to send.
Operation string
}
var tmpl = template.Must(template.ParseFiles(tmplFilename))
func Generate() error {
var data TemplateParams
err := tmpl.Execute(os.Stdout, data)
if err != nil {
return fmt.Errorf("template did not render: %v", err)
}
return nil
}
@@ -10,16 +10,17 @@ import (
type {{.ResponseName}} = {{.ResponseType}}
// {{.QueryDoc}}
func {{.QueryName}}(ctx context.Context) (*{{.ResponseName}}, error) {
req, err := http.NewRequestWithContext(
ctx, http.MethodPost,
// {{.OperationDoc}}
func {{.OperationName}}(ctx context.Context) (*{{.ResponseName}}, error) {
req, err := http.NewRequest(
http.MethodPost,
`{{.Endpoint}}`,
strings.NewReader(`{{.Query}}`))
strings.NewReader(`{{.Operation}}`))
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err