From ad3c1c818f86e8e610733e4be0cf6f755e60af25 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 6 Feb 2020 19:23:09 +1100 Subject: [PATCH] Allow configuring the federation output file location --- api/generate.go | 2 +- cmd/init.go | 5 +++ codegen/config/config.go | 21 ++++++++++- codegen/config/config_test.go | 36 ++++++++++++++++--- .../testdata/cfg/conflictedPackages.yml | 10 ------ docs/content/config.md | 5 +++ plugin/federation/federation.go | 4 +-- plugin/federation/test_data/gqlgen.yml | 5 ++- plugin/federation/test_data/nokey.yml | 5 ++- 9 files changed, 73 insertions(+), 20 deletions(-) delete mode 100644 codegen/config/testdata/cfg/conflictedPackages.yml diff --git a/api/generate.go b/api/generate.go index b64626401be..da148540743 100644 --- a/api/generate.go +++ b/api/generate.go @@ -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...) } diff --git a/cmd/init.go b/cmd/init.go index 0dff3ddeea6..d81588447dc 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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 diff --git a/codegen/config/config.go b/codegen/config/config.go index a4d676a2a66..f0a7f14f7c2 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -20,6 +20,7 @@ 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"` @@ -27,10 +28,12 @@ type Config struct { 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"} @@ -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 { diff --git a/codegen/config/config_test.go b/codegen/config/config_test.go index 2cb15becc23..28adff3f712 100644 --- a/codegen/config/config_test.go +++ b/codegen/config/config_test.go @@ -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") }) } diff --git a/codegen/config/testdata/cfg/conflictedPackages.yml b/codegen/config/testdata/cfg/conflictedPackages.yml deleted file mode 100644 index ec6ceea515a..00000000000 --- a/codegen/config/testdata/cfg/conflictedPackages.yml +++ /dev/null @@ -1,10 +0,0 @@ -schema: - - schema.graphql -exec: - filename: generated/exec.go - package: graphql -model: - filename: generated/models.go -resolver: - filename: generated/resolver.go - type: Resolver diff --git a/docs/content/config.md b/docs/content/config.md index cbc51d4ccb4..7a51851460b 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -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 diff --git a/plugin/federation/federation.go b/plugin/federation/federation.go index 37994f5b35c..2fb36e3b9dd 100644 --- a/plugin/federation/federation.go +++ b/plugin/federation/federation.go @@ -240,8 +240,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, diff --git a/plugin/federation/test_data/gqlgen.yml b/plugin/federation/test_data/gqlgen.yml index 284f88e2004..51e7fa628b8 100644 --- a/plugin/federation/test_data/gqlgen.yml +++ b/plugin/federation/test_data/gqlgen.yml @@ -1,3 +1,6 @@ schema: - "test_data/schema.graphql" -federated: true +exec: + filename: graph/generated/exec.go +federation: + filename: graph/generated/federation.go diff --git a/plugin/federation/test_data/nokey.yml b/plugin/federation/test_data/nokey.yml index ca370c38ef7..49ef0e9688d 100644 --- a/plugin/federation/test_data/nokey.yml +++ b/plugin/federation/test_data/nokey.yml @@ -1,3 +1,6 @@ schema: - "test_data/nokey.graphql" -federated: true +exec: + filename: graph/generated/exec.go +federation: + filename: graph/generated/federation.go