github.com/nathanstitt/genqlient@v0.3.1-0.20211028004951-a2bda3c41ab8/generate/operation.go.tmpl (about)

     1  {{.Doc}}
     2  func {{.Name}}(
     3      {{if ne .Config.ContextType "-" -}}
     4      ctx {{ref .Config.ContextType}},
     5      {{end}}
     6      {{- if not .Config.ClientGetter -}}
     7      client {{ref "github.com/Khan/genqlient/graphql.Client"}},
     8      {{end}}
     9      {{- if .Input -}}
    10      {{- range .Input.Fields -}}
    11      {{/* the GraphQL name here is the user-specified variable-name */ -}}
    12      {{.GraphQLName}} {{.GoType.Reference}},
    13      {{end -}}
    14      {{end -}}
    15  ) (*{{.ResponseName}}, error) {
    16      {{- if .Input -}}
    17      {{/* We need to avoid conflicting with any of the function's argument names
    18           which are derived from the GraphQL argument names; notably `input` is
    19           a common one.  So we use a name that's not legal in GraphQL, namely
    20           one starting with a double-underscore. */ -}}
    21      __input := {{.Input.GoName}}{
    22          {{range .Input.Fields -}}
    23          {{.GoName}}: {{.GraphQLName}},
    24          {{end -}}
    25      }
    26      {{end -}}
    27  
    28      var err error
    29      {{if .Config.ClientGetter -}}
    30      client, err := {{ref .Config.ClientGetter}}({{if ne .Config.ContextType "-"}}ctx{{else}}{{end}})
    31      if err != nil {
    32          return nil, err
    33      }
    34      {{end}}
    35  
    36  	var retval {{.ResponseName}}
    37      err = client.MakeRequest(
    38          {{if ne .Config.ContextType "-"}}ctx{{else}}nil{{end}},
    39          "{{.Name}}",
    40          `{{.Body}}`,
    41          &retval,
    42          {{if .Input}}&__input{{else}}nil{{end}},
    43      )
    44      return &retval, err
    45  }