github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/maps_test.go (about)

     1  package followschema
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/operandinc/gqlgen/client"
     8  	"github.com/operandinc/gqlgen/graphql/handler"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestMaps(t *testing.T) {
    13  	resolver := &Stub{}
    14  	resolver.QueryResolver.MapStringInterface = func(ctx context.Context, in map[string]interface{}) (i map[string]interface{}, e error) {
    15  		return in, nil
    16  	}
    17  	resolver.QueryResolver.MapNestedStringInterface = func(ctx context.Context, in *NestedMapInput) (i map[string]interface{}, e error) {
    18  		if in == nil {
    19  			return nil, nil
    20  		}
    21  		return in.Map, nil
    22  	}
    23  
    24  	c := client.New(handler.NewDefaultServer(
    25  		NewExecutableSchema(Config{Resolvers: resolver}),
    26  	))
    27  	t.Run("unset", func(t *testing.T) {
    28  		var resp struct {
    29  			MapStringInterface map[string]interface{}
    30  		}
    31  		err := c.Post(`query { mapStringInterface { a, b } }`, &resp)
    32  		require.NoError(t, err)
    33  		require.Nil(t, resp.MapStringInterface)
    34  	})
    35  
    36  	t.Run("nil", func(t *testing.T) {
    37  		var resp struct {
    38  			MapStringInterface map[string]interface{}
    39  		}
    40  		err := c.Post(`query { mapStringInterface(in: null) { a, b } }`, &resp)
    41  		require.NoError(t, err)
    42  		require.Nil(t, resp.MapStringInterface)
    43  	})
    44  
    45  	t.Run("values", func(t *testing.T) {
    46  		var resp struct {
    47  			MapStringInterface map[string]interface{}
    48  		}
    49  		err := c.Post(`query { mapStringInterface(in: { a: "a", b: null }) { a, b } }`, &resp)
    50  		require.NoError(t, err)
    51  		require.Equal(t, "a", resp.MapStringInterface["a"])
    52  		require.Nil(t, resp.MapStringInterface["b"])
    53  	})
    54  
    55  	t.Run("nested", func(t *testing.T) {
    56  		var resp struct {
    57  			MapNestedStringInterface map[string]interface{}
    58  		}
    59  		err := c.Post(`query { mapNestedStringInterface(in: { map: { a: "a", b: null } }) { a, b } }`, &resp)
    60  		require.NoError(t, err)
    61  		require.Equal(t, "a", resp.MapNestedStringInterface["a"])
    62  		require.Nil(t, resp.MapNestedStringInterface["b"])
    63  	})
    64  
    65  	t.Run("nested nil", func(t *testing.T) {
    66  		var resp struct {
    67  			MapNestedStringInterface map[string]interface{}
    68  		}
    69  		err := c.Post(`query { mapNestedStringInterface(in: { map: null }) { a, b } }`, &resp)
    70  		require.NoError(t, err)
    71  		require.Nil(t, resp.MapNestedStringInterface)
    72  	})
    73  }