Skip to content

Commit

Permalink
Rename @extraTag directive to @goTag and make repeatable (#1680)
Browse files Browse the repository at this point in the history
* Allow Repeatable `goTag` Directive

* Default to field name if none provided

* Update Docs
  • Loading branch information
wilhelmeek committed Oct 30, 2021
1 parent 87f9e43 commit 37a4e7e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
32 changes: 21 additions & 11 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (c *Config) injectTypesFromSchema() error {
SkipRuntime: true,
}

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

Expand All @@ -266,7 +266,7 @@ 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,
ExtraTags: c.Models[schemaType.Name].Fields[field.Name].ExtraTags,
FieldName: c.Models[schemaType.Name].Fields[field.Name].FieldName,
Resolver: c.Models[schemaType.Name].Fields[field.Name].Resolver,
}
Expand All @@ -292,12 +292,22 @@ func (c *Config) injectTypesFromSchema() error {
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+`"`)
for _, goTag := range field.Directives.ForNames("goTag") {
key := ""
value := field.Name
if arg := goTag.Arguments.ForName("key"); arg != nil {
if k, err := arg.Value.Value(nil); err == nil {
key = k.(string)
}
}
typeMapField.ExtraTag = strings.Join(args, " ")

if arg := goTag.Arguments.ForName("value"); arg != nil {
if v, err := arg.Value.Value(nil); err == nil {
value = v.(string)
}
}

typeMapField.ExtraTags = append(typeMapField.ExtraTags, key+":\""+value+"\"")
directive = true
}

Expand All @@ -323,10 +333,10 @@ type TypeMapEntry struct {
}

type TypeMapField struct {
Resolver bool `yaml:"resolver"`
FieldName string `yaml:"fieldName"`
ExtraTag string `yaml:"extraTag"`
GeneratedMethod string `yaml:"-"`
Resolver bool `yaml:"resolver"`
FieldName string `yaml:"fieldName"`
ExtraTags []string `yaml:"extraTags"`
GeneratedMethod string `yaml:"-"`
}

type StringList []string
Expand Down
34 changes: 19 additions & 15 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ models:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32

```

Everything has defaults, so add things as you need.
Expand All @@ -84,18 +83,20 @@ gqlgen ships with some builtin directives that make it a little easier to manage
To start using them you first need to define them:

```graphql
directive @goModel(model: String, models: [String!]) on OBJECT
| INPUT_OBJECT
| SCALAR
| ENUM
| INTERFACE
| UNION

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

directive @extraTag on INPUT_FIELD_DEFINITION
| FIELD_DEFINITION
directive @goModel(
model: String
models: [String!]
) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION

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

directive @goTag(
key: String!
value: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
```

> Here be dragons
Expand All @@ -107,7 +108,10 @@ 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) @extraTag(xorm: "-")
id: ID! @goField(name: "todoId")
name: String!
@goField(forceResolver: true)
@goTag(key: "xorm", value: "-")
@goTag(key: "yaml")
}
```
10 changes: 5 additions & 5 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/types"
"sort"
"strings"

"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
Expand All @@ -18,6 +19,7 @@ type FieldMutateHook = func(td *ast.Definition, fd *ast.FieldDefinition, f *Fiel
func defaultFieldMutateHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) {
return f, nil
}

func defaultBuildMutateHook(b *ModelBuild) *ModelBuild {
return b
}
Expand Down Expand Up @@ -169,16 +171,14 @@ 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
}
tags := []string{`json:"` + field.Name + `"`}
tags = append(tags, cfg.Models[schemaType.Name].Fields[field.Name].ExtraTags...)

f := &Field{
Name: name,
Type: typ,
Description: field.Description,
Tag: tag,
Tag: strings.Join(tags, " "),
}

if m.FieldHook != nil {
Expand Down

0 comments on commit 37a4e7e

Please sign in to comment.