...

Source file src/github.com/99designs/gqlgen/codegen/testserver/followschema/middleware_test.go

Documentation: github.com/99designs/gqlgen/codegen/testserver/followschema

     1  package followschema
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/99designs/gqlgen/client"
    12  	"github.com/99designs/gqlgen/graphql"
    13  	"github.com/99designs/gqlgen/graphql/handler"
    14  )
    15  
    16  func TestMiddleware(t *testing.T) {
    17  	resolvers := &Stub{}
    18  	resolvers.QueryResolver.ErrorBubble = func(ctx context.Context) (i *Error, e error) {
    19  		return &Error{ID: "E1234"}, nil
    20  	}
    21  
    22  	resolvers.QueryResolver.User = func(ctx context.Context, id int) (user *User, e error) {
    23  		return &User{ID: 1}, nil
    24  	}
    25  
    26  	resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) (users []*User, e error) {
    27  		return []*User{{ID: 1}}, nil
    28  	}
    29  
    30  	resolvers.QueryResolver.ModelMethods = func(ctx context.Context) (methods *ModelMethods, e error) {
    31  		return &ModelMethods{}, nil
    32  	}
    33  
    34  	var mu sync.Mutex
    35  	areMethods := map[string]bool{}
    36  	areResolvers := map[string]bool{}
    37  	srv := handler.NewDefaultServer(
    38  		NewExecutableSchema(Config{Resolvers: resolvers}),
    39  	)
    40  	srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
    41  		path, _ := ctx.Value(ckey("path")).([]int)
    42  		return next(context.WithValue(ctx, ckey("path"), append(path, 1)))
    43  	})
    44  
    45  	srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
    46  		path, _ := ctx.Value(ckey("path")).([]int)
    47  		return next(context.WithValue(ctx, ckey("path"), append(path, 2)))
    48  	})
    49  
    50  	srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
    51  		fc := graphql.GetFieldContext(ctx)
    52  		mu.Lock()
    53  		areMethods[fc.Field.Name] = fc.IsMethod
    54  		areResolvers[fc.Field.Name] = fc.IsResolver
    55  		mu.Unlock()
    56  		return next(ctx)
    57  	})
    58  
    59  	c := client.New(srv)
    60  
    61  	var resp struct {
    62  		User struct {
    63  			ID      int
    64  			Friends []struct {
    65  				ID int
    66  			}
    67  		}
    68  		ModelMethods struct {
    69  			NoContext bool
    70  		}
    71  	}
    72  
    73  	called := false
    74  	resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]*User, error) {
    75  		assert.Equal(t, []int{1, 2, 1, 2}, ctx.Value(ckey("path")))
    76  		called = true
    77  		return []*User{}, nil
    78  	}
    79  
    80  	err := c.Post(`query {
    81  		user(id: 1) {
    82  			id,
    83  			friends {
    84  				id
    85  			}
    86  		}
    87  		modelMethods {
    88  			noContext
    89  		}
    90  	}`, &resp)
    91  
    92  	assert.Equal(t, map[string]bool{
    93  		"user":         true,
    94  		"id":           false,
    95  		"friends":      true,
    96  		"modelMethods": true,
    97  		"noContext":    true,
    98  	}, areMethods)
    99  	assert.Equal(t, map[string]bool{
   100  		"user":         true,
   101  		"id":           false,
   102  		"friends":      true,
   103  		"modelMethods": true,
   104  		"noContext":    false,
   105  	}, areResolvers)
   106  
   107  	require.NoError(t, err)
   108  	require.True(t, called)
   109  }
   110  

View as plain text