Files
genqlient/generate/operation.go.tmpl
T
Craig SilversteinandGitHub 587f77046b Expose the graphql operation as a constant in the generated genqlient file (#238)
This allows client code to see the operation (query or mutation) exactly
as genqlient sends it over the wire. This data was already available in
the generated safelist.json file, but now it's easily available from Go
code as well.
    
Fixes #236

I have:
- [x] Written a clear PR title and description (above)
- [x] Signed the [Khan Academy CLA](https://www.khanacademy.org/r/cla)
- [x] Added tests covering my changes, if applicable
- [x] Included a link to the issue fixed, if applicable
- [x] Included documentation, for new features
- [x] Added an entry to the changelog
2022-11-21 08:35:09 -08:00

50 lines
1.4 KiB
Cheetah

// The query or mutation executed by {{.Name}}.
const {{.Name}}Operation = `{{$.Body}}`
{{.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}}
{{- if .Input -}}
{{- range .Input.Fields -}}
{{/* the GraphQL name here is the user-specified variable-name */ -}}
{{.GraphQLName}} {{.GoType.Reference}},
{{end -}}
{{end -}}
) (*{{.ResponseName}}, {{if .Config.Extensions -}}map[string]interface{},{{end}} error) {
req := &graphql.Request{
OpName: "{{.Name}}",
Query: {{.Name}}Operation,
{{if .Input -}}
Variables: &{{.Input.GoName}}{
{{range .Input.Fields -}}
{{.GoName}}: {{.GraphQLName}},
{{end -}}
},
{{end -}}
}
var err error
{{if .Config.ClientGetter -}}
var client graphql.Client
client, err = {{ref .Config.ClientGetter}}({{if ne .Config.ContextType "-"}}ctx{{else}}{{end}})
if err != nil {
return nil, {{if .Config.Extensions -}}nil,{{end -}} err
}
{{end}}
var data {{.ResponseName}}
resp := &graphql.Response{Data: &data}
err = client.MakeRequest(
{{if ne .Config.ContextType "-"}}ctx{{else}}nil{{end}},
req,
resp,
)
return &data, {{if .Config.Extensions -}}resp.Extensions,{{end -}} err
}