Skip to content

Commit

Permalink
Merge pull request #1012 from 99designs/federation-config
Browse files Browse the repository at this point in the history
Allow configuring the federation output file location
  • Loading branch information
vektah authored Feb 6, 2020
2 parents afa9a15 + ad3c1c8 commit 1d41c2e
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion api/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Generate(cfg *config.Config, option ...Option) error {
plugins = append(plugins, modelgen.New())
}
plugins = append(plugins, resolvergen.New())
if cfg.Federated {
if cfg.Federation.IsDefined() {
plugins = append([]plugin.Plugin{federation.New()}, plugins...)
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ exec:
filename: graph/generated/generated.go
package: generated
# Uncomment to enable federation
# federation:
# filename: graph/generated/federation.go
# package: generated
# Where should any generated models go?
model:
filename: graph/model/models_gen.go
Expand Down
21 changes: 20 additions & 1 deletion codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ type Config struct {
SchemaFilename StringList `yaml:"schema,omitempty"`
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model,omitempty"`
Federation PackageConfig `yaml:"federation,omitempty"`
Resolver ResolverConfig `yaml:"resolver,omitempty"`
AutoBind []string `yaml:"autobind"`
Models TypeMap `yaml:"models,omitempty"`
StructTag string `yaml:"struct_tag,omitempty"`
Directives map[string]DirectiveConfig `yaml:"directives,omitempty"`
OmitSliceElementPointers bool `yaml:"omit_slice_element_pointers,omitempty"`
SkipValidation bool `yaml:"skip_validation,omitempty"`
Federated bool `yaml:"federated,omitempty"`
AdditionalSources []*ast.Source `yaml:"-"`
Packages *code.Packages `yaml:"-"`
Schema *ast.Schema `yaml:"-"`

// Deprecated use Federation instead. Will be removed next release
Federated bool `yaml:"federated,omitempty"`
}

var cfgFilenames = []string{".gqlgen.yml", "gqlgen.yml", "gqlgen.yaml"}
Expand Down Expand Up @@ -330,6 +333,22 @@ func (c *Config) check() error {
Declaree: "resolver",
})
}
if c.Federation.IsDefined() {
if err := c.Federation.Check(); err != nil {
return errors.Wrap(err, "config.federation")
}
fileList[c.Federation.ImportPath()] = append(fileList[c.Federation.ImportPath()], FilenamePackage{
Filename: c.Federation.Filename,
Package: c.Federation.Package,
Declaree: "federation",
})
if c.Federation.ImportPath() != c.Exec.ImportPath() {
return fmt.Errorf("federation and exec must be in the same package")
}
}
if c.Federated {
return fmt.Errorf("federated has been removed, instead use\nfederation:\n filename: path/to/federated.go")
}

for importPath, pkg := range fileList {
for _, file1 := range pkg {
Expand Down
36 changes: 32 additions & 4 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,39 @@ func TestReferencedPackages(t *testing.T) {

func TestConfigCheck(t *testing.T) {
t.Run("invalid config format due to conflicting package names", func(t *testing.T) {
config, err := LoadConfig("testdata/cfg/conflictedPackages.yml")
require.NoError(t, err)
config := Config{
Exec: PackageConfig{Filename: "generated/exec.go", Package: "graphql"},
Model: PackageConfig{Filename: "generated/models.go"},
}

require.EqualError(t, config.check(), "exec and model define the same import path (github.com/99designs/gqlgen/codegen/config/generated) with different package names (graphql vs generated)")
})

t.Run("federation must be in exec package", func(t *testing.T) {
config := Config{
Exec: PackageConfig{Filename: "generated/exec.go"},
Federation: PackageConfig{Filename: "anotherpkg/federation.go"},
}

require.EqualError(t, config.check(), "federation and exec must be in the same package")
})

t.Run("federation must have same package name as exec", func(t *testing.T) {
config := Config{
Exec: PackageConfig{Filename: "generated/exec.go"},
Federation: PackageConfig{Filename: "generated/federation.go", Package: "federation"},
}

require.EqualError(t, config.check(), "exec and federation define the same import path (github.com/99designs/gqlgen/codegen/config/generated) with different package names (generated vs federation)")
})

t.Run("deprecated federated flag raises an error", func(t *testing.T) {
config := Config{
Exec: PackageConfig{Filename: "generated/exec.go"},
Federated: true,
}

err = config.check()
require.EqualError(t, err, "exec and model define the same import path (github.com/99designs/gqlgen/codegen/config/generated) with different package names (graphql vs generated)")
require.EqualError(t, config.check(), "federated has been removed, instead use\nfederation:\n filename: path/to/federated.go")
})
}

Expand Down
10 changes: 0 additions & 10 deletions codegen/config/testdata/cfg/conflictedPackages.yml

This file was deleted.

5 changes: 5 additions & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ exec:
filename: graph/generated/generated.go
package: generated

# Enable Apollo federation support
federation:
filename: graph/generated/federation.go
package: generated

# Where should any generated models go?
model:
filename: graph/model/models_gen.go
Expand Down
4 changes: 2 additions & 2 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func (f *federation) GenerateCode(data *codegen.Data) error {
}

return templates.Render(templates.Options{
PackageName: data.Config.Exec.Package,
Filename: "service.go",
PackageName: data.Config.Federation.Package,
Filename: data.Config.Federation.Filename,
Data: f,
GeneratedHeader: true,
Packages: data.Config.Packages,
Expand Down
5 changes: 4 additions & 1 deletion plugin/federation/test_data/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
schema:
- "test_data/schema.graphql"
federated: true
exec:
filename: graph/generated/exec.go
federation:
filename: graph/generated/federation.go
5 changes: 4 additions & 1 deletion plugin/federation/test_data/nokey.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
schema:
- "test_data/nokey.graphql"
federated: true
exec:
filename: graph/generated/exec.go
federation:
filename: graph/generated/federation.go

0 comments on commit 1d41c2e

Please sign in to comment.