From 635b1aef316c528b6f17416057d41a0948b42a2d Mon Sep 17 00:00:00 2001 From: Wilhelm Date: Sat, 6 Mar 2021 12:51:20 +1100 Subject: [PATCH] Add Test Case --- codegen/testserver/generated.go | 105 ++++++++++++++++++++++++++++-- codegen/testserver/nulls.graphql | 1 + codegen/testserver/nulls_test.go | 16 ++++- codegen/testserver/resolver.go | 4 ++ codegen/testserver/slices.graphql | 8 +-- codegen/testserver/stub.go | 4 ++ 6 files changed, 129 insertions(+), 9 deletions(-) diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 2aac317f20b..21876449d4f 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -278,6 +278,7 @@ type ComplexityRoot struct { EmbeddedCase3 func(childComplexity int) int EnumInInput func(childComplexity int, input *InputWithEnumValue) int ErrorBubble func(childComplexity int) int + ErrorBubbleList func(childComplexity int) int Errors func(childComplexity int) int Fallback func(childComplexity int, arg FallbackToStringEncoding) int InputNullableSlice func(childComplexity int, arg []string) int @@ -457,6 +458,7 @@ type QueryResolver interface { MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) MapNestedStringInterface(ctx context.Context, in *NestedMapInput) (map[string]interface{}, error) ErrorBubble(ctx context.Context) (*Error, error) + ErrorBubbleList(ctx context.Context) ([]*Error, error) Errors(ctx context.Context) (*Errors, error) Valid(ctx context.Context) (string, error) Panics(ctx context.Context) (*Panics, error) @@ -1199,6 +1201,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.ErrorBubble(childComplexity), true + case "Query.errorBubbleList": + if e.complexity.Query.ErrorBubbleList == nil { + break + } + + return e.complexity.Query.ErrorBubbleList(childComplexity), true + case "Query.errors": if e.complexity.Query.Errors == nil { break @@ -2039,6 +2048,7 @@ input NestedInput { `, BuiltIn: false}, {Name: "nulls.graphql", Input: `extend type Query { errorBubble: Error + errorBubbleList: [Error!] errors: Errors valid: String! } @@ -2204,10 +2214,10 @@ scalar Time } type Slices { - test1: [String] - test2: [String!] - test3: [String]! - test4: [String!]! + test1: [String] + test2: [String!] + test3: [String]! + test4: [String!]! } scalar Bytes @@ -7212,6 +7222,35 @@ func (ec *executionContext) _Query_errorBubble(ctx context.Context, field graphq return ec.marshalOError2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐError(ctx, field.Selections, res) } +func (ec *executionContext) _Query_errorBubbleList(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "Query", + Field: field, + Args: nil, + IsMethod: true, + IsResolver: true, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().ErrorBubbleList(rctx) + }) + + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]*Error) + fc.Result = res + return ec.marshalOError2ᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐErrorᚄ(ctx, field.Selections, res) +} + func (ec *executionContext) _Query_errors(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -12266,6 +12305,17 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr res = ec._Query_errorBubble(ctx, field) return res }) + case "errorBubbleList": + field := field + out.Concurrently(i, func() (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_errorBubbleList(ctx, field) + return res + }) case "errors": field := field out.Concurrently(i, func() (res graphql.Marshaler) { @@ -14216,6 +14266,53 @@ func (ec *executionContext) marshalOEmbeddedCase32ᚖgithubᚗcomᚋ99designsᚋ return ec._EmbeddedCase3(ctx, sel, v) } +func (ec *executionContext) marshalOError2ᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐErrorᚄ(ctx context.Context, sel ast.SelectionSet, v []*Error) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNError2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐError(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + func (ec *executionContext) marshalOError2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐError(ctx context.Context, sel ast.SelectionSet, v *Error) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/codegen/testserver/nulls.graphql b/codegen/testserver/nulls.graphql index d93f6b0cf62..0f1af3dd30d 100644 --- a/codegen/testserver/nulls.graphql +++ b/codegen/testserver/nulls.graphql @@ -1,5 +1,6 @@ extend type Query { errorBubble: Error + errorBubbleList: [Error!] errors: Errors valid: String! } diff --git a/codegen/testserver/nulls_test.go b/codegen/testserver/nulls_test.go index e79eecc283a..5bf2112ae6d 100644 --- a/codegen/testserver/nulls_test.go +++ b/codegen/testserver/nulls_test.go @@ -14,13 +14,15 @@ func TestNullBubbling(t *testing.T) { resolvers.QueryResolver.Valid = func(ctx context.Context) (s string, e error) { return "Ok", nil } - resolvers.QueryResolver.Errors = func(ctx context.Context) (errors *Errors, e error) { return &Errors{}, nil } resolvers.QueryResolver.ErrorBubble = func(ctx context.Context) (i *Error, e error) { return &Error{ID: "E1234"}, nil } + resolvers.QueryResolver.ErrorBubbleList = func(ctx context.Context) (i []*Error, e error) { + return []*Error{nil}, nil + } c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))) @@ -68,6 +70,18 @@ func TestNullBubbling(t *testing.T) { require.Equal(t, "Ok", resp.Valid) }) + t.Run("when non-null list element is null", func(t *testing.T) { + var resp struct { + Valid string + ErrorBubbleList []*struct{} + } + err := c.Post(`query { valid, errorBubbleList { id } }`, &resp) + + require.EqualError(t, err, `[{"message":"must not be null","path":["errorBubbleList", 0]}]`) + require.Nil(t, resp.ErrorBubbleList) + require.Equal(t, "Ok", resp.Valid) + }) + t.Run("null args", func(t *testing.T) { var resp struct { NullableArg *string diff --git a/codegen/testserver/resolver.go b/codegen/testserver/resolver.go index 952d20943e8..edc4001f51b 100644 --- a/codegen/testserver/resolver.go +++ b/codegen/testserver/resolver.go @@ -232,6 +232,10 @@ func (r *queryResolver) ErrorBubble(ctx context.Context) (*Error, error) { panic("not implemented") } +func (r *queryResolver) ErrorBubbleList(ctx context.Context) ([]*Error, error) { + panic("not implemented") +} + func (r *queryResolver) Errors(ctx context.Context) (*Errors, error) { panic("not implemented") } diff --git a/codegen/testserver/slices.graphql b/codegen/testserver/slices.graphql index 671235d0b37..b1265c56c0b 100644 --- a/codegen/testserver/slices.graphql +++ b/codegen/testserver/slices.graphql @@ -4,10 +4,10 @@ extend type Query { } type Slices { - test1: [String] - test2: [String!] - test3: [String]! - test4: [String!]! + test1: [String] + test2: [String!] + test3: [String]! + test4: [String!]! } scalar Bytes diff --git a/codegen/testserver/stub.go b/codegen/testserver/stub.go index daf4052ea08..5ca93fd8046 100644 --- a/codegen/testserver/stub.go +++ b/codegen/testserver/stub.go @@ -85,6 +85,7 @@ type Stub struct { MapStringInterface func(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) MapNestedStringInterface func(ctx context.Context, in *NestedMapInput) (map[string]interface{}, error) ErrorBubble func(ctx context.Context) (*Error, error) + ErrorBubbleList func(ctx context.Context) ([]*Error, error) Errors func(ctx context.Context) (*Errors, error) Valid func(ctx context.Context) (string, error) Panics func(ctx context.Context) (*Panics, error) @@ -358,6 +359,9 @@ func (r *stubQuery) MapNestedStringInterface(ctx context.Context, in *NestedMapI func (r *stubQuery) ErrorBubble(ctx context.Context) (*Error, error) { return r.QueryResolver.ErrorBubble(ctx) } +func (r *stubQuery) ErrorBubbleList(ctx context.Context) ([]*Error, error) { + return r.QueryResolver.ErrorBubbleList(ctx) +} func (r *stubQuery) Errors(ctx context.Context) (*Errors, error) { return r.QueryResolver.Errors(ctx) }