From fa4711801c59d27db887a50e1f8393006268194e Mon Sep 17 00:00:00 2001 From: "Roman A. Grigorovich" Date: Fri, 8 Sep 2023 15:12:15 +0300 Subject: [PATCH] ForceGenerate parameter to @goModel added. (#2780) * ForceGenerate parameter to @goModel added. * forceGenerate to docs added --------- Co-authored-by: Roman A. Grigorovich --- codegen/config/config.go | 23 +++++++++++++++++++++-- docs/content/config.md | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/codegen/config/config.go b/codegen/config/config.go index 9f4be075ff9..ddf10ac21d3 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -284,6 +284,7 @@ func (c *Config) injectTypesFromSchema() error { c.Models.Add(schemaType.Name, mv.(string)) } } + if ma := bd.Arguments.ForName("models"); ma != nil { if mvs, err := ma.Value.Value(nil); err == nil { for _, mv := range mvs.([]interface{}) { @@ -291,6 +292,12 @@ func (c *Config) injectTypesFromSchema() error { } } } + + if fg := bd.Arguments.ForName("forceGenerate"); fg != nil { + if mv, err := fg.Value.Value(nil); err == nil { + c.Models.ForceGenerate(schemaType.Name, mv.(bool)) + } + } } if schemaType.Kind == ast.Object || schemaType.Kind == ast.InputObject { @@ -332,8 +339,9 @@ func (c *Config) injectTypesFromSchema() error { } type TypeMapEntry struct { - Model StringList `yaml:"model"` - Fields map[string]TypeMapField `yaml:"fields,omitempty"` + Model StringList `yaml:"model,omitempty"` + ForceGenerate bool `yaml:"forceGenerate,omitempty"` + Fields map[string]TypeMapField `yaml:"fields,omitempty"` // Key is the Go name of the field. ExtraFields map[string]ModelExtraField `yaml:"extraFields,omitempty"` @@ -532,6 +540,12 @@ func (tm TypeMap) Add(name string, goType string) { tm[name] = modelCfg } +func (tm TypeMap) ForceGenerate(name string, forceGenerate bool) { + modelCfg := tm[name] + modelCfg.ForceGenerate = forceGenerate + tm[name] = modelCfg +} + type DirectiveConfig struct { SkipRuntime bool `yaml:"skip_runtime"` } @@ -590,6 +604,11 @@ func (c *Config) autobind() error { continue } + if c.Models[t.Name].ForceGenerate { + delete(c.Models, t.Name) + continue + } + for i, p := range ps { if p == nil || p.Module == nil { return fmt.Errorf("unable to load %s - make sure you're using an import path to a package that exists", c.AutoBind[i]) diff --git a/docs/content/config.md b/docs/content/config.md index 547c62b8d81..65e5e6fd56e 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -132,12 +132,13 @@ To start using them you first need to define them: directive @goModel( model: String models: [String!] + forceGenerate: Boolean ) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION directive @goField( forceResolver: Boolean name: String - omittable: Boolean + omittable: Boolean ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION directive @goTag(