Skip to content

Commit a649136

Browse files
authored
feat(modelgen): ios - support public/private @auth rules (#181)
1 parent ca25ab1 commit a649136

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,52 @@ describe('AppSyncSwiftVisitor', () => {
18121812
}"
18131813
`);
18141814
});
1815+
1816+
it('should generate class with private authorization', () => {
1817+
const schema = /* GraphQL */ `
1818+
type Post @model @auth(rules: [{ allow: private }]) {
1819+
id: ID!
1820+
title: String!
1821+
}
1822+
`;
1823+
const visitor = getVisitor(schema, 'Post', CodeGenGenerateEnum.metadata);
1824+
const generatedCode = visitor.generate();
1825+
expect(generatedCode).toMatchInlineSnapshot(`
1826+
"// swiftlint:disable all
1827+
import Amplify
1828+
import Foundation
1829+
1830+
extension Post {
1831+
// MARK: - CodingKeys
1832+
public enum CodingKeys: String, ModelKey {
1833+
case id
1834+
case title
1835+
case createdAt
1836+
case updatedAt
1837+
}
1838+
1839+
public static let keys = CodingKeys.self
1840+
// MARK: - ModelSchema
1841+
1842+
public static let schema = defineSchema { model in
1843+
let post = Post.keys
1844+
1845+
model.authRules = [
1846+
rule(allow: .private, operations: [.create, .update, .delete, .read])
1847+
]
1848+
1849+
model.pluralName = \\"Posts\\"
1850+
1851+
model.fields(
1852+
.id(),
1853+
.field(post.title, is: .required, ofType: .string),
1854+
.field(post.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
1855+
.field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
1856+
)
1857+
}
1858+
}"
1859+
`);
1860+
});
18151861
});
18161862
it('should support multiple auth rules', () => {
18171863
const schema = /* GraphQL */ `
@@ -1821,7 +1867,7 @@ describe('AppSyncSwiftVisitor', () => {
18211867
rules: [
18221868
{ allow: groups, groups: ["admin"] }
18231869
{ allow: owner, operations: ["create", "update"] }
1824-
{ allow: public, operation: ["read"] }
1870+
{ allow: public, operations: ["read"] }
18251871
]
18261872
) {
18271873
id: ID!
@@ -1855,7 +1901,8 @@ describe('AppSyncSwiftVisitor', () => {
18551901
18561902
model.authRules = [
18571903
rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\"], operations: [.create, .update, .delete, .read]),
1858-
rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", operations: [.create, .update])
1904+
rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", operations: [.create, .update]),
1905+
rule(allow: .public, operations: [.read])
18591906
]
18601907
18611908
model.pluralName = \\"Posts\\"

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ export class AppSyncSwiftVisitor<
395395
authRule.push(`ownerField: "${rule.ownerField}"`);
396396
authRule.push(`identityClaim: "${rule.identityClaim}"`);
397397
break;
398+
case AuthStrategy.private:
399+
authRule.push('allow: .private');
400+
break;
401+
case AuthStrategy.public:
402+
authRule.push('allow: .public');
403+
break;
398404
case AuthStrategy.groups:
399405
authRule.push('allow: .groups');
400406
authRule.push(`groupClaim: "${rule.groupClaim}"`);

0 commit comments

Comments
 (0)