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

Make gqlgen StructFieldsAlwaysPointers configurable #216

Merged
merged 3 commits into from
Apr 24, 2024
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
7 changes: 6 additions & 1 deletion clientgenv2/source_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,14 @@ func (r *SourceGenerator) NewResponseField(selection ast.Selection, typeName str
nil,
)

var typ types.Type = baseType
if r.cfg.StructFieldsAlwaysPointers {
typ = types.NewPointer(baseType)
}

return &ResponseField{
Name: selection.Name,
Type: types.NewPointer(baseType),
Type: typ,
IsFragmentSpread: true,
ResponseFields: fieldsResponseFields,
}
Expand Down
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ func LoadConfig(filename string) (*Config, error) {
sources = append(sources, &ast.Source{Name: filename, Input: string(schemaRaw)})
}

structFieldsAlwaysPointers := true
if cfg.Generate != nil && cfg.Generate.StructFieldsAlwaysPointers != nil {
structFieldsAlwaysPointers = *cfg.Generate.StructFieldsAlwaysPointers
}

cfg.GQLConfig = &config.Config{
Model: cfg.Model,
Models: models,
Expand All @@ -209,7 +214,7 @@ func LoadConfig(filename string) (*Config, error) {
Exec: config.ExecConfig{Filename: "generated.go"},
Directives: map[string]config.DirectiveConfig{},
Sources: sources,
StructFieldsAlwaysPointers: true,
StructFieldsAlwaysPointers: structFieldsAlwaysPointers,
ReturnPointersInUmarshalInput: false,
ResolversAlwaysReturnPointers: true,
NullableInputOmittable: false,
Expand Down
3 changes: 2 additions & 1 deletion config/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type GenerateConfig struct {
OmitEmptyTypes *bool `yaml:"omitEmptyTypes,omitempty"`
// Deprecated: not working because v1 is deleted. Must use ClientV2
// if true, used client v2 in generate code
ClientV2 bool `yaml:"clientV2,omitempty"`
ClientV2 bool `yaml:"clientV2,omitempty"`
StructFieldsAlwaysPointers *bool `yaml:"structFieldsAlwaysPointers,omitempty"`
}

func (c *GenerateConfig) ShouldGenerateClient() bool {
Expand Down