github.com/matiasanaya/gqlgen@v0.6.0/example/dataloader/dataloader_test.go (about)

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