@@ -16,6 +16,7 @@ import {
1616 TranslationBehavior ,
1717} from '@aws-amplify/data-construct' ;
1818import { GraphqlOutput } from '@aws-amplify/backend-output-schemas' ;
19+ import { generateModelsSync } from '@aws-amplify/graphql-generator' ;
1920import * as path from 'path' ;
2021import { AmplifyDataError , DataProps } from './types.js' ;
2122import {
@@ -45,6 +46,10 @@ import {
4546 FunctionSchemaAccess ,
4647 JsResolver ,
4748} from '@aws-amplify/data-schema-types' ;
49+ import { Bucket } from 'aws-cdk-lib/aws-s3' ;
50+ import { BucketDeployment , Source } from 'aws-cdk-lib/aws-s3-deployment' ;
51+
52+ const modelIntrospectionSchemaKey = 'modelIntrospectionSchema.json' ;
4853
4954/**
5055 * Singleton factory for AmplifyGraphqlApi constructs that can be used in Amplify project files.
@@ -55,6 +60,8 @@ export class DataFactory implements ConstructFactory<AmplifyData> {
5560 // publicly accessible for testing purpose only.
5661 static factoryCount = 0 ;
5762
63+ readonly provides = 'DataResources' ;
64+
5865 private generator : ConstructContainerEntryGenerator ;
5966
6067 /**
@@ -231,14 +238,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
231238 ...schemasLambdaFunctions ,
232239 } ) ;
233240 let amplifyApi = undefined ;
241+ let modelIntrospectionSchema : string | undefined = undefined ;
234242
235243 const isSandboxDeployment =
236244 scope . node . tryGetContext ( CDKContextKey . DEPLOYMENT_TYPE ) === 'sandbox' ;
237245
238246 try {
247+ const combinedSchema = combineCDKSchemas ( amplifyGraphqlDefinitions ) ;
248+ modelIntrospectionSchema = generateModelsSync ( {
249+ schema : combinedSchema . schema ,
250+ target : 'introspection' ,
251+ } ) [ 'model-introspection.json' ] ;
252+
239253 amplifyApi = new AmplifyData ( scope , this . name , {
240254 apiName : this . name ,
241- definition : combineCDKSchemas ( amplifyGraphqlDefinitions ) ,
255+ definition : combinedSchema ,
242256 authorizationModes,
243257 outputStorageStrategy : this . outputStorageStrategy ,
244258 functionNameMap,
@@ -263,6 +277,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
263277 ) ;
264278 }
265279
280+ // TODO Any risk that this throws?
281+ const modelIntrospectionSchemaBucket = new Bucket (
282+ scope ,
283+ 'modelIntrospectionSchemaBucket' ,
284+ { enforceSSL : true }
285+ ) ;
286+ new BucketDeployment ( scope , 'modelIntrospectionSchemaBucketDeployment' , {
287+ // See https://github.com/aws-amplify/amplify-category-api/pull/1939
288+ memoryLimit : 1536 ,
289+ destinationBucket : modelIntrospectionSchemaBucket ,
290+ sources : [
291+ Source . data ( modelIntrospectionSchemaKey , modelIntrospectionSchema ) ,
292+ ] ,
293+ } ) ;
294+
266295 Tags . of ( amplifyApi ) . add ( TagName . FRIENDLY_NAME , this . name ) ;
267296
268297 /**;
@@ -279,10 +308,15 @@ class DataGenerator implements ConstructContainerEntryGenerator {
279308 ssmEnvironmentEntriesGenerator . generateSsmEnvironmentEntries ( {
280309 [ `${ this . name } _GRAPHQL_ENDPOINT` ] :
281310 amplifyApi . resources . cfnResources . cfnGraphqlApi . attrGraphQlUrl ,
311+ [ `${ this . name } _MODEL_INTROSPECTION_SCHEMA_BUCKET_NAME` ] :
312+ modelIntrospectionSchemaBucket . bucketName ,
313+ [ `${ this . name } _MODEL_INTROSPECTION_SCHEMA_KEY` ] :
314+ modelIntrospectionSchemaKey ,
282315 } ) ;
283316
284317 const policyGenerator = new AppSyncPolicyGenerator (
285- amplifyApi . resources . graphqlApi
318+ amplifyApi . resources . graphqlApi ,
319+ `${ modelIntrospectionSchemaBucket . bucketArn } /${ modelIntrospectionSchemaKey } `
286320 ) ;
287321
288322 schemasFunctionSchemaAccess . forEach ( ( accessDefinition ) => {
0 commit comments