Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding GraphQL Extensions #184

Merged
merged 9 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ When releasing a new version:
### Breaking changes:

- genqlient now requires Go 1.16 or higher.
- The [`graphql.Client`](https://pkg.go.dev/github.com/Khan/genqlient/graphql#Client) interface now accepts two structs for the request and response, to allow future expansion, rather than several individual arguments. Clients implementing the interface themselves will need to change the signature; clients who simply call `graphql.NewClient` are unaffected.

### New features:

- genqlient can now run as a portable binary (i.e. without a local checkout of the repository or `go run`).
- You can now enable `use_extensions` in the configuration file, to receive extensions returned by the GraphQL API server. Generated functions will return extensions as `map[string]interface{}`, if enabled.

### Bug fixes:

Expand Down
10 changes: 10 additions & 0 deletions docs/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ client_getter: "github.com/you/yourpkg.GetClient"
# Defaults to false.
use_struct_references: boolean

# If set, generated code will have a third return parameter of type
# map[string]interface{}. This will contain the optional values
# of the Extensions field send from Servers.
# ref.: https://spec.graphql.org/October2021/#sec-Response-Format
#
# This can be useful for extending the GraphQL Protocol.
#
# Defaults to false.
use_extensions: boolean


# A map from GraphQL type name to Go fully-qualified type name to override
# the Go type genqlient will use for this GraphQL type.
Expand Down
56 changes: 33 additions & 23 deletions example/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion example/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ schema: schema.graphql
operations:
- genqlient.graphql
generated: generated.go

# needed since it doesn't match the directory name:
package: main

Expand Down
1 change: 1 addition & 0 deletions generate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
ClientGetter string `yaml:"client_getter"`
Bindings map[string]*TypeBinding `yaml:"bindings"`
StructReferences bool `yaml:"use_struct_references"`
Extensions bool `yaml:"use_extensions"`

// Set to true to use features that aren't fully ready to use.
//
Expand Down
4 changes: 4 additions & 0 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func TestGenerateWithConfig(t *testing.T) {
ClientGetter: "github.com/Khan/genqlient/internal/testutil.GetClientFromNowhere",
ContextType: "-",
}},
{"Extensions", "", &Config{
Generated: "generated.go",
Extensions: true,
}},
}

sourceFilename := "SimpleQuery.graphql"
Expand Down
35 changes: 18 additions & 17 deletions generate/operation.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ func {{.Name}}(
{{.GraphQLName}} {{.GoType.Reference}},
{{end -}}
{{end -}}
) (*{{.ResponseName}}, error) {
{{- if .Input -}}
{{/* We need to avoid conflicting with any of the function's argument names
which are derived from the GraphQL argument names; notably `input` is
a common one. So we use a name that's not legal in GraphQL, namely
one starting with a double-underscore. */ -}}
__input := {{.Input.GoName}}{
) (*{{.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 -}}
client, err := {{ref .Config.ClientGetter}}({{if ne .Config.ContextType "-"}}ctx{{else}}{{end}})
var client graphql.Client

client, err = {{ref .Config.ClientGetter}}({{if ne .Config.ContextType "-"}}ctx{{else}}{{end}})
if err != nil {
return nil, err
return nil, {{if .Config.Extensions -}}nil,{{end -}} err
}
{{end}}
var data {{.ResponseName}}
resp := &graphql.Response{Data: &data}

var retval {{.ResponseName}}
err = client.MakeRequest(
{{if ne .Config.ContextType "-"}}ctx{{else}}nil{{end}},
"{{.Name}}",
`{{.Body}}`,
&retval,
{{if .Input}}&__input{{else}}nil{{end}},
req,
resp,
)
return &retval, err

return &data, {{if .Config.Extensions -}}resp.Extensions,{{end -}} err
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading