github.com/humans-group/gqlgen@v0.7.2/graphql/context_test.go (about) 1 package graphql 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/vektah/gqlparser/ast" 10 ) 11 12 func TestRequestContext_GetErrors(t *testing.T) { 13 c := &RequestContext{ 14 ErrorPresenter: DefaultErrorPresenter, 15 } 16 17 ctx := context.Background() 18 19 root := &ResolverContext{ 20 Field: CollectedField{ 21 Field: &ast.Field{ 22 Alias: "foo", 23 }, 24 }, 25 } 26 ctx = WithResolverContext(ctx, root) 27 c.Error(ctx, errors.New("foo1")) 28 c.Error(ctx, errors.New("foo2")) 29 30 index := 1 31 child := &ResolverContext{ 32 Parent: root, 33 Index: &index, 34 } 35 ctx = WithResolverContext(ctx, child) 36 c.Error(ctx, errors.New("bar")) 37 38 specs := []struct { 39 Name string 40 RCtx *ResolverContext 41 Messages []string 42 }{ 43 { 44 Name: "with root ResolverContext", 45 RCtx: root, 46 Messages: []string{"foo1", "foo2"}, 47 }, 48 { 49 Name: "with child ResolverContext", 50 RCtx: child, 51 Messages: []string{"bar"}, 52 }, 53 } 54 55 for _, spec := range specs { 56 t.Run(spec.Name, func(t *testing.T) { 57 errList := c.GetErrors(spec.RCtx) 58 if assert.Equal(t, len(spec.Messages), len(errList)) { 59 for idx, err := range errList { 60 assert.Equal(t, spec.Messages[idx], err.Message) 61 } 62 } 63 }) 64 } 65 }