Skip to content

Commit

Permalink
Allow prefixes when using autobind
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Sep 22, 2019
1 parent 17f32d2 commit ed2a853
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 ed2a853

Please sign in to comment.