## Summary:
genqlient has some code (`imports.go`) dedicated to tracking which
imports we need and avoiding conflicts, as well as converting a
(restricted) Go expression like `map[string]github.com/me/mypkg.MyType`
to an import (`github.com/me/mypkg`) and a type-reference
(`map[string]mypkg.MyType`) to be used in the context of that import,
and at least making some attempt to track conflicts. (Right now the
conflict-avoidance is not very smart, and not very well tested, but it
comes up rarely anyway.) Sadly, that code was a bit cumbersome to use,
because you had to first register the imports (typically from
`convert.go`), then use them (often from the template).
In this commit I refactor the order we write things in order to allow a
significant simplification of how we import; in particular we no longer
have to guess in advance what imports which template will need; it can
just do `{{ref <expr>}}` as before, and it just works. To do this, I:
- changed the importer to have only one API, which adds the import if
needed, and returns the reference either way
- added a check that we don't add imports after they're written
- reorganized the toplevel templates a bit to make sure that check never
fires; we now generate all the types and operations, then write the
imports and glue it all together
This removes a bunch of silly code, and should simplify the process of
adding custom (un)marshalers (#38).
While I was at it, I put the documentation of what expressions we
support in a more visible place, and added a type-assertion that your
custom context type implements context.Context (if applicable).
## Test plan:
make check
Author: benjaminjkraft
Reviewers: StevenACoffman, dnerdy, aberkan, jvoll, mahtabsabet, MiguelCastillo
Required Reviewers:
Approved By: StevenACoffman, dnerdy
Checks: ⌛ Test (1.17), ⌛ Test (1.16), ⌛ Test (1.15), ⌛ Test (1.14), ⌛ Lint, ⌛ Test (1.17), ⌛ Test (1.16), ⌛ Test (1.15), ⌛ Test (1.14), ⌛ Lint
Pull Request URL: https://github.com/Khan/genqlient/pull/101
55 lines
1.4 KiB
Cheetah
55 lines
1.4 KiB
Cheetah
{{.Doc}}
|
|
func {{.Name}}(
|
|
{{if ne .Config.ContextType "-" -}}
|
|
ctx {{ref .Config.ContextType}},
|
|
{{end}}
|
|
{{- if not .Config.ClientGetter -}}
|
|
client {{ref "github.com/Khan/genqlient/graphql.Client"}},
|
|
{{end}}
|
|
{{- range .Args -}}
|
|
{{.GoName}} {{.GoType}},
|
|
{{end -}}
|
|
) (*{{.ResponseName}}, error) {
|
|
{{- if .Args -}}
|
|
variables := map[string]interface{}{
|
|
{{range .Args -}}
|
|
{{if not .Options.GetOmitempty -}}
|
|
"{{.GraphQLName}}": {{.GoName}},
|
|
{{end -}}
|
|
{{end}}
|
|
}
|
|
{{range .Args -}}
|
|
{{if .Options.GetOmitempty -}}
|
|
{{if .IsSlice -}}
|
|
if len({{.GoName}}) > 0 {
|
|
{{else -}}
|
|
{{/* zero_{{.GoType}} would be a better name, but {{.GoType}} would require
|
|
munging since it might be, say, `time.Time`. */}}
|
|
var zero_{{.GoName}} {{.GoType}}
|
|
if {{.GoName}} != zero_{{.GoName}} {
|
|
{{end -}}
|
|
variables["{{.GraphQLName}}"] = {{.GoName}}
|
|
}
|
|
{{end}}
|
|
{{end}}
|
|
{{end -}}
|
|
|
|
var err error
|
|
{{if .Config.ClientGetter -}}
|
|
client, err := {{ref .Config.ClientGetter}}({{if ne .Config.ContextType "-"}}ctx{{else}}{{end}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
{{end}}
|
|
|
|
var retval {{.ResponseName}}
|
|
err = client.MakeRequest(
|
|
{{if ne .Config.ContextType "-"}}ctx{{else}}nil{{end}},
|
|
"{{.Name}}",
|
|
`{{.Body}}`,
|
|
&retval,
|
|
{{if .Args}}variables{{else}}nil{{end}},
|
|
)
|
|
return &retval, err
|
|
}
|