Skip to content

Commit

Permalink
add extraTag directive (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 authored Oct 11, 2021
1 parent fd1bd7c commit 393f755
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
36 changes: 29 additions & 7 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func (c *Config) injectTypesFromSchema() error {
SkipRuntime: true,
}

c.Directives["extraTag"] = DirectiveConfig{
SkipRuntime: true,
}

for _, schemaType := range c.Schema.Types {
if schemaType == c.Schema.Query || schemaType == c.Schema.Mutation || schemaType == c.Schema.Subscription {
continue
Expand All @@ -253,9 +257,15 @@ func (c *Config) injectTypesFromSchema() error {

if schemaType.Kind == ast.Object || schemaType.Kind == ast.InputObject {
for _, field := range schemaType.Fields {
typeMapField := TypeMapField{
ExtraTag: c.Models[schemaType.Name].Fields[field.Name].ExtraTag,
FieldName: c.Models[schemaType.Name].Fields[field.Name].FieldName,
Resolver: c.Models[schemaType.Name].Fields[field.Name].Resolver,
}
directive := false
if fd := field.Directives.ForName("goField"); fd != nil {
forceResolver := c.Models[schemaType.Name].Fields[field.Name].Resolver
fieldName := c.Models[schemaType.Name].Fields[field.Name].FieldName
forceResolver := typeMapField.Resolver
fieldName := typeMapField.FieldName

if ra := fd.Arguments.ForName("forceResolver"); ra != nil {
if fr, err := ra.Value.Value(nil); err == nil {
Expand All @@ -269,17 +279,28 @@ func (c *Config) injectTypesFromSchema() error {
}
}

typeMapField.FieldName = fieldName
typeMapField.Resolver = forceResolver
directive = true
}

if ex := field.Directives.ForName("extraTag"); ex != nil {
args := []string{}
for _, arg := range ex.Arguments {
args = append(args, arg.Name+`:"`+arg.Value.Raw+`"`)
}
typeMapField.ExtraTag = strings.Join(args, " ")
directive = true
}

if directive {
if c.Models[schemaType.Name].Fields == nil {
c.Models[schemaType.Name] = TypeMapEntry{
Model: c.Models[schemaType.Name].Model,
Fields: map[string]TypeMapField{},
}
}

c.Models[schemaType.Name].Fields[field.Name] = TypeMapField{
FieldName: fieldName,
Resolver: forceResolver,
}
c.Models[schemaType.Name].Fields[field.Name] = typeMapField
}
}
}
Expand All @@ -296,6 +317,7 @@ type TypeMapEntry struct {
type TypeMapField struct {
Resolver bool `yaml:"resolver"`
FieldName string `yaml:"fieldName"`
ExtraTag string `yaml:"extraTag"`
GeneratedMethod string `yaml:"-"`
}

Expand Down
5 changes: 4 additions & 1 deletion docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ directive @goModel(model: String, models: [String!]) on OBJECT

directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION
| FIELD_DEFINITION

directive @extraTag on INPUT_FIELD_DEFINITION
| FIELD_DEFINITION
```

> Here be dragons
Expand All @@ -101,6 +104,6 @@ Now you can use these directives when defining types in your schema:
```graphql
type User @goModel(model: "github.com/my/app/models.User") {
id: ID! @goField(name: "todoId")
name: String! @goField(forceResolver: true)
name: String! @goField(forceResolver: true) @extraTag(xorm: "-")
}
```
7 changes: 6 additions & 1 deletion plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,16 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
typ = types.NewPointer(typ)
}

tag := `json:"` + field.Name + `"`
if extraTag := cfg.Models[schemaType.Name].Fields[field.Name].ExtraTag; extraTag != "" {
tag = tag + " " + extraTag
}

it.Fields = append(it.Fields, &Field{
Name: name,
Type: typ,
Description: field.Description,
Tag: `json:"` + field.Name + `"`,
Tag: tag,
})
}

Expand Down

0 comments on commit 393f755

Please sign in to comment.