...

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

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

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

View as plain text