diff --git a/docs/genqlient.yaml b/docs/genqlient.yaml index bb221499..abed6673 100644 --- a/docs/genqlient.yaml +++ b/docs/genqlient.yaml @@ -78,14 +78,14 @@ context_type: context.Context client_getter: "github.com/you/yourpkg.GetClient" -# if set, fields with a complex type will default to having -# the "pointer: true, omitempty: true" flag +# if set, fields with a struct type will default to having +# the "pointer: true, omitempty: true" flag. # -# This can be useful for complex schema where it would be burdensome -# to manually set the flags on a large number of fields +# This can be useful for struct schema where it would be burdensome +# to manually set the flags on a large number of fields. # # Defaults to false. -use_weak_references: boolean +use_struct_references: boolean # A map from GraphQL type name to Go fully-qualified type name to override diff --git a/generate/config.go b/generate/config.go index 64a9f85f..60fb2c32 100644 --- a/generate/config.go +++ b/generate/config.go @@ -27,7 +27,7 @@ type Config struct { ContextType string `yaml:"context_type"` ClientGetter string `yaml:"client_getter"` Bindings map[string]*TypeBinding `yaml:"bindings"` - WeakReferences bool `yaml:"use_weak_references"` + StructReferences bool `yaml:"use_struct_references"` // Set to true to use features that aren't fully ready to use. // diff --git a/generate/convert.go b/generate/convert.go index 5521602a..06e904ba 100644 --- a/generate/convert.go +++ b/generate/convert.go @@ -174,7 +174,7 @@ func (g *generator) convertArguments( // names.go) and the selection-set (we use all the input type's fields, // and so on recursively). See also the `case ast.InputObject` in // convertDefinition, below. - goTyp, err := g.convertType(nil, arg.Type, false, nil, options, queryOptions) + goTyp, err := g.convertType(nil, arg.Type, nil, options, queryOptions) if err != nil { return nil, err } @@ -218,7 +218,6 @@ func (g *generator) convertArguments( func (g *generator) convertType( namePrefix *prefixList, typ *ast.Type, - isStructField bool, selectionSet ast.SelectionSet, options, queryOptions *genqlientDirective, ) (goType, error) { @@ -236,7 +235,7 @@ func (g *generator) convertType( if typ.Elem != nil { // Type is a list. elem, err := g.convertType( - namePrefix, typ.Elem, isStructField, selectionSet, options, queryOptions) + namePrefix, typ.Elem, selectionSet, options, queryOptions) return &goSliceType{elem}, err } @@ -246,12 +245,14 @@ func (g *generator) convertType( goTyp, err := g.convertDefinition( namePrefix, def, typ.Position, selectionSet, options, queryOptions) - if g.getWeakReference(options, isStructField, def) { - if options.Omitempty == nil { + if g.getStructReference(options, def) { + if options.Pointer == nil || *options.Pointer == true { + goTyp = &goPointerType{goTyp} + } + if options.Omitempty == nil || *options.Omitempty == true { oe := true options.Omitempty = &oe } - goTyp = &goPointerType{goTyp} } else if options.GetPointer() { // Whatever we get, wrap it in a pointer. (Because of the way the // options work, recursing here isn't as connvenient.) @@ -261,16 +262,13 @@ func (g *generator) convertType( return goTyp, err } -// getWeakReference decides if a field should be of pointer type and have the omitempty flag set. -func (g *generator) getWeakReference( +// getStructReference decides if a field should be of pointer type and have the omitempty flag set. +func (g *generator) getStructReference( options *genqlientDirective, - isStructField bool, def *ast.Definition, ) bool { - return options.Pointer == nil && - isStructField && - g.Config.WeakReferences && - len(def.Fields) > 0 + return g.Config.StructReferences && + (def.Kind == ast.Object || def.Kind == ast.InputObject) } // convertDefinition decides the Go type we will generate corresponding to a @@ -424,7 +422,7 @@ func (g *generator) convertDefinition( // will be ignored? We know field.Type is a scalar, enum, or input // type. But plumbing that is a bit tricky in practice. fieldGoType, err := g.convertType( - namePrefix, field.Type, true, nil, fieldOptions, queryOptions) + namePrefix, field.Type, nil, fieldOptions, queryOptions) if err != nil { return nil, err } @@ -847,7 +845,7 @@ func (g *generator) convertField( namePrefix = nextPrefix(namePrefix, field) fieldGoType, err := g.convertType( - namePrefix, field.Definition.Type, true, field.SelectionSet, + namePrefix, field.Definition.Type, field.SelectionSet, fieldOptions, queryOptions) if err != nil { return nil, err diff --git a/generate/generate_test.go b/generate/generate_test.go index 4999f229..f12c31c8 100644 --- a/generate/generate_test.go +++ b/generate/generate_test.go @@ -170,9 +170,9 @@ func TestGenerateWithConfig(t *testing.T) { Generated: "generated.go", ContextType: "github.com/Khan/genqlient/internal/testutil.MyContext", }}, - {"WeakReferences", "", &Config{ - WeakReferences: true, - Generated: "generated-weakrefs.go", + {"StructReferences", "", &Config{ + StructReferences: true, + Generated: "generated-structrefs.go", }}, {"NoContext", "", &Config{ Generated: "generated.go", diff --git a/generate/testdata/snapshots/TestGenerateWithConfig-WeakReferences-testdata-queries-generated-weakrefs.go b/generate/testdata/snapshots/TestGenerateWithConfig-StructReferences-testdata-queries-generated-structrefs.go similarity index 100% rename from generate/testdata/snapshots/TestGenerateWithConfig-WeakReferences-testdata-queries-generated-weakrefs.go rename to generate/testdata/snapshots/TestGenerateWithConfig-StructReferences-testdata-queries-generated-structrefs.go