From 7b345cab94a15b7f7d6102f19b352eb3bc7634c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Endika=20Guti=C3=A9rrez?= Date: Thu, 22 Jun 2023 22:32:36 +0200 Subject: [PATCH] Added flag to omit interface checks --- codegen/config/config.go | 1 + docs/content/config.md | 3 +++ init-templates/gqlgen.yml.gotmpl | 3 +++ plugin/modelgen/models.go | 2 ++ plugin/modelgen/models.gotpl | 8 +++++--- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/codegen/config/config.go b/codegen/config/config.go index b1b3a5cca3e..8a7205f06bf 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -29,6 +29,7 @@ type Config struct { GoInitialisms GoInitialismsConfig `yaml:"go_initialisms,omitempty"` OmitSliceElementPointers bool `yaml:"omit_slice_element_pointers,omitempty"` OmitGetters bool `yaml:"omit_getters,omitempty"` + OmitInterfaceChecks bool `yaml:"omit_interface_checks,omitempty"` OmitComplexity bool `yaml:"omit_complexity,omitempty"` OmitGQLGenFileNotice bool `yaml:"omit_gqlgen_file_notice,omitempty"` OmitGQLGenVersionInFileNotice bool `yaml:"omit_gqlgen_version_in_file_notice,omitempty"` diff --git a/docs/content/config.md b/docs/content/config.md index 493da22cd0c..7233055346e 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -46,6 +46,9 @@ resolver: # Optional: turn on to use []Thing instead of []*Thing # omit_slice_element_pointers: false +# Optional: turn on to omit Is() methods to interface and unions +# omit_interface_checks : true + # Optional: turn on to skip generation of ComplexityRoot struct content and Complexity function # omit_complexity: false diff --git a/init-templates/gqlgen.yml.gotmpl b/init-templates/gqlgen.yml.gotmpl index 4e0ecf8a5d2..6e97f8bf716 100644 --- a/init-templates/gqlgen.yml.gotmpl +++ b/init-templates/gqlgen.yml.gotmpl @@ -32,6 +32,9 @@ resolver: # Optional: turn on to use []Thing instead of []*Thing # omit_slice_element_pointers: false +# Optional: turn on to omit Is() methods to interface and unions +# omit_interface_checks : true + # Optional: turn on to skip generation of ComplexityRoot struct content and Complexity function # omit_complexity: false diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index a649a2378eb..6b4f4837527 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -50,6 +50,7 @@ type Interface struct { Name string Fields []*Field Implements []string + OmitCheck bool } type Object struct { @@ -124,6 +125,7 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error { Name: schemaType.Name, Implements: schemaType.Interfaces, Fields: fields, + OmitCheck: cfg.OmitInterfaceChecks, } b.Interfaces = append(b.Interfaces, it) diff --git a/plugin/modelgen/models.gotpl b/plugin/modelgen/models.gotpl index 7ad43bef1bd..b11146939b9 100644 --- a/plugin/modelgen/models.gotpl +++ b/plugin/modelgen/models.gotpl @@ -15,10 +15,12 @@ {{- range $model := .Interfaces }} {{ with .Description }} {{.|prefixLines "// "}} {{ end }} type {{ goModelName .Name }} interface { - {{- range $impl := .Implements }} - Is{{ goModelName $impl }}() + {{- if not .OmitCheck }} + {{- range $impl := .Implements }} + Is{{ goModelName $impl }}() + {{- end }} + Is{{ goModelName .Name }}() {{- end }} - Is{{ goModelName .Name }}() {{- range $field := .Fields }} {{- with .Description }} {{.|prefixLines "// "}}