Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add extraTag directive #1173

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,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 @@ -225,9 +229,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 @@ -241,17 +251,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 @@ -268,6 +289,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 @@ -88,6 +88,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 @@ -100,6 +103,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