github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/primitive_objects_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/assert"
    10  )
    11  
    12  func TestPrimitiveObjects(t *testing.T) {
    13  	resolvers := &Stub{}
    14  	resolvers.QueryResolver.PrimitiveObject = func(ctx context.Context) (out []Primitive, e error) {
    15  		return []Primitive{2, 4}, nil
    16  	}
    17  
    18  	resolvers.PrimitiveResolver.Value = func(ctx context.Context, obj *Primitive) (i int, e error) {
    19  		return int(*obj), nil
    20  	}
    21  
    22  	c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
    23  
    24  	t.Run("can fetch value", func(t *testing.T) {
    25  		var resp struct {
    26  			PrimitiveObject []struct {
    27  				Value   int
    28  				Squared int
    29  			}
    30  		}
    31  		c.MustPost(`query { primitiveObject { value, squared } }`, &resp)
    32  
    33  		assert.Equal(t, 2, resp.PrimitiveObject[0].Value)
    34  		assert.Equal(t, 4, resp.PrimitiveObject[0].Squared)
    35  		assert.Equal(t, 4, resp.PrimitiveObject[1].Value)
    36  		assert.Equal(t, 16, resp.PrimitiveObject[1].Squared)
    37  	})
    38  }
    39  
    40  func TestPrimitiveStringObjects(t *testing.T) {
    41  	resolvers := &Stub{}
    42  	resolvers.QueryResolver.PrimitiveStringObject = func(ctx context.Context) (out []PrimitiveString, e error) {
    43  		return []PrimitiveString{"hello", "world"}, nil
    44  	}
    45  
    46  	resolvers.PrimitiveStringResolver.Value = func(ctx context.Context, obj *PrimitiveString) (i string, e error) {
    47  		return string(*obj), nil
    48  	}
    49  
    50  	resolvers.PrimitiveStringResolver.Len = func(ctx context.Context, obj *PrimitiveString) (i int, e error) {
    51  		return len(string(*obj)), nil
    52  	}
    53  
    54  	c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
    55  
    56  	t.Run("can fetch value", func(t *testing.T) {
    57  		var resp struct {
    58  			PrimitiveStringObject []struct {
    59  				Value   string
    60  				Doubled string
    61  				Len     int
    62  			}
    63  		}
    64  		c.MustPost(`query { primitiveStringObject { value, doubled, len } }`, &resp)
    65  
    66  		assert.Equal(t, "hello", resp.PrimitiveStringObject[0].Value)
    67  		assert.Equal(t, "hellohello", resp.PrimitiveStringObject[0].Doubled)
    68  		assert.Equal(t, 5, resp.PrimitiveStringObject[0].Len)
    69  		assert.Equal(t, "world", resp.PrimitiveStringObject[1].Value)
    70  		assert.Equal(t, "worldworld", resp.PrimitiveStringObject[1].Doubled)
    71  		assert.Equal(t, 5, resp.PrimitiveStringObject[1].Len)
    72  	})
    73  }