Skip to content

Commit

Permalink
Add file/package configuration options when generating service.go
Browse files Browse the repository at this point in the history
This patch adds the option for users to specific the filename and/or
package of the `service.go` file. To configure, users will need to add a
new section like the following to their `gqlgen.yml` file similar to the
ones that exist now for `exec` and `resolver:

```
service:
 filename: generated/service.go
  package: generated
```
  • Loading branch information
dkapadia committed Sep 24, 2019
1 parent 63e6214 commit dd02f6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model"`
Resolver PackageConfig `yaml:"resolver,omitempty"`
Service PackageConfig `yaml:"service,omitempty"`
AutoBind []string `yaml:"autobind"`
Models TypeMap `yaml:"models,omitempty"`
StructTag string `yaml:"struct_tag,omitempty"`
Expand All @@ -39,6 +40,7 @@ func DefaultConfig() *Config {
SchemaFilename: StringList{"schema.graphql"},
Model: PackageConfig{Filename: "models_gen.go"},
Exec: PackageConfig{Filename: "generated.go"},
Service: PackageConfig{Filename: "service.go"},
Directives: map[string]DirectiveConfig{},
}
}
Expand Down Expand Up @@ -236,13 +238,19 @@ func (c *Config) Check() error {
return errors.Wrap(err, "config.resolver")
}
}
if c.Service.IsDefined() {
if err := c.Service.Check(); err != nil {
return errors.Wrap(err, "config.service")
}
}

// check packages names against conflict, if present in the same dir
// and check filenames for uniqueness
packageConfigList := []PackageConfig{
c.Model,
c.Exec,
c.Resolver,
c.Service,
}
filesMap := make(map[string]bool)
pkgConfigsByDir := make(map[string]PackageConfig)
Expand Down Expand Up @@ -378,6 +386,12 @@ func (c *Config) normalize() error {
}
}

if c.Service.IsDefined() {
if err := c.Service.normalize(); err != nil {
return errors.Wrap(err, "service")
}
}

if c.Models == nil {
c.Models = TypeMap{}
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ func (f *federation) GenerateCode(data *codegen.Data) error {
}
return templates.Render(templates.Options{
Template: tmpl,
PackageName: data.Config.Exec.Package,
Filename: "service.go",
PackageName: data.Config.Service.Package,
Filename: data.Config.Service.Filename,
Data: f,
GeneratedHeader: true,
})
Expand Down

0 comments on commit dd02f6d

Please sign in to comment.