From b66fff16de0b16edc317398a5574fcff2cb39e66 Mon Sep 17 00:00:00 2001 From: Bill Rose Date: Mon, 29 Aug 2022 11:44:20 -0400 Subject: [PATCH] Add omit_getters config option (#2348) Co-authored-by: Bill Rose --- README.md | 10 ++++ codegen/config/config.go | 1 + plugin/modelgen/models.go | 10 ++-- plugin/modelgen/models_test.go | 6 +++ .../modelgen/out_struct_pointers/generated.go | 48 ++----------------- .../testdata/gqlgen_struct_field_pointers.yml | 1 + 6 files changed, 29 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 6c61c85ecd..e0b45a5671 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,16 @@ first model in this list is used as the default type and it will always be used There isn't any way around this, gqlgen has no way to know what you want in a given context. +### Why do my interfaces have getters? Can I disable these? +These were added in v0.17.14 to allow accessing common interface fields without casting to a concrete type. +However, certain fields, like Relay-style Connections, cannot be implemented with simple getters. + +If you'd prefer to not have getters generated in your interfaces, you can add the following in your `gqlgen.yml`: +```yaml +# gqlgen.yml +omit_getters: true +``` + ## Other Resources - [Christopher Biscardi @ Gophercon UK 2018](https://youtu.be/FdURVezcdcw) diff --git a/codegen/config/config.go b/codegen/config/config.go index 9deba8b106..c6de8625f0 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -26,6 +26,7 @@ type Config struct { StructTag string `yaml:"struct_tag,omitempty"` Directives map[string]DirectiveConfig `yaml:"directives,omitempty"` OmitSliceElementPointers bool `yaml:"omit_slice_element_pointers,omitempty"` + OmitGetters bool `yaml:"omit_getters,omitempty"` StructFieldsAlwaysPointers bool `yaml:"struct_fields_always_pointers,omitempty"` ResolversAlwaysReturnPointers bool `yaml:"resolvers_always_return_pointers,omitempty"` SkipValidation bool `yaml:"skip_validation,omitempty"` diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 3cf62046b5..45c32715c6 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -103,9 +103,13 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error { } switch schemaType.Kind { case ast.Interface, ast.Union: - fields, err := m.generateFields(cfg, schemaType) - if err != nil { - return err + var fields []*Field + var err error + if !cfg.OmitGetters { + fields, err = m.generateFields(cfg, schemaType) + if err != nil { + return err + } } it := &Interface{ diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index 71dfbe5a8d..dab490305b 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -312,6 +312,12 @@ func TestModelGenerationStructFieldPointers(t *testing.T) { require.Nil(t, out_struct_pointers.Recursive{}.FieldThree) require.NotNil(t, out_struct_pointers.Recursive{}.FieldFour) }) + + t.Run("no getters", func(t *testing.T) { + generated, err := os.ReadFile("./out_struct_pointers/generated.go") + require.NoError(t, err) + require.NotContains(t, string(generated), "func (this") + }) } func mutateHook(b *ModelBuild) *ModelBuild { diff --git a/plugin/modelgen/out_struct_pointers/generated.go b/plugin/modelgen/out_struct_pointers/generated.go index 3b311c64d0..51f1034c1d 100644 --- a/plugin/modelgen/out_struct_pointers/generated.go +++ b/plugin/modelgen/out_struct_pointers/generated.go @@ -10,50 +10,38 @@ import ( type A interface { IsA() - GetA() string } type ArrayOfA interface { IsArrayOfA() - GetTrickyField() []A - GetTrickyFieldPointer() []A } type B interface { IsB() - GetB() int } type C interface { IsA() IsC() - GetA() string - GetC() bool } type D interface { IsA() IsB() IsD() - GetA() string - GetB() int - GetD() *string } type FooBarer interface { IsFooBarer() - GetName() string } // InterfaceWithDescription is an interface with a description type InterfaceWithDescription interface { IsInterfaceWithDescription() - GetName() *string } type MissingInterface interface { IsMissingInterface() - GetName() *string } type MissingUnion interface { @@ -72,17 +60,12 @@ type CDImplemented struct { D *string `json:"d" database:"CDImplementedd"` } -func (CDImplemented) IsC() {} -func (this CDImplemented) GetA() string { return this.A } -func (this CDImplemented) GetC() bool { return this.C } +func (CDImplemented) IsC() {} func (CDImplemented) IsA() {} func (CDImplemented) IsD() {} -func (this CDImplemented) GetB() int { return this.B } -func (this CDImplemented) GetD() *string { return this.D } - func (CDImplemented) IsB() {} type CyclicalA struct { @@ -113,26 +96,6 @@ type ImplArrayOfA struct { } func (ImplArrayOfA) IsArrayOfA() {} -func (this ImplArrayOfA) GetTrickyField() []A { - if this.TrickyField == nil { - return nil - } - interfaceSlice := make([]A, 0, len(this.TrickyField)) - for _, concrete := range this.TrickyField { - interfaceSlice = append(interfaceSlice, concrete) - } - return interfaceSlice -} -func (this ImplArrayOfA) GetTrickyFieldPointer() []A { - if this.TrickyFieldPointer == nil { - return nil - } - interfaceSlice := make([]A, 0, len(this.TrickyFieldPointer)) - for _, concrete := range this.TrickyFieldPointer { - interfaceSlice = append(interfaceSlice, concrete) - } - return interfaceSlice -} type MissingInput struct { Name *string `json:"name" database:"MissingInputname"` @@ -147,8 +110,7 @@ type MissingTypeNotNull struct { Missing2 MissingTypeNullable `json:"missing2" database:"MissingTypeNotNullmissing2"` } -func (MissingTypeNotNull) IsMissingInterface() {} -func (this MissingTypeNotNull) GetName() *string { return &this.Name } +func (MissingTypeNotNull) IsMissingInterface() {} func (MissingTypeNotNull) IsExistingInterface() {} @@ -164,8 +126,7 @@ type MissingTypeNullable struct { Missing2 *MissingTypeNotNull `json:"missing2" database:"MissingTypeNullablemissing2"` } -func (MissingTypeNullable) IsMissingInterface() {} -func (this MissingTypeNullable) GetName() *string { return this.Name } +func (MissingTypeNullable) IsMissingInterface() {} func (MissingTypeNullable) IsExistingInterface() {} @@ -206,8 +167,7 @@ type FooBarr struct { Name string `json:"name" database:"_Foo_Barrname"` } -func (FooBarr) IsFooBarer() {} -func (this FooBarr) GetName() string { return this.Name } +func (FooBarr) IsFooBarer() {} // EnumWithDescription is an enum with a description type EnumWithDescription string diff --git a/plugin/modelgen/testdata/gqlgen_struct_field_pointers.yml b/plugin/modelgen/testdata/gqlgen_struct_field_pointers.yml index 5b1fdd0b4b..f64dc6fea5 100644 --- a/plugin/modelgen/testdata/gqlgen_struct_field_pointers.yml +++ b/plugin/modelgen/testdata/gqlgen_struct_field_pointers.yml @@ -7,6 +7,7 @@ model: filename: out_struct_pointers/generated.go struct_fields_always_pointers: false +omit_getters: true models: ExistingModel: