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