@@ -16,6 +16,7 @@ import {
16
16
TranslationBehavior ,
17
17
} from '@aws-amplify/data-construct' ;
18
18
import { GraphqlOutput } from '@aws-amplify/backend-output-schemas' ;
19
+ import { generateModelsSync } from '@aws-amplify/graphql-generator' ;
19
20
import * as path from 'path' ;
20
21
import { AmplifyDataError , DataProps } from './types.js' ;
21
22
import {
@@ -45,6 +46,10 @@ import {
45
46
FunctionSchemaAccess ,
46
47
JsResolver ,
47
48
} 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' ;
48
53
49
54
/**
50
55
* Singleton factory for AmplifyGraphqlApi constructs that can be used in Amplify project files.
@@ -55,6 +60,8 @@ export class DataFactory implements ConstructFactory<AmplifyData> {
55
60
// publicly accessible for testing purpose only.
56
61
static factoryCount = 0 ;
57
62
63
+ readonly provides = 'DataResources' ;
64
+
58
65
private generator : ConstructContainerEntryGenerator ;
59
66
60
67
/**
@@ -231,14 +238,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
231
238
...schemasLambdaFunctions ,
232
239
} ) ;
233
240
let amplifyApi = undefined ;
241
+ let modelIntrospectionSchema : string | undefined = undefined ;
234
242
235
243
const isSandboxDeployment =
236
244
scope . node . tryGetContext ( CDKContextKey . DEPLOYMENT_TYPE ) === 'sandbox' ;
237
245
238
246
try {
247
+ const combinedSchema = combineCDKSchemas ( amplifyGraphqlDefinitions ) ;
248
+ modelIntrospectionSchema = generateModelsSync ( {
249
+ schema : combinedSchema . schema ,
250
+ target : 'introspection' ,
251
+ } ) [ 'model-introspection.json' ] ;
252
+
239
253
amplifyApi = new AmplifyData ( scope , this . name , {
240
254
apiName : this . name ,
241
- definition : combineCDKSchemas ( amplifyGraphqlDefinitions ) ,
255
+ definition : combinedSchema ,
242
256
authorizationModes,
243
257
outputStorageStrategy : this . outputStorageStrategy ,
244
258
functionNameMap,
@@ -263,6 +277,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
263
277
) ;
264
278
}
265
279
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
+
266
295
Tags . of ( amplifyApi ) . add ( TagName . FRIENDLY_NAME , this . name ) ;
267
296
268
297
/**;
@@ -279,10 +308,15 @@ class DataGenerator implements ConstructContainerEntryGenerator {
279
308
ssmEnvironmentEntriesGenerator . generateSsmEnvironmentEntries ( {
280
309
[ `${ this . name } _GRAPHQL_ENDPOINT` ] :
281
310
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 ,
282
315
} ) ;
283
316
284
317
const policyGenerator = new AppSyncPolicyGenerator (
285
- amplifyApi . resources . graphqlApi
318
+ amplifyApi . resources . graphqlApi ,
319
+ `${ modelIntrospectionSchemaBucket . bucketArn } /${ modelIntrospectionSchemaKey } `
286
320
) ;
287
321
288
322
schemasFunctionSchemaAccess . forEach ( ( accessDefinition ) => {
0 commit comments