allow omitting context
This commit is contained in:
@@ -53,6 +53,8 @@ For a complete working example, see `example/`.
|
|||||||
|
|
||||||
`go test ./...` does some perfunctory tests. (This is run by GitHub Actions.)
|
`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.
|
TODO(benkraft): Figure out how to get GitHub Actions to run the example -- it needs a token.
|
||||||
|
|
||||||
## Major TODOs
|
## Major TODOs
|
||||||
@@ -62,9 +64,7 @@ Query structures to support:
|
|||||||
- fragments
|
- fragments
|
||||||
|
|
||||||
Config options:
|
Config options:
|
||||||
- file locations (queries, generated, schema (or get via HTTP))
|
- get schema via HTTP (perhaps even via GraphQL introspection)
|
||||||
- use ctx or not, including complexities of how Khan uses context
|
|
||||||
- HTTP calling convention (is there enough variation to matter?)
|
|
||||||
- proper config/arguments setup (e.g. with [viper](https://github.com/spf13/viper)
|
- proper config/arguments setup (e.g. with [viper](https://github.com/spf13/viper)
|
||||||
|
|
||||||
Other:
|
Other:
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
# These are the defaults.
|
# These are the defaults, and are just included to be explicit.
|
||||||
package: example
|
package: example
|
||||||
schema: schema.graphql
|
schema: schema.graphql
|
||||||
queries: queries.graphql
|
queries: queries.graphql
|
||||||
generated: generated.go
|
generated: generated.go
|
||||||
|
use_context: true
|
||||||
|
|||||||
+9
-17
@@ -9,9 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var defaultConfig = &Config{
|
var defaultConfig = &Config{
|
||||||
Schema: "schema.graphql",
|
Schema: "schema.graphql",
|
||||||
Queries: "queries.graphql",
|
Queries: "queries.graphql",
|
||||||
Generated: "generated.go",
|
Generated: "generated.go",
|
||||||
|
UseContext: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -28,19 +29,12 @@ type Config struct {
|
|||||||
// The filename to which to write the generated code; defaults to
|
// The filename to which to write the generated code; defaults to
|
||||||
// generated.go
|
// generated.go
|
||||||
Generated string `yaml:"generated"`
|
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 {
|
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 == "" {
|
if c.Package == "" {
|
||||||
abs, err := filepath.Abs(c.Generated)
|
abs, err := filepath.Abs(c.Generated)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -56,10 +50,8 @@ func (c *Config) ValidateAndFillDefaults() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadAndValidateConfig(filename string) (*Config, error) {
|
func ReadAndValidateConfig(filename string) (*Config, error) {
|
||||||
var config Config
|
config := *defaultConfig
|
||||||
if filename == "" {
|
if filename != "" {
|
||||||
config = *defaultConfig
|
|
||||||
} else {
|
|
||||||
text, err := ioutil.ReadFile(filename)
|
text, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unreadable config file %v: %v", filename, err)
|
return nil, fmt.Errorf("unreadable config file %v: %v", filename, err)
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ var tmpl = template.Must(template.ParseFiles(tmplAbsFilename))
|
|||||||
// generator is the context for the codegen process (and ends up getting passed
|
// generator is the context for the codegen process (and ends up getting passed
|
||||||
// to the template).
|
// to the template).
|
||||||
type generator struct {
|
type generator struct {
|
||||||
// The name of the package into which to generate the operation-helpers.
|
// The config for which we are generating code.
|
||||||
PackageName string
|
Config *Config
|
||||||
// The list of operations for which to generate code.
|
// The list of operations for which to generate code.
|
||||||
Operations []operation
|
Operations []operation
|
||||||
// The types needed for these operations.
|
// The types needed for these operations.
|
||||||
@@ -54,11 +54,11 @@ type argument struct {
|
|||||||
GraphQLName string
|
GraphQLName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGenerator(packageName string, schema *ast.Schema) *generator {
|
func newGenerator(config *Config, schema *ast.Schema) *generator {
|
||||||
return &generator{
|
return &generator{
|
||||||
PackageName: packageName,
|
Config: config,
|
||||||
typeMap: map[string]string{},
|
typeMap: map[string]string{},
|
||||||
schema: schema,
|
schema: schema,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ func Generate(config *Config) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
g := newGenerator(config.Package, schema)
|
g := newGenerator(config, schema)
|
||||||
for _, op := range document.Operations {
|
for _, op := range document.Operations {
|
||||||
if err = g.addOperation(op); err != nil {
|
if err = g.addOperation(op); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package {{.PackageName}}
|
package {{$.Config.Package}}
|
||||||
|
|
||||||
// Code generated by github.com/Khan/genql, DO NOT EDIT.
|
// Code generated by github.com/Khan/genql, DO NOT EDIT.
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
{{range .Operations}}
|
{{range .Operations}}
|
||||||
{{.Doc}}
|
{{.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 -}}
|
{{- if .Args -}}
|
||||||
variables := map[string]interface{}{
|
variables := map[string]interface{}{
|
||||||
{{range .Args -}}
|
{{range .Args -}}
|
||||||
@@ -21,7 +21,7 @@ func {{.Name}}(ctx context.Context, client *graphql.Client{{range .Args}}, {{.Go
|
|||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
var retval {{.ResponseName}}
|
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
|
return &retval, err
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
Reference in New Issue
Block a user