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 config option to omit pointers to slice elements #874

Merged
merged 1 commit into from
Sep 24, 2019
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
2 changes: 1 addition & 1 deletion codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
func (b *Binder) CopyModifiersFromAst(t *ast.Type, base types.Type) types.Type {
if t.Elem != nil {
child := b.CopyModifiersFromAst(t.Elem, base)
if _, isStruct := child.Underlying().(*types.Struct); isStruct {
if _, isStruct := child.Underlying().(*types.Struct); isStruct && !b.cfg.OmitSliceElementPointers {
child = types.NewPointer(child)
}
return types.NewSlice(child)
Expand Down
61 changes: 61 additions & 0 deletions codegen/config/binder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
)

func TestSlicePointerBinding(t *testing.T) {
t.Run("without OmitSliceElementPointers", func(t *testing.T) {
binder, schema := createBinder(Config{
OmitSliceElementPointers: false,
})

ta, err := binder.TypeReference(schema.Query.Fields.ForName("messages").Type, nil)
if err != nil {
panic(err)
}

require.Equal(t, ta.GO.String(), "[]*github.com/99designs/gqlgen/example/chat.Message")
})

t.Run("with OmitSliceElementPointers", func(t *testing.T) {
binder, schema := createBinder(Config{
OmitSliceElementPointers: true,
})

ta, err := binder.TypeReference(schema.Query.Fields.ForName("messages").Type, nil)
if err != nil {
panic(err)
}

require.Equal(t, ta.GO.String(), "[]github.com/99designs/gqlgen/example/chat.Message")
})
}

func createBinder(cfg Config) (*Binder, *ast.Schema) {
cfg.Models = TypeMap{
"Message": TypeMapEntry{
Model: []string{"github.com/99designs/gqlgen/example/chat.Message"},
},
}

s := gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
type Message { id: ID }

type Query {
messages: [Message!]!
}
`})

b, err := cfg.NewBinder(s)
if err != nil {
panic(err)
}

return b, s
}
17 changes: 9 additions & 8 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
)

type Config struct {
SchemaFilename StringList `yaml:"schema,omitempty"`
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model"`
Resolver PackageConfig `yaml:"resolver,omitempty"`
AutoBind []string `yaml:"autobind"`
Models TypeMap `yaml:"models,omitempty"`
StructTag string `yaml:"struct_tag,omitempty"`
Directives map[string]DirectiveConfig `yaml:"directives,omitempty"`
SchemaFilename StringList `yaml:"schema,omitempty"`
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model"`
Resolver PackageConfig `yaml:"resolver,omitempty"`
AutoBind []string `yaml:"autobind"`
Models TypeMap `yaml:"models,omitempty"`
StructTag string `yaml:"struct_tag,omitempty"`
Directives map[string]DirectiveConfig `yaml:"directives,omitempty"`
OmitSliceElementPointers bool `yaml:"omit_slice_element_pointers,omitempty"`
}

var cfgFilenames = []string{".gqlgen.yml", "gqlgen.yml", "gqlgen.yaml"}
Expand Down
3 changes: 3 additions & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ resolver:
# Optional, turns on binding to field names by tag provided
struct_tag: json

# Optional, set to true if you prefer []*Thing over []Thing
omit_slice_element_pointers: false

# Instead of listing out every model like below, you can automatically bind to any matching types
# within the given path by using `model: User` or `model: models.User`. EXPERIMENTAL in v0.9.1
autobind:
Expand Down