Skip to content

Commit

Permalink
Merge pull request #253 from abe-tetsu/add-config-omitempty
Browse files Browse the repository at this point in the history
Add `EnableClientJsonOmitemptyTag` to Config
  • Loading branch information
Yamashou authored Dec 18, 2024
2 parents 35a30c2 + 5081f23 commit 9193658
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ generate:
clientInterfaceName: "GithubGraphQLClient" # Determine the name of the generated client interface
structFieldsAlwaysPointers: true # Optional: Always use pointers for struct fields (default: true). [same as gqlgen](https://github.com/99designs/gqlgen/blob/e1ef86e795e738654c98553b325a248c02c8c2f8/docs/content/config.md?plain=1#L73)
onlyUsedModels: true # Optional: Only generate used models
enableClientJsonOmitemptyTag: true # Optional: Controls whether the "omitempty" option is added to JSON tags (default: true)
```
Execute the following command on same directory for .gqlgenc.yml
Expand Down
2 changes: 1 addition & 1 deletion clientgenv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (p *Plugin) Name() string {
func (p *Plugin) MutateConfig(cfg *config.Config) error {
// テンプレートと情報ソースを元にコード生成
// Generate code from template and document source
sourceGenerator := NewSourceGenerator(cfg, p.Client)
sourceGenerator := NewSourceGenerator(cfg, p.Client, p.GenerateConfig)
source := NewSource(cfg.Schema, p.queryDocument, sourceGenerator, p.GenerateConfig)

fragments, err := source.Fragments()
Expand Down
15 changes: 9 additions & 6 deletions clientgenv2/source_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
gqlgencConfig "github.com/Yamashou/gqlgenc/config"
"github.com/vektah/gqlparser/v2/ast"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -84,14 +85,16 @@ type SourceGenerator struct {
cfg *config.Config
binder *config.Binder
client config.PackageConfig
genCfg *gqlgencConfig.GenerateConfig
StructSources []*StructSource
}

func NewSourceGenerator(cfg *config.Config, client config.PackageConfig) *SourceGenerator {
func NewSourceGenerator(cfg *config.Config, client config.PackageConfig, generateConfig *gqlgencConfig.GenerateConfig) *SourceGenerator {
return &SourceGenerator{
cfg: cfg,
binder: cfg.NewBinder(),
client: client,
genCfg: generateConfig,
StructSources: []*StructSource{},
}
}
Expand All @@ -110,13 +113,13 @@ func NewLayerTypeName(base, thisField string) string {
}

func (r *SourceGenerator) NewResponseField(selection ast.Selection, typeName string) *ResponseField {
var nonNull bool
var isOptional bool
switch selection := selection.(type) {
case *ast.Field:
typeName = NewLayerTypeName(typeName, templates.ToGo(selection.Alias))
fieldsResponseFields := r.NewResponseFields(selection.SelectionSet, typeName)

nonNull = selection.Definition.Type.NonNull
isOptional = !selection.Definition.Type.NonNull

var baseType types.Type
switch {
Expand Down Expand Up @@ -147,9 +150,9 @@ func (r *SourceGenerator) NewResponseField(selection ast.Selection, typeName str
// return pointer type then optional type or slice pointer then slice type of definition in GraphQL.
typ := r.binder.CopyModifiersFromAst(selection.Definition.Type, baseType)

jsonTag := fmt.Sprintf(`json:"%s,omitempty"`, selection.Alias)
if nonNull {
jsonTag = fmt.Sprintf(`json:"%s"`, selection.Alias)
jsonTag := fmt.Sprintf(`json:"%s"`, selection.Alias)
if r.genCfg.IsEnableClientJsonOmitemptyTag() && isOptional {
jsonTag = fmt.Sprintf(`json:"%s,omitempty"`, selection.Alias)
}
tags := []string{
jsonTag,
Expand Down
25 changes: 21 additions & 4 deletions config/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ type GenerateConfig struct {
// Deprecated: not working because it is generated by gqlgen
Query *bool `yaml:"query,omitempty"`
// Deprecated: not working because it is generated by gqlgen
Mutation *bool `yaml:"mutation,omitempty"`
Client *bool `yaml:"client,omitempty"`
ClientInterfaceName *string `yaml:"clientInterfaceName,omitempty"`
OmitEmptyTypes *bool `yaml:"omitEmptyTypes,omitempty"`
Mutation *bool `yaml:"mutation,omitempty"`
Client *bool `yaml:"client,omitempty"`
ClientInterfaceName *string `yaml:"clientInterfaceName,omitempty"`
OmitEmptyTypes *bool `yaml:"omitEmptyTypes,omitempty"`
EnableClientJsonOmitemptyTag *bool `yaml:"enableClientJsonOmitemptyTag,omitempty"`

// Deprecated: not working because v1 is deleted. Must use ClientV2
// if true, used client v2 in generate code
ClientV2 bool `yaml:"clientV2,omitempty"`
Expand Down Expand Up @@ -42,6 +44,21 @@ func (c *GenerateConfig) ShouldOmitEmptyTypes() bool {
return false
}

// IsEnableClientJsonOmitemptyTag controls whether the "omitempty" option is added to JSON tags.
// If EnableClientJsonOmitemptyTag is set to false, even optional fields will not include "omitempty".
// The default value is true.
func (c *GenerateConfig) IsEnableClientJsonOmitemptyTag() bool {
if c == nil {
return true
}

if c.EnableClientJsonOmitemptyTag != nil && *c.EnableClientJsonOmitemptyTag {
return true
}

return false
}

func (c *GenerateConfig) GetClientInterfaceName() *string {
if c == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions example/skipmodel/.gqlgenc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ query:
generate:
clientV2: true
onlyUsedModels: true
enableClientJsonOmitemptyTag: false
23 changes: 22 additions & 1 deletion example/skipmodel/gen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/skipmodel/query/query.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
mutation A($input: UserInput!) {
user(input: $input) {
id
profile {
id
}
}
}
2 changes: 1 addition & 1 deletion example/skipmodel/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ input ProfileInput {

type User {
id: ID!
profile: Profile!
profile: Profile
}

type Profile {
Expand Down

0 comments on commit 9193658

Please sign in to comment.