Skip to content

Commit d381f73

Browse files
committed
feat: Add mis build to the data factory
1 parent 5d873a1 commit d381f73

File tree

6 files changed

+140
-35
lines changed

6 files changed

+140
-35
lines changed

.changeset/rare-kiwis-live.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/backend': patch
3+
'@aws-amplify/backend-data': patch
4+
---
5+
6+
feat: Add mis build to S3 from the data construct factory

package-lock.json

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/backend-data/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"@aws-amplify/backend-output-storage": "^1.1.3",
3232
"@aws-amplify/backend-output-schemas": "^1.4.0",
3333
"@aws-amplify/data-construct": "^1.10.1",
34-
"@aws-amplify/plugin-types": "^1.4.0",
35-
"@aws-amplify/data-schema-types": "^1.2.0"
34+
"@aws-amplify/data-schema-types": "^1.2.0",
35+
"@aws-amplify/graphql-generator": "^0.5.1",
36+
"@aws-amplify/plugin-types": "^1.4.0"
3637
}
3738
}

packages/backend-data/src/app_sync_policy_generator.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export class AppSyncPolicyGenerator {
1414
/**
1515
* Initialize with the GraphqlAPI that the policies will be scoped to
1616
*/
17-
constructor(private readonly graphqlApi: IGraphqlApi) {
17+
constructor(
18+
private readonly graphqlApi: IGraphqlApi,
19+
private readonly modelIntrospectionSchemaArn?: string
20+
) {
1821
this.stack = Stack.of(graphqlApi);
1922
}
2023
/**
@@ -29,13 +32,25 @@ export class AppSyncPolicyGenerator {
2932
.map((action) => actionToTypeMap[action])
3033
// convert Type to resourceName
3134
.map((type) => [this.graphqlApi.arn, 'types', type, '*'].join('/'));
32-
return new Policy(this.stack, `${this.policyPrefix}${this.policyCount++}`, {
33-
statements: [
35+
36+
const statements = [
37+
new PolicyStatement({
38+
actions: ['appsync:GraphQL'],
39+
resources,
40+
}),
41+
];
42+
43+
if (this.modelIntrospectionSchemaArn) {
44+
statements.push(
3445
new PolicyStatement({
35-
actions: ['appsync:GraphQL'],
36-
resources,
37-
}),
38-
],
46+
actions: ['s3:GetObject'],
47+
resources: [this.modelIntrospectionSchemaArn],
48+
})
49+
);
50+
}
51+
52+
return new Policy(this.stack, `${this.policyPrefix}${this.policyCount++}`, {
53+
statements,
3954
});
4055
}
4156
}

packages/backend-data/src/factory.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ const createConstructContainerWithUserPoolAuthRegistered = (
8585
authenticatedUserIamRole: new Role(stack, 'testAuthRole', {
8686
assumedBy: new ServicePrincipal('test.amazon.com'),
8787
}),
88-
identityPoolId: 'identityPoolId',
8988
cfnResources: {
9089
cfnUserPool: new CfnUserPool(stack, 'CfnUserPool', {}),
9190
cfnUserPoolClient: new CfnUserPoolClient(stack, 'CfnUserPoolClient', {
@@ -101,6 +100,7 @@ const createConstructContainerWithUserPoolAuthRegistered = (
101100
),
102101
},
103102
groups: {},
103+
identityPoolId: 'identityPool',
104104
},
105105
}),
106106
});
@@ -567,6 +567,23 @@ void describe('DataFactory', () => {
567567
},
568568
],
569569
},
570+
{
571+
Action: 's3:GetObject',
572+
Resource: {
573+
'Fn::Join': [
574+
'',
575+
[
576+
{
577+
'Fn::GetAtt': [
578+
'modelIntrospectionSchemaBucketF566B665',
579+
'Arn',
580+
],
581+
},
582+
'/modelIntrospectionSchema.json',
583+
],
584+
],
585+
},
586+
},
570587
],
571588
},
572589
Roles: [
@@ -675,6 +692,23 @@ void describe('DataFactory', () => {
675692
],
676693
},
677694
},
695+
{
696+
Action: 's3:GetObject',
697+
Resource: {
698+
'Fn::Join': [
699+
'',
700+
[
701+
{
702+
'Fn::GetAtt': [
703+
'modelIntrospectionSchemaBucketF566B665',
704+
'Arn',
705+
],
706+
},
707+
'/modelIntrospectionSchema.json',
708+
],
709+
],
710+
},
711+
},
678712
],
679713
},
680714
Roles: [
@@ -701,6 +735,23 @@ void describe('DataFactory', () => {
701735
],
702736
},
703737
},
738+
{
739+
Action: 's3:GetObject',
740+
Resource: {
741+
'Fn::Join': [
742+
'',
743+
[
744+
{
745+
'Fn::GetAtt': [
746+
'modelIntrospectionSchemaBucketF566B665',
747+
'Arn',
748+
],
749+
},
750+
'/modelIntrospectionSchema.json',
751+
],
752+
],
753+
},
754+
},
704755
],
705756
},
706757
Roles: [

packages/backend-data/src/factory.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
TranslationBehavior,
1818
} from '@aws-amplify/data-construct';
1919
import { GraphqlOutput } from '@aws-amplify/backend-output-schemas';
20+
import { generateModelsSync } from '@aws-amplify/graphql-generator';
2021
import * as path from 'path';
2122
import { AmplifyDataError, DataProps } from './types.js';
2223
import {
@@ -46,6 +47,10 @@ import {
4647
FunctionSchemaAccess,
4748
JsResolver,
4849
} from '@aws-amplify/data-schema-types';
50+
import { Bucket } from 'aws-cdk-lib/aws-s3';
51+
import { BucketDeployment, Source } from 'aws-cdk-lib/aws-s3-deployment';
52+
53+
const modelIntrospectionSchemaKey = 'modelIntrospectionSchema.json';
4954

5055
/**
5156
* Singleton factory for AmplifyGraphqlApi constructs that can be used in Amplify project files.
@@ -232,14 +237,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
232237
...schemasLambdaFunctions,
233238
});
234239
let amplifyApi = undefined;
240+
let modelIntrospectionSchema: string | undefined = undefined;
235241

236242
const isSandboxDeployment =
237243
scope.node.tryGetContext(CDKContextKey.DEPLOYMENT_TYPE) === 'sandbox';
238244

239245
try {
246+
const combinedSchema = combineCDKSchemas(amplifyGraphqlDefinitions);
247+
modelIntrospectionSchema = generateModelsSync({
248+
schema: combinedSchema.schema,
249+
target: 'introspection',
250+
})['model-introspection.json'];
251+
240252
amplifyApi = new AmplifyData(scope, this.name, {
241253
apiName: this.name,
242-
definition: combineCDKSchemas(amplifyGraphqlDefinitions),
254+
definition: combinedSchema,
243255
authorizationModes,
244256
outputStorageStrategy: this.outputStorageStrategy,
245257
functionNameMap,
@@ -264,6 +276,20 @@ class DataGenerator implements ConstructContainerEntryGenerator {
264276
);
265277
}
266278

279+
const modelIntrospectionSchemaBucket = new Bucket(
280+
scope,
281+
'modelIntrospectionSchemaBucket',
282+
{ enforceSSL: true }
283+
);
284+
new BucketDeployment(scope, 'modelIntrospectionSchemaBucketDeployment', {
285+
// See https://github.com/aws-amplify/amplify-category-api/pull/1939
286+
memoryLimit: 1536,
287+
destinationBucket: modelIntrospectionSchemaBucket,
288+
sources: [
289+
Source.data(modelIntrospectionSchemaKey, modelIntrospectionSchema),
290+
],
291+
});
292+
267293
Tags.of(amplifyApi).add(TagName.FRIENDLY_NAME, this.name);
268294

269295
/**;
@@ -280,10 +306,15 @@ class DataGenerator implements ConstructContainerEntryGenerator {
280306
ssmEnvironmentEntriesGenerator.generateSsmEnvironmentEntries({
281307
[`${this.name}_GRAPHQL_ENDPOINT`]:
282308
amplifyApi.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl,
309+
[`${this.name}_MODEL_INTROSPECTION_SCHEMA_BUCKET_NAME`]:
310+
modelIntrospectionSchemaBucket.bucketName,
311+
[`${this.name}_MODEL_INTROSPECTION_SCHEMA_KEY`]:
312+
modelIntrospectionSchemaKey,
283313
});
284314

285315
const policyGenerator = new AppSyncPolicyGenerator(
286-
amplifyApi.resources.graphqlApi
316+
amplifyApi.resources.graphqlApi,
317+
`${modelIntrospectionSchemaBucket.bucketArn}/${modelIntrospectionSchemaKey}`
287318
);
288319

289320
schemasFunctionSchemaAccess.forEach((accessDefinition) => {

0 commit comments

Comments
 (0)