From b65bca7f1851f01e256c3026d5f600815591cdfe Mon Sep 17 00:00:00 2001 From: Kacper Sawicki Date: Fri, 16 Jun 2023 14:39:46 +0200 Subject: [PATCH] COSMOS-3252: Nexus annotation nexus-graphql-ts-type not working as expected for array of customType (#201) * Support graphql array types * Don't add @jsonencoded if field don't have type-name annotation * Revert "Don't add @jsonencoded if field don't have type-name annotation" This reverts commit f76d400490acc688f1923897293ed354ef7e1c23. * Fix Makefile --- compiler/Makefile | 3 +- compiler/example/datamodel/config/gns/gns.go | 1 + .../apis/gns.tsm.tanzu.vmware.com/v1/types.go | 1 + .../output/_rendered_templates/model/model.go | 3 + .../nexus-client/client.go | 21 +++++++ .../nexus-gql/graph/graphqlResolver.go | 6 ++ .../nexus-gql/graph/schema.graphqls | 1 + .../tsm-nexus-gql/graph/schema.graphqls | 5 +- .../apis/gns.tsm.tanzu.vmware.com/v1/types.go | 1 + .../v1/zz_generated.deepcopy.go | 6 ++ .../output/generated/crds/gns_gns.yaml | 7 +++ .../example/output/generated/model/model.go | 3 + .../output/generated/nexus-client/client.go | 21 +++++++ .../nexus-gql/graph/generated/generated.go | 56 +++++++++++++++++++ .../nexus-gql/graph/graphqlResolver.go | 6 ++ .../nexus-gql/graph/model/models_gen.go | 1 + .../generated/nexus-gql/graph/schema.graphqls | 1 + .../tsm-nexus-gql/graph/schema.graphqls | 5 +- compiler/pkg/generator/graphql_common.go | 23 +++++++- compiler/pkg/parser/pkg_test.go | 2 +- 20 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 compiler/example/output/_rendered_templates/model/model.go create mode 100644 compiler/example/output/generated/model/model.go diff --git a/compiler/Makefile b/compiler/Makefile index 7821aee34f..1ee5f81904 100644 --- a/compiler/Makefile +++ b/compiler/Makefile @@ -232,7 +232,8 @@ test_generate_code_in_container: ${BUILDER_NAME}\:${BUILDER_TAG}.image.exists in CRD_MODULE_PATH="github.com/vmware-tanzu/graph-framework-for-microservices/compiler/example/output/generated/" \ LOG_LEVEL=trace \ GENERATED_OUTPUT_DIRECTORY=example/output/generated && \ - cd example/output/generated && go vet -structtag=FALSE ./... && golangci-lint run ./...) + cd example/output/generated && [ ! -f model/model.go ] || mv model/model.go model/model.tmp && \ + go vet -structtag=FALSE ./... && golangci-lint run ./... && [ ! -f model/model.tmp ] || mv model/model.tmp model/model.go) @if [ -n "$$(git ls-files --modified --exclude-standard)" ]; then\ echo "The following changes should be committed:";\ git status;\ diff --git a/compiler/example/datamodel/config/gns/gns.go b/compiler/example/datamodel/config/gns/gns.go index 9701841387..12b38ca0a4 100644 --- a/compiler/example/datamodel/config/gns/gns.go +++ b/compiler/example/datamodel/config/gns/gns.go @@ -183,6 +183,7 @@ type Gns struct { IgnoreChild IgnoreChild `nexus:"child" nexus-graphql:"ignore:true"` Foo Foo `nexus:"child"` Meta string + IntOrString []intstr.IntOrString `nexus-graphql-type-name:"IntOrString" json:"intOrString,omitempty" mapstructure:"intOrString,omitempty"` Port *int // pointer test OtherDescription *Description // pointer test - struct diff --git a/compiler/example/output/_rendered_templates/apis/gns.tsm.tanzu.vmware.com/v1/types.go b/compiler/example/output/_rendered_templates/apis/gns.tsm.tanzu.vmware.com/v1/types.go index 6ce43ba227..79cb5d431c 100644 --- a/compiler/example/output/_rendered_templates/apis/gns.tsm.tanzu.vmware.com/v1/types.go +++ b/compiler/example/output/_rendered_templates/apis/gns.tsm.tanzu.vmware.com/v1/types.go @@ -119,6 +119,7 @@ type GnsSpec struct { TargetPort intstr.IntOrString `json:"targetPort,omitempty" mapstructure:"targetPort,omitempty"` Description Description `json:"description" yaml:"description"` Meta string `json:"meta" yaml:"meta"` + IntOrString []intstr.IntOrString `nexus-graphql-type-name:"IntOrString" json:"intOrString,omitempty" mapstructure:"intOrString,omitempty"` Port *int `json:"port" yaml:"port"` OtherDescription *Description `json:"otherDescription" yaml:"otherDescription"` MapPointer *map[string]string `json:"mapPointer" yaml:"mapPointer"` diff --git a/compiler/example/output/_rendered_templates/model/model.go b/compiler/example/output/_rendered_templates/model/model.go new file mode 100644 index 0000000000..cbb7347546 --- /dev/null +++ b/compiler/example/output/_rendered_templates/model/model.go @@ -0,0 +1,3 @@ +type IntOrString []intstr.IntOrString + + diff --git a/compiler/example/output/_rendered_templates/nexus-client/client.go b/compiler/example/output/_rendered_templates/nexus-client/client.go index e699352013..a2eac08c2e 100644 --- a/compiler/example/output/_rendered_templates/nexus-client/client.go +++ b/compiler/example/output/_rendered_templates/nexus-client/client.go @@ -6919,6 +6919,27 @@ func (group *GnsTsmV1) UpdateGnsByName(ctx context.Context, patch = append(patch, patchOpMeta) } + rt = reflect.TypeOf(objToUpdate.Spec.IntOrString) + if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array || rt.Kind() == reflect.Map { + if !reflect.ValueOf(objToUpdate.Spec.IntOrString).IsNil() { + patchValueIntOrString := objToUpdate.Spec.IntOrString + patchOpIntOrString := PatchOp{ + Op: "replace", + Path: "/spec/intOrString", + Value: patchValueIntOrString, + } + patch = append(patch, patchOpIntOrString) + } + } else { + patchValueIntOrString := objToUpdate.Spec.IntOrString + patchOpIntOrString := PatchOp{ + Op: "replace", + Path: "/spec/intOrString", + Value: patchValueIntOrString, + } + patch = append(patch, patchOpIntOrString) + } + rt = reflect.TypeOf(objToUpdate.Spec.Port) if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array || rt.Kind() == reflect.Map { if !reflect.ValueOf(objToUpdate.Spec.Port).IsNil() { diff --git a/compiler/example/output/_rendered_templates/nexus-gql/graph/graphqlResolver.go b/compiler/example/output/_rendered_templates/nexus-gql/graph/graphqlResolver.go index 52cc731a92..77aaed5a5a 100644 --- a/compiler/example/output/_rendered_templates/nexus-gql/graph/graphqlResolver.go +++ b/compiler/example/output/_rendered_templates/nexus-gql/graph/graphqlResolver.go @@ -410,6 +410,8 @@ TargetPortData := string(TargetPort) Description, _ := json.Marshal(vGns.Spec.Description) DescriptionData := string(Description) vMeta := string(vGns.Spec.Meta) +IntOrString, _ := json.Marshal(vGns.Spec.IntOrString) +IntOrStringData := string(IntOrString) OtherDescription, _ := json.Marshal(vGns.Spec.OtherDescription) OtherDescriptionData := string(OtherDescription) MapPointer, _ := json.Marshal(vGns.Spec.MapPointer) @@ -441,6 +443,7 @@ ServiceSegmentRefMapData := string(ServiceSegmentRefMap) TargetPort: &TargetPortData, Description: &DescriptionData, Meta: &vMeta, + IntOrString: &IntOrStringData, OtherDescription: &OtherDescriptionData, MapPointer: &MapPointerData, SlicePointer: &SlicePointerData, @@ -477,6 +480,8 @@ TargetPortData := string(TargetPort) Description, _ := json.Marshal(vGns.Spec.Description) DescriptionData := string(Description) vMeta := string(vGns.Spec.Meta) +IntOrString, _ := json.Marshal(vGns.Spec.IntOrString) +IntOrStringData := string(IntOrString) OtherDescription, _ := json.Marshal(vGns.Spec.OtherDescription) OtherDescriptionData := string(OtherDescription) MapPointer, _ := json.Marshal(vGns.Spec.MapPointer) @@ -508,6 +513,7 @@ ServiceSegmentRefMapData := string(ServiceSegmentRefMap) TargetPort: &TargetPortData, Description: &DescriptionData, Meta: &vMeta, + IntOrString: &IntOrStringData, OtherDescription: &OtherDescriptionData, MapPointer: &MapPointerData, SlicePointer: &SlicePointerData, diff --git a/compiler/example/output/_rendered_templates/nexus-gql/graph/schema.graphqls b/compiler/example/output/_rendered_templates/nexus-gql/graph/schema.graphqls index 1dd0fc09ed..2d988edea2 100644 --- a/compiler/example/output/_rendered_templates/nexus-gql/graph/schema.graphqls +++ b/compiler/example/output/_rendered_templates/nexus-gql/graph/schema.graphqls @@ -86,6 +86,7 @@ type gns_Gns { TargetPort: String Description: String Meta: String + IntOrString: String Port: Int OtherDescription: String MapPointer: String diff --git a/compiler/example/output/_rendered_templates/tsm-nexus-gql/graph/schema.graphqls b/compiler/example/output/_rendered_templates/tsm-nexus-gql/graph/schema.graphqls index 3a7e615caa..edd776691a 100644 --- a/compiler/example/output/_rendered_templates/tsm-nexus-gql/graph/schema.graphqls +++ b/compiler/example/output/_rendered_templates/tsm-nexus-gql/graph/schema.graphqls @@ -22,10 +22,10 @@ type Config_Config @nexus(group:"config.tsm.tanzu.vmware.com",version:"v1",kind: fooExample(id: ID): [Config_FooTypeABC!] svcGrpInfo: Servicegroup_SvcGroupLinkInfo myStr0: String - myStr1: String + myStr1: String @jsonencoded(gofile:"model.go", name:"MyStr", goname:"nexus_gns.MyStr") myStr2: String xYZPort: String @jsonencoded(gofile:"model.go", name:"Port", goname:"nexus_gns.Port") - aBCHost: String + aBCHost: String @jsonencoded(gofile:"model.go", name:"Host", goname:"nexus_gns.Host") clusterNamespaces: String testValMarkers: String @jsonencoded(gofile:"model.go", name:"TestValMarkers", goname:"nexus_config.TestValMarkers") instance: Float @@ -79,6 +79,7 @@ type Gns_Gns @nexus(group:"gns.tsm.tanzu.vmware.com",version:"v1",kind:"Gns",res targetPort: String @jsonencoded(gofile:"model.go", name:"IntOrString") description: String @jsonencoded(gofile:"model.go", name:"Description", goname:"nexus_gns.Description") meta: String + intOrString: String @jsonencoded(gofile:"model.go", name:"IntOrString") port: Int otherDescription: String mapPointer: String diff --git a/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/types.go b/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/types.go index 8a10066ce2..836b949706 100644 --- a/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/types.go +++ b/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/types.go @@ -119,6 +119,7 @@ type GnsSpec struct { TargetPort intstr.IntOrString `json:"targetPort,omitempty" mapstructure:"targetPort,omitempty"` Description Description `json:"description" yaml:"description"` Meta string `json:"meta" yaml:"meta"` + IntOrString []intstr.IntOrString `nexus-graphql-type-name:"IntOrString" json:"intOrString,omitempty" mapstructure:"intOrString,omitempty"` Port *int `json:"port" yaml:"port"` OtherDescription *Description `json:"otherDescription" yaml:"otherDescription"` MapPointer *map[string]string `json:"mapPointer" yaml:"mapPointer"` diff --git a/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/zz_generated.deepcopy.go b/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/zz_generated.deepcopy.go index 5326f3323c..965656163c 100644 --- a/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/zz_generated.deepcopy.go +++ b/compiler/example/output/generated/apis/gns.tsm.tanzu.vmware.com/v1/zz_generated.deepcopy.go @@ -24,6 +24,7 @@ package v1 import ( v1alpha1 "github.com/vmware-tanzu/cartographer/pkg/apis/v1alpha1" runtime "k8s.io/apimachinery/pkg/runtime" + intstr "k8s.io/apimachinery/pkg/util/intstr" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -482,6 +483,11 @@ func (in *GnsSpec) DeepCopyInto(out *GnsSpec) { out.Annotations = in.Annotations out.TargetPort = in.TargetPort in.Description.DeepCopyInto(&out.Description) + if in.IntOrString != nil { + in, out := &in.IntOrString, &out.IntOrString + *out = make([]intstr.IntOrString, len(*in)) + copy(*out, *in) + } if in.Port != nil { in, out := &in.Port, &out.Port *out = new(int) diff --git a/compiler/example/output/generated/crds/gns_gns.yaml b/compiler/example/output/generated/crds/gns_gns.yaml index ee94c2a563..0a716d5b93 100644 --- a/compiler/example/output/generated/crds/gns_gns.yaml +++ b/compiler/example/output/generated/crds/gns_gns.yaml @@ -167,6 +167,13 @@ spec: - kind - name type: object + intOrString: + items: + anyOf: + - type: integer + - type: string + x-kubernetes-int-or-string: true + type: array mapPointer: additionalProperties: type: string diff --git a/compiler/example/output/generated/model/model.go b/compiler/example/output/generated/model/model.go new file mode 100644 index 0000000000..cbb7347546 --- /dev/null +++ b/compiler/example/output/generated/model/model.go @@ -0,0 +1,3 @@ +type IntOrString []intstr.IntOrString + + diff --git a/compiler/example/output/generated/nexus-client/client.go b/compiler/example/output/generated/nexus-client/client.go index 1fb13a6ba2..f7fd8df79e 100644 --- a/compiler/example/output/generated/nexus-client/client.go +++ b/compiler/example/output/generated/nexus-client/client.go @@ -6919,6 +6919,27 @@ func (group *GnsTsmV1) UpdateGnsByName(ctx context.Context, patch = append(patch, patchOpMeta) } + rt = reflect.TypeOf(objToUpdate.Spec.IntOrString) + if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array || rt.Kind() == reflect.Map { + if !reflect.ValueOf(objToUpdate.Spec.IntOrString).IsNil() { + patchValueIntOrString := objToUpdate.Spec.IntOrString + patchOpIntOrString := PatchOp{ + Op: "replace", + Path: "/spec/intOrString", + Value: patchValueIntOrString, + } + patch = append(patch, patchOpIntOrString) + } + } else { + patchValueIntOrString := objToUpdate.Spec.IntOrString + patchOpIntOrString := PatchOp{ + Op: "replace", + Path: "/spec/intOrString", + Value: patchValueIntOrString, + } + patch = append(patch, patchOpIntOrString) + } + rt = reflect.TypeOf(objToUpdate.Spec.Port) if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array || rt.Kind() == reflect.Map { if !reflect.ValueOf(objToUpdate.Spec.Port).IsNil() { diff --git a/compiler/example/output/generated/nexus-gql/graph/generated/generated.go b/compiler/example/output/generated/nexus-gql/graph/generated/generated.go index 3cf63c6cbd..4d94db9747 100644 --- a/compiler/example/output/generated/nexus-gql/graph/generated/generated.go +++ b/compiler/example/output/generated/nexus-gql/graph/generated/generated.go @@ -136,6 +136,7 @@ type ComplexityRoot struct { FooChild func(childComplexity int) int GnsAccessControlPolicy func(childComplexity int, id *string) int Id func(childComplexity int) int + IntOrString func(childComplexity int) int MapPointer func(childComplexity int) int Meta func(childComplexity int) int OtherDescription func(childComplexity int) int @@ -703,6 +704,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Gns_Gns.Id(childComplexity), true + case "gns_Gns.IntOrString": + if e.complexity.Gns_Gns.IntOrString == nil { + break + } + + return e.complexity.Gns_Gns.IntOrString(childComplexity), true + case "gns_Gns.MapPointer": if e.complexity.Gns_Gns.MapPointer == nil { break @@ -1181,6 +1189,7 @@ type gns_Gns { TargetPort: String Description: String Meta: String + IntOrString: String Port: Int OtherDescription: String MapPointer: String @@ -4934,6 +4943,8 @@ func (ec *executionContext) fieldContext_config_Config_GNS(ctx context.Context, return ec.fieldContext_gns_Gns_Description(ctx, field) case "Meta": return ec.fieldContext_gns_Gns_Meta(ctx, field) + case "IntOrString": + return ec.fieldContext_gns_Gns_IntOrString(ctx, field) case "Port": return ec.fieldContext_gns_Gns_Port(ctx, field) case "OtherDescription": @@ -6652,6 +6663,47 @@ func (ec *executionContext) fieldContext_gns_Gns_Meta(ctx context.Context, field return fc, nil } +func (ec *executionContext) _gns_Gns_IntOrString(ctx context.Context, field graphql.CollectedField, obj *model.GnsGns) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_gns_Gns_IntOrString(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.IntOrString, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*string) + fc.Result = res + return ec.marshalOString2áš–string(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_gns_Gns_IntOrString(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "gns_Gns", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type String does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _gns_Gns_Port(ctx context.Context, field graphql.CollectedField, obj *model.GnsGns) (ret graphql.Marshaler) { fc, err := ec.fieldContext_gns_Gns_Port(ctx, field) if err != nil { @@ -9417,6 +9469,10 @@ func (ec *executionContext) _gns_Gns(ctx context.Context, sel ast.SelectionSet, out.Values[i] = ec._gns_Gns_Meta(ctx, field, obj) + case "IntOrString": + + out.Values[i] = ec._gns_Gns_IntOrString(ctx, field, obj) + case "Port": out.Values[i] = ec._gns_Gns_Port(ctx, field, obj) diff --git a/compiler/example/output/generated/nexus-gql/graph/graphqlResolver.go b/compiler/example/output/generated/nexus-gql/graph/graphqlResolver.go index 5082429483..5a6bee2cbc 100644 --- a/compiler/example/output/generated/nexus-gql/graph/graphqlResolver.go +++ b/compiler/example/output/generated/nexus-gql/graph/graphqlResolver.go @@ -423,6 +423,8 @@ func getConfigConfigGNSResolver(obj *model.ConfigConfig, id *string) (*model.Gns Description, _ := json.Marshal(vGns.Spec.Description) DescriptionData := string(Description) vMeta := string(vGns.Spec.Meta) + IntOrString, _ := json.Marshal(vGns.Spec.IntOrString) + IntOrStringData := string(IntOrString) OtherDescription, _ := json.Marshal(vGns.Spec.OtherDescription) OtherDescriptionData := string(OtherDescription) MapPointer, _ := json.Marshal(vGns.Spec.MapPointer) @@ -454,6 +456,7 @@ func getConfigConfigGNSResolver(obj *model.ConfigConfig, id *string) (*model.Gns TargetPort: &TargetPortData, Description: &DescriptionData, Meta: &vMeta, + IntOrString: &IntOrStringData, OtherDescription: &OtherDescriptionData, MapPointer: &MapPointerData, SlicePointer: &SlicePointerData, @@ -490,6 +493,8 @@ func getConfigConfigGNSResolver(obj *model.ConfigConfig, id *string) (*model.Gns Description, _ := json.Marshal(vGns.Spec.Description) DescriptionData := string(Description) vMeta := string(vGns.Spec.Meta) + IntOrString, _ := json.Marshal(vGns.Spec.IntOrString) + IntOrStringData := string(IntOrString) OtherDescription, _ := json.Marshal(vGns.Spec.OtherDescription) OtherDescriptionData := string(OtherDescription) MapPointer, _ := json.Marshal(vGns.Spec.MapPointer) @@ -521,6 +526,7 @@ func getConfigConfigGNSResolver(obj *model.ConfigConfig, id *string) (*model.Gns TargetPort: &TargetPortData, Description: &DescriptionData, Meta: &vMeta, + IntOrString: &IntOrStringData, OtherDescription: &OtherDescriptionData, MapPointer: &MapPointerData, SlicePointer: &SlicePointerData, diff --git a/compiler/example/output/generated/nexus-gql/graph/model/models_gen.go b/compiler/example/output/generated/nexus-gql/graph/model/models_gen.go index cf9500650b..7367915d42 100644 --- a/compiler/example/output/generated/nexus-gql/graph/model/models_gen.go +++ b/compiler/example/output/generated/nexus-gql/graph/model/models_gen.go @@ -86,6 +86,7 @@ type GnsGns struct { TargetPort *string `json:"TargetPort"` Description *string `json:"Description"` Meta *string `json:"Meta"` + IntOrString *string `json:"IntOrString"` Port *int `json:"Port"` OtherDescription *string `json:"OtherDescription"` MapPointer *string `json:"MapPointer"` diff --git a/compiler/example/output/generated/nexus-gql/graph/schema.graphqls b/compiler/example/output/generated/nexus-gql/graph/schema.graphqls index 1dd0fc09ed..2d988edea2 100644 --- a/compiler/example/output/generated/nexus-gql/graph/schema.graphqls +++ b/compiler/example/output/generated/nexus-gql/graph/schema.graphqls @@ -86,6 +86,7 @@ type gns_Gns { TargetPort: String Description: String Meta: String + IntOrString: String Port: Int OtherDescription: String MapPointer: String diff --git a/compiler/example/output/generated/tsm-nexus-gql/graph/schema.graphqls b/compiler/example/output/generated/tsm-nexus-gql/graph/schema.graphqls index 3a7e615caa..edd776691a 100644 --- a/compiler/example/output/generated/tsm-nexus-gql/graph/schema.graphqls +++ b/compiler/example/output/generated/tsm-nexus-gql/graph/schema.graphqls @@ -22,10 +22,10 @@ type Config_Config @nexus(group:"config.tsm.tanzu.vmware.com",version:"v1",kind: fooExample(id: ID): [Config_FooTypeABC!] svcGrpInfo: Servicegroup_SvcGroupLinkInfo myStr0: String - myStr1: String + myStr1: String @jsonencoded(gofile:"model.go", name:"MyStr", goname:"nexus_gns.MyStr") myStr2: String xYZPort: String @jsonencoded(gofile:"model.go", name:"Port", goname:"nexus_gns.Port") - aBCHost: String + aBCHost: String @jsonencoded(gofile:"model.go", name:"Host", goname:"nexus_gns.Host") clusterNamespaces: String testValMarkers: String @jsonencoded(gofile:"model.go", name:"TestValMarkers", goname:"nexus_config.TestValMarkers") instance: Float @@ -79,6 +79,7 @@ type Gns_Gns @nexus(group:"gns.tsm.tanzu.vmware.com",version:"v1",kind:"Gns",res targetPort: String @jsonencoded(gofile:"model.go", name:"IntOrString") description: String @jsonencoded(gofile:"model.go", name:"Description", goname:"nexus_gns.Description") meta: String + intOrString: String @jsonencoded(gofile:"model.go", name:"IntOrString") port: Int otherDescription: String mapPointer: String diff --git a/compiler/pkg/generator/graphql_common.go b/compiler/pkg/generator/graphql_common.go index caa13345a1..2032d0d5e9 100644 --- a/compiler/pkg/generator/graphql_common.go +++ b/compiler/pkg/generator/graphql_common.go @@ -401,7 +401,7 @@ func addFieldAnnotations(pkg parser.Package, f *ast.Field, schemaName string, sT typeName := parser.GetFieldAnnotationVal(f, parser.GRAPHQL_TYPE_NAME) externalType := fmt.Sprintf("%s.%s", x, val.Sel.Name) aliasType := fmt.Sprintf("type %s %s", typeName, externalType) - if !slices.Contains(nonNexusTypes.ExternalTypes, aliasType) { + if nonNexusTypes != nil && !slices.Contains(nonNexusTypes.ExternalTypes, aliasType) { nonNexusTypes.ExternalTypes = append(nonNexusTypes.ExternalTypes, aliasType) } schemaName = addJsonencodedAnnotation(f, parser.GRAPHQL_TS_TYPE_ANNOTATION, x, typeName, schemaName, true, imp) @@ -410,6 +410,27 @@ func addFieldAnnotations(pkg parser.Package, f *ast.Field, schemaName string, sT } } } + } else if arrayExpr, ok := f.Type.(*ast.ArrayType); ok { + if val, ok := arrayExpr.Elt.(*ast.SelectorExpr); ok { + x := types.ExprString(val.X) + if imp, ok := importMap[x]; ok { + if strings.HasPrefix(imp, fmt.Sprintf(`"%s`, pkg.ModPath)) { + schemaName = addJsonencodedAnnotation(f, parser.GRAPHQL_TS_TYPE_ANNOTATION, x, val.Sel.Name, schemaName, false, imp) + } else { + if parser.IsFieldAnnotationPresent(f, parser.GRAPHQL_TYPE_NAME) { + typeName := parser.GetFieldAnnotationVal(f, parser.GRAPHQL_TYPE_NAME) + externalType := fmt.Sprintf("%s.%s", x, val.Sel.Name) + aliasType := fmt.Sprintf("type %s []%s", typeName, externalType) + if nonNexusTypes != nil && !slices.Contains(nonNexusTypes.ExternalTypes, aliasType) { + nonNexusTypes.ExternalTypes = append(nonNexusTypes.ExternalTypes, aliasType) + } + schemaName = addJsonencodedAnnotation(f, parser.GRAPHQL_TS_TYPE_ANNOTATION, x, typeName, schemaName, true, imp) + } else { + schemaName = addJsonencodedAnnotation(f, parser.GRAPHQL_TS_TYPE_ANNOTATION, x, val.Sel.Name, schemaName, true, imp) + } + } + } + } } else if val, ok := f.Type.(*ast.Ident); ok && convertGraphqlStdType(val.Name) == "" { x := "" importExpr := "" diff --git a/compiler/pkg/parser/pkg_test.go b/compiler/pkg/parser/pkg_test.go index b912a07e5e..d54881778a 100644 --- a/compiler/pkg/parser/pkg_test.go +++ b/compiler/pkg/parser/pkg_test.go @@ -102,7 +102,7 @@ var _ = Describe("Pkg tests", func() { It("should get spec fields for gns", func() { nodes := gnsPkg.GetNexusNodes() specFields := parser.GetSpecFields(nodes[1]) - Expect(specFields).To(HaveLen(16)) + Expect(specFields).To(HaveLen(17)) }) It("should get field name", func() {