Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading