diff --git a/TESTING.md b/TESTING.md index ad7e63352ac..72ba06056c6 100644 --- a/TESTING.md +++ b/TESTING.md @@ -5,7 +5,7 @@ Testing generated code is a little tricky, heres how its currently set up. ### Testing responses from a server -There is a server in `codegen/testserver` that is generated as part +There is a server in `codegen/testserver` that is generated as part of `go generate ./...`, and tests written against it. There are also a bunch of tests in against the examples, feel free to take examples from there. @@ -15,7 +15,7 @@ There are also a bunch of tests in against the examples, feel free to take examp These tests are **really** slow, because they need to run the whole codegen step. Use them very sparingly. If you can, find a way to unit test it instead. -Take a look at `codegen/input_test.go` for an example. +Take a look at `codegen/testserver/input_test.go` for an example. ### Testing introspection diff --git a/codegen/config/binder.go b/codegen/config/binder.go index 2be7b7bdd6b..6de7ae117a3 100644 --- a/codegen/config/binder.go +++ b/codegen/config/binder.go @@ -217,6 +217,14 @@ func (t *TypeReference) IsSlice() bool { return t.GQL.Elem != nil && isSlice } +func (t *TypeReference) IsPtrToSlice() bool { + if t.IsPtr() { + _, isPointerToSlice := t.GO.(*types.Pointer).Elem().(*types.Slice) + return isPointerToSlice + } + return false +} + func (t *TypeReference) IsNamed() bool { _, isSlice := t.GO.(*types.Named) return isSlice diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 3064d382784..c39877b90f1 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 @@ -302,6 +306,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 @@ -476,6 +481,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) @@ -1028,6 +1034,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 @@ -1423,6 +1436,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 @@ -2146,6 +2166,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! @@ -5811,6 +5839,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 { @@ -7497,6 +7554,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 { @@ -12042,6 +12131,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 { @@ -12607,6 +12720,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) { @@ -13856,6 +13983,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) @@ -15118,6 +15259,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 00000000000..b16bf48890f --- /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 00000000000..b773d83d428 --- /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 00000000000..2fe498feb8c --- /dev/null +++ b/codegen/testserver/ptr_to_slice_test.go @@ -0,0 +1,37 @@ +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 867f692c1dd..ed813bf511e 100644 --- a/codegen/testserver/resolver.go +++ b/codegen/testserver/resolver.go @@ -260,6 +260,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 051f1919534..8b6d7671a26 100644 --- a/codegen/testserver/stub.go +++ b/codegen/testserver/stub.go @@ -92,6 +92,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) @@ -383,6 +384,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) } diff --git a/codegen/type.go b/codegen/type.go index 06b370be7f2..5a059d70131 100644 --- a/codegen/type.go +++ b/codegen/type.go @@ -26,7 +26,7 @@ func processType(ret map[string]*config.TypeReference, ref *config.TypeReference } ret[key] = ref - if ref.IsSlice() { + if ref.IsSlice() || ref.IsPtrToSlice() { processType(ret, ref.Elem()) } } diff --git a/codegen/type.gotpl b/codegen/type.gotpl index 85a7cf6a5f2..9c421b02d3d 100644 --- a/codegen/type.gotpl +++ b/codegen/type.gotpl @@ -4,7 +4,10 @@ {{- if and $type.IsNilable (not $type.GQL.NonNull) }} if v == nil { return nil, nil } {{- end }} - {{- if $type.IsSlice }} + {{- if $type.IsPtrToSlice }} + res, err := ec.{{ $type.Elem.UnmarshalFunc }}(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) + {{- else if $type.IsSlice }} var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -66,7 +69,9 @@ {{ with $type.MarshalFunc }} func (ec *executionContext) {{ . }}(ctx context.Context, sel ast.SelectionSet, v {{ $type.GO | ref }}) graphql.Marshaler { - {{- if $type.IsSlice }} + {{- if $type.IsPtrToSlice }} + return ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, *v) + {{- else if $type.IsSlice }} {{- if not $type.GQL.NonNull }} if v == nil { return graphql.Null