Skip to content

Commit

Permalink
Add omit_getters config option (#2348)
Browse files Browse the repository at this point in the history
Co-authored-by: Bill Rose <neptoess@gmail.com>
  • Loading branch information
neptoess and Bill Rose committed Aug 29, 2022
1 parent 2ba8040 commit b66fff1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 47 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions codegen/config/config.go
Expand Up @@ -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"`
Expand Down
10 changes: 7 additions & 3 deletions plugin/modelgen/models.go
Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions plugin/modelgen/models_test.go
Expand Up @@ -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 {
Expand Down
48 changes: 4 additions & 44 deletions plugin/modelgen/out_struct_pointers/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugin/modelgen/testdata/gqlgen_struct_field_pointers.yml
Expand Up @@ -7,6 +7,7 @@ model:
filename: out_struct_pointers/generated.go

struct_fields_always_pointers: false
omit_getters: true

models:
ExistingModel:
Expand Down

0 comments on commit b66fff1

Please sign in to comment.