github.com/HaswinVidanage/gqlgen@v0.8.1-0.20220609041233-69528c1bf712/example/scalars/scalar_test.go (about)

     1  package scalars
     2  
     3  import (
     4  	"net/http/httptest"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/HaswinVidanage/gqlgen/client"
     9  	"github.com/HaswinVidanage/gqlgen/graphql/introspection"
    10  	"github.com/HaswinVidanage/gqlgen/handler"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  type RawUser struct {
    15  	ID                string
    16  	Name              string
    17  	Created           int64
    18  	Address           struct{ Location string }
    19  	PrimitiveResolver string
    20  	CustomResolver    string
    21  	Tier              string
    22  }
    23  
    24  func TestScalars(t *testing.T) {
    25  	srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{}})))
    26  	c := client.New(srv.URL)
    27  
    28  	t.Run("marshaling", func(t *testing.T) {
    29  		var resp struct {
    30  			User   RawUser
    31  			Search []RawUser
    32  		}
    33  		c.MustPost(`{
    34  				user(id:"=1=") {
    35  					...UserData
    36  				}
    37  				search(input:{location:"6,66", createdAfter:666}) {
    38  					...UserData
    39  				}
    40  			}
    41  			fragment UserData on User  { id name created tier address { location } }`, &resp)
    42  
    43  		require.Equal(t, "1,2", resp.User.Address.Location)
    44  		// There can be a delay between creation and test assertion, so we
    45  		// give some leeway to eliminate false positives.
    46  		require.WithinDuration(t, time.Now(), time.Unix(resp.User.Created, 0), 5*time.Second)
    47  		require.Equal(t, "6,66", resp.Search[0].Address.Location)
    48  		require.Equal(t, int64(666), resp.Search[0].Created)
    49  		require.Equal(t, "A", resp.Search[0].Tier)
    50  	})
    51  
    52  	t.Run("default search location", func(t *testing.T) {
    53  		var resp struct{ Search []RawUser }
    54  
    55  		err := c.Post(`{ search {  address { location }  } }`, &resp)
    56  		require.NoError(t, err)
    57  		require.Equal(t, "37,144", resp.Search[0].Address.Location)
    58  	})
    59  
    60  	t.Run("custom error messages", func(t *testing.T) {
    61  		var resp struct{ Search []RawUser }
    62  
    63  		err := c.Post(`{ search(input:{createdAfter:"2014"}) { id } }`, &resp)
    64  		require.EqualError(t, err, `[{"message":"time should be a unix timestamp","path":["search"]}]`)
    65  	})
    66  
    67  	t.Run("scalar resolver methods", func(t *testing.T) {
    68  		var resp struct{ User RawUser }
    69  		c.MustPost(`{ user(id: "=1=") { primitiveResolver, customResolver } }`, &resp)
    70  
    71  		require.Equal(t, "test", resp.User.PrimitiveResolver)
    72  		require.Equal(t, "5,1", resp.User.CustomResolver)
    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  }