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

Add configuration AutoBindings method #169

Merged
merged 8 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ When releasing a new version:
- genqlient now generates getter methods for all fields, even those which do not implement a genqlient-generated interface; this can be useful for callers who wish to define their own interface and have several unrelated genqlient types which have the same fields implement it.
- genqlient config now accepts either a single or multiple files for the `schema` and `operations` fields (previously it accepted only one `schema`, and required a list of `operations` files).
- The `typename` option can now be used on basic types (string, int, etc) as well as structs; this can be useful to have genqlient define new types like `type Language string` and use that type for specified fields.
- genqlient config add `auto_bindings` types from package source.

### Bug fixes:

Expand Down
5 changes: 5 additions & 0 deletions docs/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ client_getter: "github.com/you/yourpkg.GetClient"
use_struct_references: boolean


# Auto add list of Type from package.
# if type is defined in bindings part. Is not replaced.
auto_binding: github.com/you/yourpkg/model


# 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
33 changes: 33 additions & 0 deletions generate/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package generate

import (
"fmt"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"unicode"

"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
)

Expand All @@ -27,6 +30,7 @@ type Config struct {
ContextType string `yaml:"context_type"`
ClientGetter string `yaml:"client_getter"`
Bindings map[string]*TypeBinding `yaml:"bindings"`
AutoBindings string `yaml:"auto_bindings"`
NuVivo314 marked this conversation as resolved.
Show resolved Hide resolved
StructReferences bool `yaml:"use_struct_references"`

// Set to true to use features that aren't fully ready to use.
Expand Down Expand Up @@ -87,6 +91,35 @@ func (c *Config) ValidateAndFillDefaults(baseDir string) error {
c.Package = base
}

if c.AutoBindings != "" {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedTypes | packages.NeedTypesInfo,
}, c.AutoBindings)
if err != nil {
return err
}

for idx := range pkgs {
p := pkgs[idx].Types
if p == nil || p.Scope() == nil {
continue
}

for _, typ := range p.Scope().Names() {
if unicode.IsUpper(rune(typ[0])) {
NuVivo314 marked this conversation as resolved.
Show resolved Hide resolved
// Check if type is manual bindings
_, exist := c.Bindings[typ]
if !exist {
pathType := fmt.Sprintf("%s.%s", p.Path(), typ)
c.Bindings[typ] = &TypeBinding{
Type: pathType,
}
}
}
}
}
}

return nil
}

Expand Down