Skip to content

Commit

Permalink
Merge pull request #872 from 99designs/autobind-prefix
Browse files Browse the repository at this point in the history
Allow prefixes when using autobind
  • Loading branch information
vektah authored Sep 24, 2019
2 parents de8e559 + ed2a853 commit 7b41ca3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
21 changes: 21 additions & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,27 @@ func (c *Config) Autobind(s *ast.Schema) error {
}
}

for i, t := range c.Models {
for j, m := range t.Model {
pkg, typename := code.PkgAndType(m)

// skip anything that looks like an import path
if strings.Contains(pkg, "/") {
continue
}

for _, p := range ps {
if p.Name != pkg {
continue
}
if t := p.Types.Scope().Lookup(typename); t != nil {
c.Models[i].Model[j] = t.Pkg().Path() + "." + t.Name()
break
}
}
}
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ resolver:
struct_tag: json

# Instead of listing out every model like below, you can automatically bind to any matching types
# within the given path. EXPERIMENTAL in v0.9.1
# within the given path by using `model: User` or `model: models.User`. EXPERIMENTAL in v0.9.1
autobind:
- github.com/my/app/models

# Tell gqlgen about any existing models you want to reuse for
# graphql. These normally come from the db or a remote api.
models:
User:
model: github.com/my/app/models.User
model: models.User # can use short paths when the package is listed in autobind
Todo:
model: github.com/my/app/models.Todo
model: github.com/my/app/models.Todo # or full paths if you need to go elsewhere
fields:
id:
resolver: true # force a resolver to be generated
Expand Down
2 changes: 1 addition & 1 deletion example/starwars/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ autobind:

models:
ReviewInput:
model: github.com/99designs/gqlgen/example/starwars/models.Review
model: models.Review
Starship:
fields:
length:
Expand Down

0 comments on commit 7b41ca3

Please sign in to comment.