diff --git a/packages/amplify-codegen-e2e-tests/schemas/modelgen/custom_type_with_model_reference.graphql b/packages/amplify-codegen-e2e-tests/schemas/modelgen/custom_type_with_model_reference.graphql new file mode 100644 index 000000000..985523738 --- /dev/null +++ b/packages/amplify-codegen-e2e-tests/schemas/modelgen/custom_type_with_model_reference.graphql @@ -0,0 +1,16 @@ +type Product @model { + id: ID! + name: String! + price: Float! +} + +type OrderSummary { + orderId: ID! + product: Product! + quantity: Int! +} + +type Mutation { + createOrder(productId: ID!, quantity: Int!): OrderSummary + @function(name: "createOrder-${env}") +} diff --git a/packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-ios.test.ts b/packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-ios.test.ts index 03fa87968..215ffc2da 100644 --- a/packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-ios.test.ts +++ b/packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-ios.test.ts @@ -3,6 +3,7 @@ import { deleteAmplifyProject, testCodegenModels } from '../codegen-tests-base'; import * as path from 'path'; const schema = 'modelgen/model_gen_schema_with_aws_scalars.graphql'; +const customTypeWithModelRefSchema = 'modelgen/custom_type_with_model_reference.graphql'; describe('Datastore Modelgen tests - iOS', () => { let projectRoot: string; @@ -22,4 +23,8 @@ describe('Datastore Modelgen tests - iOS', () => { it(`should generate files at overridden location`, async () => { await testCodegenModels(DEFAULT_IOS_CONFIG, projectRoot, schema, path.join('amplification', 'manufactured', 'models')); }); + + it(`should generate correct syntax for custom types referencing @model types`, async () => { + await testCodegenModels(DEFAULT_IOS_CONFIG, projectRoot, customTypeWithModelRefSchema); + }); }); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 307f681ac..815479466 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -1533,6 +1533,67 @@ describe('AppSyncSwiftVisitor', () => { `); }); + it('should support custom types referencing @model types', () => { + const schema = /* GraphQL */ ` + type Product @model { + id: ID! + name: String! + price: Float! + } + + type OrderSummary { + orderId: ID! + product: Product! + quantity: Int! + } + `; + + const visitorOrderSummary = getVisitor(schema, 'OrderSummary', CodeGenGenerateEnum.code); + expect(visitorOrderSummary.generate()).toMatchInlineSnapshot(` + "// swiftlint:disable all + import Amplify + import Foundation + + public struct OrderSummary: Embeddable { + var orderId: String + var product: Product + var quantity: Int + }" + `); + + const visitorOrderSummarySchema = getVisitor(schema, 'OrderSummary', CodeGenGenerateEnum.metadata); + expect(visitorOrderSummarySchema.generate()).toMatchInlineSnapshot(` + "// swiftlint:disable all + import Amplify + import Foundation + + extension OrderSummary { + // MARK: - CodingKeys + public enum CodingKeys: String, ModelKey { + case orderId + case product + case quantity + } + + public static let keys = CodingKeys.self + // MARK: - ModelSchema + + public static let schema = defineSchema { model in + let orderSummary = OrderSummary.keys + + model.listPluralName = \\"OrderSummaries\\" + model.syncPluralName = \\"OrderSummaries\\" + + model.fields( + .field(orderSummary.orderId, is: .required, ofType: .string), + .field(orderSummary.product, is: .required, ofType: .model(type: Product.self)), + .field(orderSummary.quantity, is: .required, ofType: .int) + ) + } + }" + `); + }); + it('should escape swift reserved keywords in enum', () => { const schema = /* GraphQL */ ` enum PostStatus { diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index f75e78ccb..97f10a8ee 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -662,7 +662,7 @@ export class AppSyncSwiftVisitor< if (isEnumType) { ofType = `.enum(type: ${typeName})`; } else if (isModelType) { - ofType = `.model(${typeName})`; + ofType = `.model(type: ${typeName})`; } else if (isNonModelType) { ofType = `.embedded(type: ${typeName})`; } else {