Skip to content

Commit f9c1b1a

Browse files
authored
feat(modelgen): ios - emit provider in @auth rules (#184)
* feat(modelgen): ios - emit provider in @auth rules * feat(modelgen): move emitProvider to Swift specific visitor * feat(modelgen): move emitProvider to Swift specific visitor
1 parent bdde4bd commit f9c1b1a

File tree

3 files changed

+120
-20
lines changed

3 files changed

+120
-20
lines changed

packages/amplify-codegen/src/commands/models.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,22 @@ async function generateModels(context) {
5959
isTimestampFieldsAdded = false;
6060
}
6161

62+
//get timestamp config value
63+
let emitAuthProvider = false;
64+
try {
65+
emitAuthProvider = FeatureFlags.getBoolean('codegen.emitAuthProvider');
66+
} catch (err) {
67+
emitAuthProvider = false;
68+
}
69+
6270
const appsyncLocalConfig = await appSyncDataStoreCodeGen.preset.buildGeneratesSection({
6371
baseOutputDir: outputPath,
6472
schema,
6573
config: {
6674
target: platformToLanguageMap[projectConfig.frontend] || projectConfig.frontend,
6775
directives: directiveDefinitions,
6876
isTimestampFieldsAdded,
77+
emitAuthProvider,
6978
},
7079
});
7180

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

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ const getVisitor = (
1313
selectedType?: string,
1414
generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code,
1515
isTimestampFieldsAdded: boolean = true,
16+
emitAuthProvider: boolean = true,
1617
) => {
1718
const ast = parse(schema);
1819
const builtSchema = buildSchemaWithDirectives(schema);
1920
const visitor = new AppSyncSwiftVisitor(
2021
builtSchema,
21-
{ directives, target: 'swift', scalars: SWIFT_SCALAR_MAP, isTimestampFieldsAdded },
22+
{ directives, target: 'swift', scalars: SWIFT_SCALAR_MAP, isTimestampFieldsAdded, emitAuthProvider },
2223
{ selectedType, generate },
2324
);
2425
visit(ast, { leave: visitor });
@@ -1255,7 +1256,7 @@ describe('AppSyncSwiftVisitor', () => {
12551256
let post = Post.keys
12561257
12571258
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])
12591260
]
12601261
12611262
model.pluralName = \\"Posts\\"
@@ -1304,7 +1305,7 @@ describe('AppSyncSwiftVisitor', () => {
13041305
let post = Post.keys
13051306
13061307
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])
13081309
]
13091310
13101311
model.pluralName = \\"Posts\\"
@@ -1354,7 +1355,7 @@ describe('AppSyncSwiftVisitor', () => {
13541355
let post = Post.keys
13551356
13561357
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])
13581359
]
13591360
13601361
model.pluralName = \\"Posts\\"
@@ -1404,7 +1405,7 @@ describe('AppSyncSwiftVisitor', () => {
14041405
let post = Post.keys
14051406
14061407
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])
14081409
]
14091410
14101411
model.pluralName = \\"Posts\\"
@@ -1451,7 +1452,7 @@ describe('AppSyncSwiftVisitor', () => {
14511452
let post = Post.keys
14521453
14531454
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])
14551456
]
14561457
14571458
model.pluralName = \\"Posts\\"
@@ -1497,7 +1498,7 @@ describe('AppSyncSwiftVisitor', () => {
14971498
let post = Post.keys
14981499
14991500
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])
15011502
]
15021503
15031504
model.pluralName = \\"Posts\\"
@@ -1549,8 +1550,8 @@ describe('AppSyncSwiftVisitor', () => {
15491550
let post = Post.keys
15501551
15511552
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])
15541555
]
15551556
15561557
model.pluralName = \\"Posts\\"
@@ -1602,8 +1603,8 @@ describe('AppSyncSwiftVisitor', () => {
16021603
let employee = Employee.keys
16031604
16041605
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])
16071608
]
16081609
16091610
model.pluralName = \\"Employees\\"
@@ -1654,7 +1655,7 @@ describe('AppSyncSwiftVisitor', () => {
16541655
let post = Post.keys
16551656
16561657
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])
16581659
]
16591660
16601661
model.pluralName = \\"Posts\\"
@@ -1702,7 +1703,7 @@ describe('AppSyncSwiftVisitor', () => {
17021703
let post = Post.keys
17031704
17041705
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])
17061707
]
17071708
17081709
model.pluralName = \\"Posts\\"
@@ -1750,7 +1751,7 @@ describe('AppSyncSwiftVisitor', () => {
17501751
let post = Post.keys
17511752
17521753
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])
17541755
]
17551756
17561757
model.pluralName = \\"Posts\\"
@@ -1797,7 +1798,7 @@ describe('AppSyncSwiftVisitor', () => {
17971798
let post = Post.keys
17981799
17991800
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])
18011802
]
18021803
18031804
model.pluralName = \\"Posts\\"
@@ -1900,8 +1901,8 @@ describe('AppSyncSwiftVisitor', () => {
19001901
let post = Post.keys
19011902
19021903
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]),
19051906
rule(allow: .public, operations: [.read])
19061907
]
19071908
@@ -1918,6 +1919,67 @@ describe('AppSyncSwiftVisitor', () => {
19181919
}"
19191920
`);
19201921
});
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+
19211983
describe('timestamp fields', () => {
19221984
const schema = /* GraphQL */ `
19231985
type SimpleModel @model {

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { indent, indentMultiline } from '@graphql-codegen/visitor-plugin-common';
1+
import { indent, indentMultiline, NormalizedScalarsMap } from '@graphql-codegen/visitor-plugin-common';
22
import { camelCase } from 'change-case';
3+
import { GraphQLSchema } from 'graphql';
34
import { lowerCaseFirst } from 'lower-case-first';
45
import { schemaTypeMap } from '../configs/swift-config';
56
import { SwiftDeclarationBlock, escapeKeywords, ListType } from '../languages/swift-declaration-block';
@@ -14,13 +15,38 @@ import {
1415
} from './appsync-visitor';
1516
import { AuthDirective, AuthStrategy } from '../utils/process-auth';
1617
import { printWarning } from '../utils/warn';
18+
import { SWIFT_SCALAR_MAP } from '../scalars';
19+
20+
export interface RawAppSyncModelSwiftConfig extends RawAppSyncModelConfig {
21+
/**
22+
* @name directives
23+
* @type boolean
24+
* @descriptions optional boolean, if true emits the provider value of @auth directives
25+
*/
26+
emitAuthProvider?: boolean;
27+
}
28+
29+
export interface ParsedAppSyncModelSwiftConfig extends ParsedAppSyncModelConfig {
30+
emitAuthProvider?: boolean;
31+
}
1732

1833
export class AppSyncSwiftVisitor<
19-
TRawConfig extends RawAppSyncModelConfig = RawAppSyncModelConfig,
20-
TPluginConfig extends ParsedAppSyncModelConfig = ParsedAppSyncModelConfig
34+
TRawConfig extends RawAppSyncModelSwiftConfig = RawAppSyncModelSwiftConfig,
35+
TPluginConfig extends ParsedAppSyncModelSwiftConfig = ParsedAppSyncModelSwiftConfig
2136
> extends AppSyncModelVisitor<TRawConfig, TPluginConfig> {
2237
protected modelExtensionImports: string[] = ['import Amplify', 'import Foundation'];
2338
protected imports: string[] = ['import Amplify', 'import Foundation'];
39+
40+
constructor(
41+
schema: GraphQLSchema,
42+
rawConfig: TRawConfig,
43+
additionalConfig: Partial<TPluginConfig>,
44+
defaultScalars: NormalizedScalarsMap = SWIFT_SCALAR_MAP,
45+
) {
46+
super(schema, rawConfig, additionalConfig, defaultScalars);
47+
this._parsedConfig.emitAuthProvider = rawConfig.emitAuthProvider || false;
48+
}
49+
2450
generate(): string {
2551
this.processDirectives();
2652
const code = [`// swiftlint:disable all`];
@@ -414,6 +440,9 @@ export class AppSyncSwiftVisitor<
414440
printWarning(`Model ${model.name} has auth with authStrategy ${rule.allow} of which is not yet supported in DataStore.`);
415441
return;
416442
}
443+
if (rule.provider != null && this.config.emitAuthProvider) {
444+
authRule.push(`provider: .${rule.provider}`);
445+
}
417446
authRule.push(`operations: [${rule.operations?.map(op => `.${op}`).join(', ')}]`);
418447
rules.push(`rule(${authRule.join(', ')})`);
419448
});

0 commit comments

Comments
 (0)