@@ -13,12 +13,13 @@ const getVisitor = (
13
13
selectedType ?: string ,
14
14
generate : CodeGenGenerateEnum = CodeGenGenerateEnum . code ,
15
15
isTimestampFieldsAdded : boolean = true ,
16
+ emitAuthProvider : boolean = true ,
16
17
) => {
17
18
const ast = parse ( schema ) ;
18
19
const builtSchema = buildSchemaWithDirectives ( schema ) ;
19
20
const visitor = new AppSyncSwiftVisitor (
20
21
builtSchema ,
21
- { directives, target : 'swift' , scalars : SWIFT_SCALAR_MAP , isTimestampFieldsAdded } ,
22
+ { directives, target : 'swift' , scalars : SWIFT_SCALAR_MAP , isTimestampFieldsAdded, emitAuthProvider } ,
22
23
{ selectedType, generate } ,
23
24
) ;
24
25
visit ( ast , { leave : visitor } ) ;
@@ -1255,7 +1256,7 @@ describe('AppSyncSwiftVisitor', () => {
1255
1256
let post = Post.keys
1256
1257
1257
1258
model.authRules = [
1258
- rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete, .read])
1259
+ rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete, .read])
1259
1260
]
1260
1261
1261
1262
model.pluralName = \\"Posts\\"
@@ -1304,7 +1305,7 @@ describe('AppSyncSwiftVisitor', () => {
1304
1305
let post = Post.keys
1305
1306
1306
1307
model.authRules = [
1307
- rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete, .read])
1308
+ rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete, .read])
1308
1309
]
1309
1310
1310
1311
model.pluralName = \\"Posts\\"
@@ -1354,7 +1355,7 @@ describe('AppSyncSwiftVisitor', () => {
1354
1355
let post = Post.keys
1355
1356
1356
1357
model.authRules = [
1357
- rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete])
1358
+ rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete])
1358
1359
]
1359
1360
1360
1361
model.pluralName = \\"Posts\\"
@@ -1404,7 +1405,7 @@ describe('AppSyncSwiftVisitor', () => {
1404
1405
let post = Post.keys
1405
1406
1406
1407
model.authRules = [
1407
- rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"sub\\", operations: [.create, .update, .delete, .read])
1408
+ rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"sub\\", provider: .userPools, operations: [.create, .update, .delete, .read])
1408
1409
]
1409
1410
1410
1411
model.pluralName = \\"Posts\\"
@@ -1451,7 +1452,7 @@ describe('AppSyncSwiftVisitor', () => {
1451
1452
let post = Post.keys
1452
1453
1453
1454
model.authRules = [
1454
- rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete, .read])
1455
+ rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete, .read])
1455
1456
]
1456
1457
1457
1458
model.pluralName = \\"Posts\\"
@@ -1497,7 +1498,7 @@ describe('AppSyncSwiftVisitor', () => {
1497
1498
let post = Post.keys
1498
1499
1499
1500
model.authRules = [
1500
- rule(allow: .owner, ownerField: \\"customField\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete, .read])
1501
+ rule(allow: .owner, ownerField: \\"customField\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete, .read])
1501
1502
]
1502
1503
1503
1504
model.pluralName = \\"Posts\\"
@@ -1549,8 +1550,8 @@ describe('AppSyncSwiftVisitor', () => {
1549
1550
let post = Post.keys
1550
1551
1551
1552
model.authRules = [
1552
- rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete, .read]),
1553
- rule(allow: .owner, ownerField: \\"editors\\", identityClaim: \\"cognito:username\\", operations: [.update, .read])
1553
+ rule(allow: .owner, ownerField: \\"author\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete, .read]),
1554
+ rule(allow: .owner, ownerField: \\"editors\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.update, .read])
1554
1555
]
1555
1556
1556
1557
model.pluralName = \\"Posts\\"
@@ -1602,8 +1603,8 @@ describe('AppSyncSwiftVisitor', () => {
1602
1603
let employee = Employee.keys
1603
1604
1604
1605
model.authRules = [
1605
- rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", operations: [.create, .update, .delete, .read]),
1606
- rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"Admins\\"], operations: [.create, .update, .delete, .read])
1606
+ rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update, .delete, .read]),
1607
+ rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"Admins\\"], provider: .userPools, operations: [.create, .update, .delete, .read])
1607
1608
]
1608
1609
1609
1610
model.pluralName = \\"Employees\\"
@@ -1654,7 +1655,7 @@ describe('AppSyncSwiftVisitor', () => {
1654
1655
let post = Post.keys
1655
1656
1656
1657
model.authRules = [
1657
- rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\", \\"editors\\"], operations: [.create, .update, .delete, .read])
1658
+ rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\", \\"editors\\"], provider: .userPools, operations: [.create, .update, .delete, .read])
1658
1659
]
1659
1660
1660
1661
model.pluralName = \\"Posts\\"
@@ -1702,7 +1703,7 @@ describe('AppSyncSwiftVisitor', () => {
1702
1703
let post = Post.keys
1703
1704
1704
1705
model.authRules = [
1705
- rule(allow: .groups, groupClaim: \\"cognito:groups\\", groupsField: \\"groups\\", operations: [.create, .update, .delete, .read])
1706
+ rule(allow: .groups, groupClaim: \\"cognito:groups\\", groupsField: \\"groups\\", provider: .userPools, operations: [.create, .update, .delete, .read])
1706
1707
]
1707
1708
1708
1709
model.pluralName = \\"Posts\\"
@@ -1750,7 +1751,7 @@ describe('AppSyncSwiftVisitor', () => {
1750
1751
let post = Post.keys
1751
1752
1752
1753
model.authRules = [
1753
- rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\"], operations: [.create, .update, .delete])
1754
+ rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\"], provider: .userPools, operations: [.create, .update, .delete])
1754
1755
]
1755
1756
1756
1757
model.pluralName = \\"Posts\\"
@@ -1797,7 +1798,7 @@ describe('AppSyncSwiftVisitor', () => {
1797
1798
let post = Post.keys
1798
1799
1799
1800
model.authRules = [
1800
- rule(allow: .groups, groupClaim: \\"custom:groups\\", groups: [\\"admin\\"], operations: [.create, .update, .delete, .read])
1801
+ rule(allow: .groups, groupClaim: \\"custom:groups\\", groups: [\\"admin\\"], provider: .userPools, operations: [.create, .update, .delete, .read])
1801
1802
]
1802
1803
1803
1804
model.pluralName = \\"Posts\\"
@@ -1900,8 +1901,8 @@ describe('AppSyncSwiftVisitor', () => {
1900
1901
let post = Post.keys
1901
1902
1902
1903
model.authRules = [
1903
- rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\"], operations: [.create, .update, .delete, .read]),
1904
- rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", operations: [.create, .update]),
1904
+ rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\"], provider: .userPools, operations: [.create, .update, .delete, .read]),
1905
+ rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update]),
1905
1906
rule(allow: .public, operations: [.read])
1906
1907
]
1907
1908
@@ -1918,6 +1919,67 @@ describe('AppSyncSwiftVisitor', () => {
1918
1919
}"
1919
1920
` ) ;
1920
1921
} ) ;
1922
+
1923
+ it ( 'should add provider to auth rules' , ( ) => {
1924
+ const schema = /* GraphQL */ `
1925
+ type Post
1926
+ @model
1927
+ @auth(
1928
+ rules: [
1929
+ { allow: groups, groups: ["admin"] }
1930
+ { allow: owner, operations: ["create", "update"] }
1931
+ { allow: public, operations: ["read"], provider: "apiKey" }
1932
+ ]
1933
+ ) {
1934
+ id: ID!
1935
+ title: String!
1936
+ owner: String!
1937
+ }
1938
+ ` ;
1939
+
1940
+ const visitor = getVisitor ( schema , 'Post' , CodeGenGenerateEnum . metadata ) ;
1941
+ const generatedCode = visitor . generate ( ) ;
1942
+ expect ( generatedCode ) . toMatchInlineSnapshot ( `
1943
+ "// swiftlint:disable all
1944
+ import Amplify
1945
+ import Foundation
1946
+
1947
+ extension Post {
1948
+ // MARK: - CodingKeys
1949
+ public enum CodingKeys: String, ModelKey {
1950
+ case id
1951
+ case title
1952
+ case owner
1953
+ case createdAt
1954
+ case updatedAt
1955
+ }
1956
+
1957
+ public static let keys = CodingKeys.self
1958
+ // MARK: - ModelSchema
1959
+
1960
+ public static let schema = defineSchema { model in
1961
+ let post = Post.keys
1962
+
1963
+ model.authRules = [
1964
+ rule(allow: .groups, groupClaim: \\"cognito:groups\\", groups: [\\"admin\\"], provider: .userPools, operations: [.create, .update, .delete, .read]),
1965
+ rule(allow: .owner, ownerField: \\"owner\\", identityClaim: \\"cognito:username\\", provider: .userPools, operations: [.create, .update]),
1966
+ rule(allow: .public, provider: .apiKey, operations: [.read])
1967
+ ]
1968
+
1969
+ model.pluralName = \\"Posts\\"
1970
+
1971
+ model.fields(
1972
+ .id(),
1973
+ .field(post.title, is: .required, ofType: .string),
1974
+ .field(post.owner, is: .required, ofType: .string),
1975
+ .field(post.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
1976
+ .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
1977
+ )
1978
+ }
1979
+ }"
1980
+ ` ) ;
1981
+ } ) ;
1982
+
1921
1983
describe ( 'timestamp fields' , ( ) => {
1922
1984
const schema = /* GraphQL */ `
1923
1985
type SimpleModel @model {
0 commit comments