github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/complexity_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/operandinc/gqlgen/graphql/handler/extension"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestComplexityCollisions(t *testing.T) {
    14  	resolvers := &Stub{}
    15  
    16  	srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
    17  
    18  	c := client.New(srv)
    19  
    20  	resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
    21  		return &OverlappingFields{
    22  			Foo:    2,
    23  			NewFoo: 3,
    24  		}, nil
    25  	}
    26  
    27  	resolvers.OverlappingFieldsResolver.OldFoo = func(ctx context.Context, obj *OverlappingFields) (i int, e error) {
    28  		return obj.Foo, nil
    29  	}
    30  
    31  	var resp struct {
    32  		Overlapping struct {
    33  			OneFoo  int `json:"oneFoo"`
    34  			TwoFoo  int `json:"twoFoo"`
    35  			OldFoo  int `json:"oldFoo"`
    36  			NewFoo  int `json:"newFoo"`
    37  			New_foo int `json:"new_foo"`
    38  		}
    39  	}
    40  	c.MustPost(`query { overlapping { oneFoo, twoFoo, oldFoo, newFoo, new_foo } }`, &resp)
    41  	require.Equal(t, 2, resp.Overlapping.OneFoo)
    42  	require.Equal(t, 2, resp.Overlapping.TwoFoo)
    43  	require.Equal(t, 2, resp.Overlapping.OldFoo)
    44  	require.Equal(t, 3, resp.Overlapping.NewFoo)
    45  	require.Equal(t, 3, resp.Overlapping.New_foo)
    46  }
    47  
    48  func TestComplexityFuncs(t *testing.T) {
    49  	resolvers := &Stub{}
    50  	cfg := Config{Resolvers: resolvers}
    51  	cfg.Complexity.OverlappingFields.Foo = func(childComplexity int) int { return 1000 }
    52  	cfg.Complexity.OverlappingFields.NewFoo = func(childComplexity int) int { return 5 }
    53  
    54  	srv := handler.NewDefaultServer(NewExecutableSchema(cfg))
    55  	srv.Use(extension.FixedComplexityLimit(10))
    56  	c := client.New(srv)
    57  
    58  	resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
    59  		return &OverlappingFields{
    60  			Foo:    2,
    61  			NewFoo: 3,
    62  		}, nil
    63  	}
    64  
    65  	t.Run("with high complexity limit will not run", func(t *testing.T) {
    66  		ran := false
    67  		resolvers.OverlappingFieldsResolver.OldFoo = func(ctx context.Context, obj *OverlappingFields) (i int, e error) {
    68  			ran = true
    69  			return obj.Foo, nil
    70  		}
    71  
    72  		var resp struct {
    73  			Overlapping interface{}
    74  		}
    75  		err := c.Post(`query { overlapping { oneFoo, twoFoo, oldFoo, newFoo, new_foo } }`, &resp)
    76  
    77  		require.EqualError(t, err, `[{"message":"operation has complexity 2012, which exceeds the limit of 10","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}]`)
    78  		require.False(t, ran)
    79  	})
    80  
    81  	t.Run("with low complexity will run", func(t *testing.T) {
    82  		ran := false
    83  		resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
    84  			ran = true
    85  			return &OverlappingFields{
    86  				Foo:    2,
    87  				NewFoo: 3,
    88  			}, nil
    89  		}
    90  
    91  		var resp struct {
    92  			Overlapping interface{}
    93  		}
    94  		c.MustPost(`query { overlapping { newFoo } }`, &resp)
    95  
    96  		require.True(t, ran)
    97  	})
    98  
    99  	t.Run("with multiple low complexity will not run", func(t *testing.T) {
   100  		ran := false
   101  		resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
   102  			ran = true
   103  			return &OverlappingFields{
   104  				Foo:    2,
   105  				NewFoo: 3,
   106  			}, nil
   107  		}
   108  
   109  		var resp interface{}
   110  		err := c.Post(`query {
   111  			a: overlapping { newFoo },
   112  			b: overlapping { newFoo },
   113  			c: overlapping { newFoo },
   114  		}`, &resp)
   115  
   116  		require.EqualError(t, err, `[{"message":"operation has complexity 18, which exceeds the limit of 10","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}]`)
   117  		require.False(t, ran)
   118  	})
   119  }