From 5042a9a549dd4d8ad3f0d2a7e2eb2c5a6e88bdf4 Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Fri, 1 May 2020 18:45:31 -0700 Subject: [PATCH] allow omitting context --- README.md | 6 +++--- example/genql.yaml | 3 ++- generate/config.go | 26 +++++++++----------------- generate/generate.go | 14 +++++++------- generate/operation.go.tmpl | 6 +++--- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 807472c..2d9a551 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ For a complete working example, see `example/`. `go test ./...` does some perfunctory tests. (This is run by GitHub Actions.) +`make example` tests that everything wires up to a real API correctly. + TODO(benkraft): Figure out how to get GitHub Actions to run the example -- it needs a token. ## Major TODOs @@ -62,9 +64,7 @@ Query structures to support: - fragments Config options: -- file locations (queries, generated, schema (or get via HTTP)) -- use ctx or not, including complexities of how Khan uses context -- HTTP calling convention (is there enough variation to matter?) +- get schema via HTTP (perhaps even via GraphQL introspection) - proper config/arguments setup (e.g. with [viper](https://github.com/spf13/viper) Other: diff --git a/example/genql.yaml b/example/genql.yaml index e6def54..8792985 100644 --- a/example/genql.yaml +++ b/example/genql.yaml @@ -1,5 +1,6 @@ -# These are the defaults. +# These are the defaults, and are just included to be explicit. package: example schema: schema.graphql queries: queries.graphql generated: generated.go +use_context: true diff --git a/generate/config.go b/generate/config.go index abd4a1c..41f7591 100644 --- a/generate/config.go +++ b/generate/config.go @@ -9,9 +9,10 @@ import ( ) var defaultConfig = &Config{ - Schema: "schema.graphql", - Queries: "queries.graphql", - Generated: "generated.go", + Schema: "schema.graphql", + Queries: "queries.graphql", + Generated: "generated.go", + UseContext: true, } type Config struct { @@ -28,19 +29,12 @@ type Config struct { // The filename to which to write the generated code; defaults to // generated.go Generated string `yaml:"generated"` + // Whether the generated helpers should accept a context.Context which will + // be used to make the request; defaults to true. + UseContext bool `yaml:"use_context"` } func (c *Config) ValidateAndFillDefaults() error { - if c.Schema == "" { - c.Schema = defaultConfig.Schema - } - if c.Queries == "" { - c.Queries = defaultConfig.Queries - } - if c.Generated == "" { - c.Generated = defaultConfig.Generated - } - if c.Package == "" { abs, err := filepath.Abs(c.Generated) if err != nil { @@ -56,10 +50,8 @@ func (c *Config) ValidateAndFillDefaults() error { } func ReadAndValidateConfig(filename string) (*Config, error) { - var config Config - if filename == "" { - config = *defaultConfig - } else { + config := *defaultConfig + if filename != "" { text, err := ioutil.ReadFile(filename) if err != nil { return nil, fmt.Errorf("unreadable config file %v: %v", filename, err) diff --git a/generate/generate.go b/generate/generate.go index 3bba20c..1784779 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -24,8 +24,8 @@ var tmpl = template.Must(template.ParseFiles(tmplAbsFilename)) // generator is the context for the codegen process (and ends up getting passed // to the template). type generator struct { - // The name of the package into which to generate the operation-helpers. - PackageName string + // The config for which we are generating code. + Config *Config // The list of operations for which to generate code. Operations []operation // The types needed for these operations. @@ -54,11 +54,11 @@ type argument struct { GraphQLName string } -func newGenerator(packageName string, schema *ast.Schema) *generator { +func newGenerator(config *Config, schema *ast.Schema) *generator { return &generator{ - PackageName: packageName, - typeMap: map[string]string{}, - schema: schema, + Config: config, + typeMap: map[string]string{}, + schema: schema, } } @@ -157,7 +157,7 @@ func Generate(config *Config) ([]byte, error) { return nil, err } - g := newGenerator(config.Package, schema) + g := newGenerator(config, schema) for _, op := range document.Operations { if err = g.addOperation(op); err != nil { return nil, err diff --git a/generate/operation.go.tmpl b/generate/operation.go.tmpl index 28e7b34..0b3734a 100644 --- a/generate/operation.go.tmpl +++ b/generate/operation.go.tmpl @@ -1,4 +1,4 @@ -package {{.PackageName}} +package {{$.Config.Package}} // Code generated by github.com/Khan/genql, DO NOT EDIT. @@ -12,7 +12,7 @@ import ( {{range .Operations}} {{.Doc}} -func {{.Name}}(ctx context.Context, client *graphql.Client{{range .Args}}, {{.GoName}} {{.GoType}}{{end}}) (*{{.ResponseName}}, error) { +func {{.Name}}({{if $.Config.UseContext}}ctx context.Context, {{end}}client *graphql.Client{{range .Args}}, {{.GoName}} {{.GoType}}{{end}}) (*{{.ResponseName}}, error) { {{- if .Args -}} variables := map[string]interface{}{ {{range .Args -}} @@ -21,7 +21,7 @@ func {{.Name}}(ctx context.Context, client *graphql.Client{{range .Args}}, {{.Go } {{end}} var retval {{.ResponseName}} - err := client.MakeRequest(ctx, `{{.Body}}`, &retval, {{if .Args}}variables{{else}}nil{{end}}) + err := client.MakeRequest({{if $.Config.UseContext}}ctx{{else}}context.Background(){{end}}, `{{.Body}}`, &retval, {{if .Args}}variables{{else}}nil{{end}}) return &retval, err } {{end}}