Skip to content

Commit

Permalink
Merge pull request 99designs#874 from 99designs/configurable-slice-el…
Browse files Browse the repository at this point in the history
…ement-pointers

Add config option to omit pointers to slice elements
  • Loading branch information
vektah authored Sep 24, 2019
2 parents 965058e + d78c3a0 commit 1168eb9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
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

0 comments on commit 1168eb9

Please sign in to comment.