github.com/animeshon/gqlgen@v0.13.1-0.20210304133704-3a770431bb6d/example/dataloader/dataloader_test.go (about)

     1  package dataloader
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/animeshon/gqlgen/client"
     7  	"github.com/animeshon/gqlgen/graphql/handler"
     8  	"github.com/animeshon/gqlgen/graphql/introspection"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestTodo(t *testing.T) {
    13  	c := client.New(LoaderMiddleware(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{}}))))
    14  
    15  	t.Run("create a new todo", func(t *testing.T) {
    16  		var resp interface{}
    17  		c.MustPost(`{
    18  		  customers {
    19  			name
    20  			address {
    21  			  street
    22  
    23  			}
    24  			orders {
    25  			  id
    26                amount
    27  			  items {
    28  				name
    29  			  }
    30  			}
    31  		  }
    32  		}`, &resp)
    33  	})
    34  
    35  	t.Run("2d array marshaling", func(t *testing.T) {
    36  		var resp struct {
    37  			Torture2d [][]Customer
    38  		}
    39  		c.MustPost(`{ torture2d(customerIds:[[1,2],[3,4,5]]) { id name } }`, &resp)
    40  
    41  		require.EqualValues(t, [][]Customer{
    42  			{{ID: 1, Name: "0 0"}, {ID: 2, Name: "0 1"}},
    43  			{{ID: 3, Name: "1 0"}, {ID: 4, Name: "1 1"}, {ID: 5, Name: "1 2"}},
    44  		}, resp.Torture2d)
    45  	})
    46  
    47  	// Input coercion on arrays should convert non array values into an array of the appropriate depth
    48  	// http://facebook.github.io/graphql/June2018/#sec-Type-System.List
    49  	t.Run("array coercion", func(t *testing.T) {
    50  		t.Run("1d", func(t *testing.T) {
    51  			var resp struct {
    52  				Torture1d []Customer
    53  			}
    54  			c.MustPost(`{ torture1d(customerIds: 1) { id name } }`, &resp)
    55  
    56  			require.EqualValues(t, []Customer{
    57  				{ID: 1, Name: "0"},
    58  			}, resp.Torture1d)
    59  		})
    60  
    61  		t.Run("2d", func(t *testing.T) {
    62  			var resp struct {
    63  				Torture2d [][]Customer
    64  			}
    65  			c.MustPost(`{ torture2d(customerIds: 1) { id name } }`, &resp)
    66  
    67  			require.EqualValues(t, [][]Customer{
    68  				{{ID: 1, Name: "0 0"}},
    69  			}, resp.Torture2d)
    70  		})
    71  	})
    72  
    73  	t.Run("introspection", func(t *testing.T) {
    74  		// Make sure we can run the graphiql introspection query without errors
    75  		var resp interface{}
    76  		c.MustPost(introspection.Query, &resp)
    77  	})
    78  
    79  	t.Run("customer array torture malformed array query", func(t *testing.T) {
    80  		var resp struct {
    81  			Torture [][]Customer
    82  		}
    83  		err := c.Post(`{ torture2d(customerIds:{}) { id name } }`, &resp)
    84  
    85  		require.EqualError(t, err, "[{\"message\":\"map[string]interface {} is not an int\",\"path\":[\"torture2d\",\"customerIds\",0,0]}]")
    86  	})
    87  
    88  }