From e37359bb001464cb97b1f50f95a6f9203da2a312 Mon Sep 17 00:00:00 2001 From: Ananya Saxena Date: Sat, 17 Oct 2020 14:10:55 -0400 Subject: [PATCH] adding tests for pointer to slice --- codegen/testserver/generated.go | 153 ++++++++++++++++++++++++ codegen/testserver/ptr_to_slice.go | 5 + codegen/testserver/ptr_to_slice.graphql | 7 ++ codegen/testserver/ptr_to_slice_test.go | 39 ++++++ codegen/testserver/resolver.go | 4 + codegen/testserver/stub.go | 4 + 6 files changed, 212 insertions(+) create mode 100644 codegen/testserver/ptr_to_slice.go create mode 100644 codegen/testserver/ptr_to_slice.graphql create mode 100644 codegen/testserver/ptr_to_slice_test.go diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index ea3c2f4854..9403c581ca 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -256,6 +256,10 @@ type ComplexityRoot struct { Value func(childComplexity int) int } + PtrToSliceContainer struct { + PtrToSlice func(childComplexity int) int + } + Query struct { Animal func(childComplexity int) int Autobind func(childComplexity int) int @@ -300,6 +304,7 @@ type ComplexityRoot struct { Panics func(childComplexity int) int PrimitiveObject func(childComplexity int) int PrimitiveStringObject func(childComplexity int) int + PtrToSliceContainer func(childComplexity int) int Recursive func(childComplexity int, input *RecursiveInputSlice) int ScalarSlice func(childComplexity int) int ShapeUnion func(childComplexity int) int @@ -462,6 +467,7 @@ type QueryResolver interface { Panics(ctx context.Context) (*Panics, error) PrimitiveObject(ctx context.Context) ([]Primitive, error) PrimitiveStringObject(ctx context.Context) ([]PrimitiveString, error) + PtrToSliceContainer(ctx context.Context) (*PtrToSliceContainer, error) DefaultScalar(ctx context.Context, arg string) (string, error) Slices(ctx context.Context) (*Slices, error) ScalarSlice(ctx context.Context) ([]byte, error) @@ -1012,6 +1018,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.PrimitiveString.Value(childComplexity), true + case "PtrToSliceContainer.ptrToSlice": + if e.complexity.PtrToSliceContainer.PtrToSlice == nil { + break + } + + return e.complexity.PtrToSliceContainer.PtrToSlice(childComplexity), true + case "Query.animal": if e.complexity.Query.Animal == nil { break @@ -1393,6 +1406,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.PrimitiveStringObject(childComplexity), true + case "Query.ptrToSliceContainer": + if e.complexity.Query.PtrToSliceContainer == nil { + break + } + + return e.complexity.Query.PtrToSliceContainer(childComplexity), true + case "Query.recursive": if e.complexity.Query.Recursive == nil { break @@ -2086,6 +2106,14 @@ type PrimitiveString { doubled: String! len: Int! } +`, BuiltIn: false}, + {Name: "ptr_to_slice.graphql", Input: `type PtrToSliceContainer { + ptrToSlice: [String!] +} + +extend type Query { + ptrToSliceContainer: PtrToSliceContainer! +} `, BuiltIn: false}, {Name: "scalar_default.graphql", Input: `extend type Query { defaultScalar(arg: DefaultScalarImplementation! = "default"): DefaultScalarImplementation! @@ -5738,6 +5766,35 @@ func (ec *executionContext) _PrimitiveString_len(ctx context.Context, field grap return ec.marshalNInt2int(ctx, field.Selections, res) } +func (ec *executionContext) _PtrToSliceContainer_ptrToSlice(ctx context.Context, field graphql.CollectedField, obj *PtrToSliceContainer) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "PtrToSliceContainer", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp := ec._fieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.PtrToSlice, nil + }) + + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*[]string) + fc.Result = res + return ec.marshalOString2ᚖᚕstringᚄ(ctx, field.Selections, res) +} + func (ec *executionContext) _Query_invalidIdentifier(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -7366,6 +7423,38 @@ func (ec *executionContext) _Query_primitiveStringObject(ctx context.Context, fi return ec.marshalNPrimitiveString2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐPrimitiveStringᚄ(ctx, field.Selections, res) } +func (ec *executionContext) _Query_ptrToSliceContainer(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().PtrToSliceContainer(rctx) + }) + + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*PtrToSliceContainer) + fc.Result = res + return ec.marshalNPtrToSliceContainer2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐPtrToSliceContainer(ctx, field.Selections, res) +} + func (ec *executionContext) _Query_defaultScalar(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -11787,6 +11876,30 @@ func (ec *executionContext) _PrimitiveString(ctx context.Context, sel ast.Select return out } +var ptrToSliceContainerImplementors = []string{"PtrToSliceContainer"} + +func (ec *executionContext) _PtrToSliceContainer(ctx context.Context, sel ast.SelectionSet, obj *PtrToSliceContainer) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, ptrToSliceContainerImplementors) + + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("PtrToSliceContainer") + case "ptrToSlice": + out.Values[i] = ec._PtrToSliceContainer_ptrToSlice(ctx, field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var queryImplementors = []string{"Query"} func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) graphql.Marshaler { @@ -12330,6 +12443,20 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr } return res }) + case "ptrToSliceContainer": + 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_ptrToSliceContainer(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + }) case "defaultScalar": field := field out.Concurrently(i, func() (res graphql.Marshaler) { @@ -13489,6 +13616,20 @@ func (ec *executionContext) marshalNPrimitiveString2ᚕgithubᚗcomᚋ99designs return ret } +func (ec *executionContext) marshalNPtrToSliceContainer2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐPtrToSliceContainer(ctx context.Context, sel ast.SelectionSet, v PtrToSliceContainer) graphql.Marshaler { + return ec._PtrToSliceContainer(ctx, sel, &v) +} + +func (ec *executionContext) marshalNPtrToSliceContainer2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐPtrToSliceContainer(ctx context.Context, sel ast.SelectionSet, v *PtrToSliceContainer) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._PtrToSliceContainer(ctx, sel, v) +} + func (ec *executionContext) unmarshalNRecursiveInputSlice2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSlice(ctx context.Context, v interface{}) (RecursiveInputSlice, error) { res, err := ec.unmarshalInputRecursiveInputSlice(ctx, v) return res, graphql.ErrorOnPath(ctx, err) @@ -14605,6 +14746,18 @@ func (ec *executionContext) marshalOString2ᚖstring(ctx context.Context, sel as return graphql.MarshalString(*v) } +func (ec *executionContext) unmarshalOString2ᚖᚕstringᚄ(ctx context.Context, v interface{}) (*[]string, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalOString2ᚕstringᚄ(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalOString2ᚖᚕstringᚄ(ctx context.Context, sel ast.SelectionSet, v *[]string) graphql.Marshaler { + return ec.marshalOString2ᚕstringᚄ(ctx, sel, *v) +} + func (ec *executionContext) marshalOTestUnion2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐTestUnion(ctx context.Context, sel ast.SelectionSet, v TestUnion) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/codegen/testserver/ptr_to_slice.go b/codegen/testserver/ptr_to_slice.go new file mode 100644 index 0000000000..b16bf48890 --- /dev/null +++ b/codegen/testserver/ptr_to_slice.go @@ -0,0 +1,5 @@ +package testserver + +type PtrToSliceContainer struct { + PtrToSlice *[]string +} diff --git a/codegen/testserver/ptr_to_slice.graphql b/codegen/testserver/ptr_to_slice.graphql new file mode 100644 index 0000000000..b773d83d42 --- /dev/null +++ b/codegen/testserver/ptr_to_slice.graphql @@ -0,0 +1,7 @@ +type PtrToSliceContainer { + ptrToSlice: [String!] +} + +extend type Query { + ptrToSliceContainer: PtrToSliceContainer! +} diff --git a/codegen/testserver/ptr_to_slice_test.go b/codegen/testserver/ptr_to_slice_test.go new file mode 100644 index 0000000000..8119748582 --- /dev/null +++ b/codegen/testserver/ptr_to_slice_test.go @@ -0,0 +1,39 @@ +package testserver + +import ( + "context" + "testing" + + "github.com/99designs/gqlgen/client" + "github.com/99designs/gqlgen/graphql/handler" + "github.com/stretchr/testify/require" +) + +func TestPtrToSlice(t *testing.T) { + resolvers := &Stub{} + + c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))) + + + resolvers.QueryResolver.PtrToSliceContainer = func(ctx context.Context) (wrappedStruct *PtrToSliceContainer, e error) { + ptrToSliceContainer := PtrToSliceContainer{ + PtrToSlice: &[]string{"hello"}, + } + return &ptrToSliceContainer, nil + } + + + t.Run("pointer to slice", func(t *testing.T) { + var resp struct { + PtrToSliceContainer struct { + PtrToSlice []string + } + } + + err := c.Post(`query { ptrToSliceContainer { ptrToSlice }}`, &resp) + require.NoError(t, err) + + require.Equal(t, []string{"hello"}, resp.PtrToSliceContainer.PtrToSlice) + + }) +} diff --git a/codegen/testserver/resolver.go b/codegen/testserver/resolver.go index 952d20943e..74edf81af5 100644 --- a/codegen/testserver/resolver.go +++ b/codegen/testserver/resolver.go @@ -252,6 +252,10 @@ func (r *queryResolver) PrimitiveStringObject(ctx context.Context) ([]PrimitiveS panic("not implemented") } +func (r *queryResolver) PtrToSliceContainer(ctx context.Context) (*PtrToSliceContainer, error) { + panic("not implemented") +} + func (r *queryResolver) DefaultScalar(ctx context.Context, arg string) (string, error) { panic("not implemented") } diff --git a/codegen/testserver/stub.go b/codegen/testserver/stub.go index daf4052ea0..d8bd15da26 100644 --- a/codegen/testserver/stub.go +++ b/codegen/testserver/stub.go @@ -90,6 +90,7 @@ type Stub struct { Panics func(ctx context.Context) (*Panics, error) PrimitiveObject func(ctx context.Context) ([]Primitive, error) PrimitiveStringObject func(ctx context.Context) ([]PrimitiveString, error) + PtrToSliceContainer func(ctx context.Context) (*PtrToSliceContainer, error) DefaultScalar func(ctx context.Context, arg string) (string, error) Slices func(ctx context.Context) (*Slices, error) ScalarSlice func(ctx context.Context) ([]byte, error) @@ -373,6 +374,9 @@ func (r *stubQuery) PrimitiveObject(ctx context.Context) ([]Primitive, error) { func (r *stubQuery) PrimitiveStringObject(ctx context.Context) ([]PrimitiveString, error) { return r.QueryResolver.PrimitiveStringObject(ctx) } +func (r *stubQuery) PtrToSliceContainer(ctx context.Context) (*PtrToSliceContainer, error) { + return r.QueryResolver.PtrToSliceContainer(ctx) +} func (r *stubQuery) DefaultScalar(ctx context.Context, arg string) (string, error) { return r.QueryResolver.DefaultScalar(ctx, arg) }