github.com/kerryoscer/gqlgen@v0.17.29/codegen/config/binder_test.go (about) 1 package config 2 3 import ( 4 "go/types" 5 "testing" 6 7 "github.com/kerryoscer/gqlgen/internal/code" 8 9 "github.com/stretchr/testify/require" 10 "github.com/vektah/gqlparser/v2/ast" 11 ) 12 13 func TestBindingToInvalid(t *testing.T) { 14 binder, schema := createBinder(Config{}) 15 _, err := binder.TypeReference(schema.Query.Fields.ForName("messages").Type, &types.Basic{}) 16 require.EqualError(t, err, "Message has an invalid type") 17 } 18 19 func TestSlicePointerBinding(t *testing.T) { 20 t.Run("without OmitSliceElementPointers", func(t *testing.T) { 21 binder, schema := createBinder(Config{ 22 OmitSliceElementPointers: false, 23 }) 24 25 ta, err := binder.TypeReference(schema.Query.Fields.ForName("messages").Type, nil) 26 if err != nil { 27 panic(err) 28 } 29 30 require.Equal(t, ta.GO.String(), "[]*github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.Message") 31 }) 32 33 t.Run("with OmitSliceElementPointers", func(t *testing.T) { 34 binder, schema := createBinder(Config{ 35 OmitSliceElementPointers: true, 36 }) 37 38 ta, err := binder.TypeReference(schema.Query.Fields.ForName("messages").Type, nil) 39 if err != nil { 40 panic(err) 41 } 42 43 require.Equal(t, ta.GO.String(), "[]github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.Message") 44 }) 45 } 46 47 func createBinder(cfg Config) (*Binder, *ast.Schema) { 48 cfg.Models = TypeMap{ 49 "Message": TypeMapEntry{ 50 Model: []string{"github.com/kerryoscer/gqlgen/codegen/config/testdata/autobinding/chat.Message"}, 51 }, 52 } 53 cfg.Packages = &code.Packages{} 54 55 cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: ` 56 type Message { id: ID } 57 58 type Query { 59 messages: [Message!]! 60 } 61 `}) 62 63 b := cfg.NewBinder() 64 65 return b, cfg.Schema 66 }