Skip to content

Commit

Permalink
ForceGenerate parameter to @GoModel added. (#2780)
Browse files Browse the repository at this point in the history
* ForceGenerate parameter to @GoModel added.

* forceGenerate to docs added

---------

Co-authored-by: Roman A. Grigorovich <ragrigorov@mts.ru>
  • Loading branch information
atzedus and Roman A. Grigorovich authored Sep 8, 2023
1 parent 11bb9b1 commit fa47118
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
23 changes: 21 additions & 2 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,20 @@ 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{}) {
c.Models.Add(schemaType.Name, mv.(string))
}
}
}

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 {
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit fa47118

Please sign in to comment.