allow omitting context

This commit is contained in:
Ben Kraft
2020-05-01 18:45:31 -07:00
parent 1156e6ceae
commit 5042a9a549
5 changed files with 24 additions and 31 deletions
+3 -3
View File
@@ -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:
+2 -1
View File
@@ -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
+9 -17
View File
@@ -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)
+7 -7
View File
@@ -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
+3 -3
View File
@@ -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}}