forked from Khan/genqlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go.tmpl
46 lines (43 loc) · 1.32 KB
/
operation.go.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{{.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: `{{.Body}}`,
{{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
}