Skip to content

Commit bc11582

Browse files
authored
Merge pull request #208 from aws-amplify/master
Release codegen v2.26.2
2 parents 031a37f + 7265f3a commit bc11582

File tree

5 files changed

+84
-59
lines changed

5 files changed

+84
-59
lines changed

.github/stale.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/amplify-codegen-e2e-core/src/init/amplifyPull.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function amplifyPull(
5555
}
5656

5757
if (settings.emptyDir) {
58-
chain.wait(/Successfully pulled backend environment .+ from the cloud\./).wait("Run 'amplify pull' to sync upstream changes.");
58+
chain.wait(/Successfully pulled backend environment .+ from the cloud\./).wait("Run 'amplify pull' to sync future upstream changes.");
5959
} else {
6060
chain.wait('Post-pull status').wait('Current Environment').wait(tableHeaderRegex).wait(tableSeperator);
6161
}

packages/amplify-codegen-e2e-tests/src/environment/env.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { nspawn as spawn, getCLIPath, getSocialProviders } from 'amplify-codegen
33
export function addEnvironment(cwd: string, settings: { envName: string; numLayers?: number }): Promise<void> {
44
return new Promise((resolve, reject) => {
55
const chain = spawn(getCLIPath(), ['env', 'add'], { cwd, stripColors: true })
6-
.wait('Do you want to use an existing environment?')
7-
.sendLine('n')
86
.wait('Enter a name for the environment')
97
.sendLine(settings.envName)
108
.wait('Select the authentication method you want to use:')
@@ -29,8 +27,6 @@ export function addEnvironment(cwd: string, settings: { envName: string; numLaye
2927
export function addEnvironmentWithImportedAuth(cwd: string, settings: { envName: string; currentEnvName: string }): Promise<void> {
3028
return new Promise((resolve, reject) => {
3129
spawn(getCLIPath(), ['env', 'add'], { cwd, stripColors: true })
32-
.wait('Do you want to use an existing environment?')
33-
.sendConfirmNo()
3430
.wait('Enter a name for the environment')
3531
.sendLine(settings.envName)
3632
.wait('Select the authentication method you want to use:')
@@ -143,8 +139,6 @@ export function addEnvironmentHostedUI(cwd: string, settings: { envName: string
143139
const { FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, GOOGLE_APP_ID, GOOGLE_APP_SECRET, AMAZON_APP_ID, AMAZON_APP_SECRET } = getSocialProviders();
144140
return new Promise((resolve, reject) => {
145141
spawn(getCLIPath(), ['env', 'add'], { cwd, stripColors: true })
146-
.wait('Do you want to use an existing environment?')
147-
.sendLine('n')
148142
.wait('Enter a name for the environment')
149143
.sendLine(settings.envName)
150144
.wait('Select the authentication method you want to use:')

packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ const getVisitor = (
1515
isTimestampFieldsAdded: boolean = true,
1616
emitAuthProvider: boolean = true,
1717
generateIndexRules: boolean = true,
18-
handleListNullabilityTransparently: boolean = true
18+
handleListNullabilityTransparently: boolean = true,
1919
) => {
2020
const ast = parse(schema);
2121
const builtSchema = buildSchemaWithDirectives(schema);
2222
const visitor = new AppSyncSwiftVisitor(
2323
builtSchema,
24-
{ directives, target: 'swift', scalars: SWIFT_SCALAR_MAP, isTimestampFieldsAdded, emitAuthProvider, generateIndexRules, handleListNullabilityTransparently },
24+
{
25+
directives,
26+
target: 'swift',
27+
scalars: SWIFT_SCALAR_MAP,
28+
isTimestampFieldsAdded,
29+
emitAuthProvider,
30+
generateIndexRules,
31+
handleListNullabilityTransparently,
32+
},
2533
{ selectedType, generate },
2634
);
2735
visit(ast, { leave: visitor });
@@ -2063,14 +2071,14 @@ describe('AppSyncSwiftVisitor', () => {
20632071
});
20642072

20652073
describe('timestamp fields', () => {
2066-
const schema = /* GraphQL */ `
2067-
type SimpleModel @model {
2068-
id: ID!
2069-
name: String
2070-
bar: String
2071-
}
2072-
`;
20732074
it('should generate timestamp fields if timestamp is enabled', () => {
2075+
const schema = /* GraphQL */ `
2076+
type SimpleModel @model {
2077+
id: ID!
2078+
name: String
2079+
bar: String
2080+
}
2081+
`;
20742082
const visitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.code, true);
20752083
const generatedCode = visitor.generate();
20762084
expect(generatedCode).toMatchInlineSnapshot(`
@@ -2109,6 +2117,13 @@ describe('AppSyncSwiftVisitor', () => {
21092117
`);
21102118
});
21112119
it('should keep the original init method if timestamp is disabled', () => {
2120+
const schema = /* GraphQL */ `
2121+
type SimpleModel @model {
2122+
id: ID!
2123+
name: String
2124+
bar: String
2125+
}
2126+
`;
21122127
const visitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.code, false);
21132128
const generatedCode = visitor.generate();
21142129
expect(generatedCode).toMatchInlineSnapshot(`
@@ -2131,5 +2146,35 @@ describe('AppSyncSwiftVisitor', () => {
21312146
}"
21322147
`);
21332148
});
2149+
it('should keep the original init method if timestamp ff is enabled and schema has explicit timestamp fields', () => {
2150+
const schema = /* GraphQL */ `
2151+
type SimpleModel @model {
2152+
id: ID!
2153+
createdAt: AWSDateTime
2154+
updatedAt: AWSDateTime
2155+
}
2156+
`;
2157+
const visitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.code, true);
2158+
const generatedCode = visitor.generate();
2159+
expect(generatedCode).toMatchInlineSnapshot(`
2160+
"// swiftlint:disable all
2161+
import Amplify
2162+
import Foundation
2163+
2164+
public struct SimpleModel: Model {
2165+
public let id: String
2166+
public var createdAt: Temporal.DateTime?
2167+
public var updatedAt: Temporal.DateTime?
2168+
2169+
public init(id: String = UUID().uuidString,
2170+
createdAt: Temporal.DateTime? = nil,
2171+
updatedAt: Temporal.DateTime? = nil) {
2172+
self.id = id
2173+
self.createdAt = createdAt
2174+
self.updatedAt = updatedAt
2175+
}
2176+
}"
2177+
`);
2178+
});
21342179
});
21352180
});

packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export interface RawAppSyncModelSwiftConfig extends RawAppSyncModelConfig {
2323
* @type boolean
2424
* @descriptions optional boolean, if true emits the provider value of @auth directives
2525
*/
26-
emitAuthProvider?: boolean;
26+
emitAuthProvider?: boolean;
2727

28-
/**
28+
/**
2929
* @name directives
3030
* @type boolean
3131
* @description optional, defines if custom indexes defined by @key directive should be generated.
@@ -90,7 +90,7 @@ export class AppSyncSwiftVisitor<
9090
isEnum: this.isEnumType(field),
9191
listType: field.isList ? listType : undefined,
9292
isListNullable: field.isListNullable,
93-
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently
93+
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently,
9494
});
9595
});
9696
const initParams: CodeGenField[] = this.getWritableFields(obj);
@@ -102,7 +102,7 @@ export class AppSyncSwiftVisitor<
102102
})
103103
.join(',\n'),
104104
).trim()})`;
105-
if (this.config.isTimestampFieldsAdded) {
105+
if (this.config.isTimestampFieldsAdded && this.hasReadOnlyFields(obj)) {
106106
//public constructor
107107
structBlock.addClassMethod(
108108
'init',
@@ -120,7 +120,9 @@ export class AppSyncSwiftVisitor<
120120
isEnum: this.isEnumType(field),
121121
listType: field.isList ? listType : undefined,
122122
isListNullable: field.isListNullable,
123-
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently
123+
handleListNullabilityTransparently: this.isHasManyConnectionField(field)
124+
? false
125+
: this.config.handleListNullabilityTransparently,
124126
},
125127
};
126128
}),
@@ -144,7 +146,9 @@ export class AppSyncSwiftVisitor<
144146
isEnum: this.isEnumType(field),
145147
listType: field.isList ? listType : undefined,
146148
isListNullable: field.isListNullable,
147-
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently
149+
handleListNullabilityTransparently: this.isHasManyConnectionField(field)
150+
? false
151+
: this.config.handleListNullabilityTransparently,
148152
},
149153
};
150154
}),
@@ -169,7 +173,9 @@ export class AppSyncSwiftVisitor<
169173
isEnum: this.isEnumType(field),
170174
listType: field.isList ? listType : undefined,
171175
isListNullable: field.isListNullable,
172-
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently
176+
handleListNullabilityTransparently: this.isHasManyConnectionField(field)
177+
? false
178+
: this.config.handleListNullabilityTransparently,
173179
},
174180
};
175181
}),
@@ -214,7 +220,7 @@ export class AppSyncSwiftVisitor<
214220
isEnum: this.isEnumType(field),
215221
listType: field.isList ? ListType.ARRAY : undefined,
216222
isListNullable: field.isListNullable,
217-
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently
223+
handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently,
218224
});
219225
});
220226
result.push(structBlock.string);
@@ -228,9 +234,7 @@ export class AppSyncSwiftVisitor<
228234
Object.values(this.getSelectedModels())
229235
.filter(m => m.type === 'model')
230236
.forEach(model => {
231-
const schemaDeclarations = new SwiftDeclarationBlock()
232-
.asKind('extension')
233-
.withName(this.getModelName(model));
237+
const schemaDeclarations = new SwiftDeclarationBlock().asKind('extension').withName(this.getModelName(model));
234238

235239
this.generateCodingKeys(this.getModelName(model), model, schemaDeclarations),
236240
this.generateModelSchema(this.getModelName(model), model, schemaDeclarations);
@@ -239,9 +243,7 @@ export class AppSyncSwiftVisitor<
239243
});
240244

241245
Object.values(this.getSelectedNonModels()).forEach(model => {
242-
const schemaDeclarations = new SwiftDeclarationBlock()
243-
.asKind('extension')
244-
.withName(this.getNonModelName(model));
246+
const schemaDeclarations = new SwiftDeclarationBlock().asKind('extension').withName(this.getNonModelName(model));
245247

246248
this.generateCodingKeys(this.getNonModelName(model), model, schemaDeclarations),
247249
this.generateModelSchema(this.getNonModelName(model), model, schemaDeclarations);
@@ -350,8 +352,10 @@ export class AppSyncSwiftVisitor<
350352
const name = `${modelKeysName}.${this.getFieldName(field)}`;
351353
const typeName = this.getSwiftModelTypeName(field);
352354
const { connectionInfo } = field;
353-
const isRequiredField = ((!this.isHasManyConnectionField(field)) && this.config.handleListNullabilityTransparently) ?
354-
this.isRequiredField(field) : this.isFieldRequired(field);
355+
const isRequiredField =
356+
!this.isHasManyConnectionField(field) && this.config.handleListNullabilityTransparently
357+
? this.isRequiredField(field)
358+
: this.isFieldRequired(field);
355359
const isRequired = isRequiredField ? '.required' : '.optional';
356360
// connected field
357361
if (connectionInfo) {
@@ -438,14 +442,14 @@ export class AppSyncSwiftVisitor<
438442

439443
protected generateKeyRules(model: CodeGenModel): string[] {
440444
const keyDirectives = model.directives
441-
.filter((directive) => directive.name === 'key')
442-
.map((directive) => {
445+
.filter(directive => directive.name === 'key')
446+
.map(directive => {
443447
const name = directive.arguments.name ? `"${directive.arguments.name}"` : 'nil';
444448
const fields: string = directive.arguments.fields.map((field: string) => `"${field}"`).join(', ');
445449
return `.index(fields: [${fields}], name: ${name})`;
446450
});
447451

448-
return keyDirectives
452+
return keyDirectives;
449453
}
450454

451455
protected isHasManyConnectionField(field: CodeGenField): boolean {
@@ -471,8 +475,8 @@ export class AppSyncSwiftVisitor<
471475
authRule.push('allow: .private');
472476
break;
473477
case AuthStrategy.public:
474-
authRule.push('allow: .public');
475-
break;
478+
authRule.push('allow: .public');
479+
break;
476480
case AuthStrategy.groups:
477481
authRule.push('allow: .groups');
478482
authRule.push(`groupClaim: "${rule.groupClaim}"`);
@@ -502,4 +506,8 @@ export class AppSyncSwiftVisitor<
502506
protected getWritableFields(model: CodeGenModel): CodeGenField[] {
503507
return model.fields.filter(f => !f.isReadOnly);
504508
}
509+
510+
protected hasReadOnlyFields(model: CodeGenModel): boolean {
511+
return model.fields.filter(f => f.isReadOnly).length !== 0;
512+
}
505513
}

0 commit comments

Comments
 (0)