1
- import { readFileSync } from 'fs' ;
2
1
import { IUserPool } from '@aws-cdk/aws-cognito' ;
3
2
import { ManagedPolicy , Role , ServicePrincipal , Grant , IGrantable } from '@aws-cdk/aws-iam' ;
4
3
import { CfnResource , Construct , Duration , IResolvable , Stack } from '@aws-cdk/core' ;
5
4
import { CfnApiKey , CfnGraphQLApi , CfnGraphQLSchema } from './appsync.generated' ;
6
5
import { IGraphqlApi , GraphqlApiBase } from './graphqlapi-base' ;
7
- import { ObjectType , ObjectTypeProps } from './schema-intermediate' ;
6
+ import { Schema } from './schema' ;
7
+ import { IIntermediateType } from './schema-base' ;
8
8
9
9
/**
10
10
* enum with all possible values for AppSync authorization type
@@ -201,21 +201,6 @@ export interface LogConfig {
201
201
readonly fieldLogLevel ?: FieldLogLevel ;
202
202
}
203
203
204
- /**
205
- * Enum containing the different modes of schema definition
206
- */
207
- export enum SchemaDefinition {
208
- /**
209
- * Define schema through functions like addType, addQuery, etc.
210
- */
211
- CODE = 'CODE' ,
212
-
213
- /**
214
- * Define schema in a file, i.e. schema.graphql
215
- */
216
- FILE = 'FILE' ,
217
- }
218
-
219
204
/**
220
205
* Properties for an AppSync GraphQL API
221
206
*/
@@ -242,18 +227,13 @@ export interface GraphQLApiProps {
242
227
/**
243
228
* GraphQL schema definition. Specify how you want to define your schema.
244
229
*
245
- * SchemaDefinition.CODE allows schema definition through CDK
246
- * SchemaDefinition.FILE allows schema definition through schema.graphql file
230
+ * Schema.fromFile(filePath: string) allows schema definition through schema.graphql file
247
231
*
248
- * @experimental
249
- */
250
- readonly schemaDefinition : SchemaDefinition ;
251
- /**
252
- * File containing the GraphQL schema definition. You have to specify a definition or a file containing one.
232
+ * @default - schema will be generated code-first (i.e. addType, addObjectType, etc.)
253
233
*
254
- * @default - Use schemaDefinition
234
+ * @experimental
255
235
*/
256
- readonly schemaDefinitionFile ?: string ;
236
+ readonly schema ?: Schema ;
257
237
/**
258
238
* A flag indicating whether or not X-Ray tracing is enabled for the GraphQL API.
259
239
*
@@ -390,9 +370,9 @@ export class GraphQLApi extends GraphqlApiBase {
390
370
public readonly name : string ;
391
371
392
372
/**
393
- * underlying CFN schema resource
373
+ * the schema attached to this api
394
374
*/
395
- public readonly schema : CfnGraphQLSchema ;
375
+ public readonly schema : Schema ;
396
376
397
377
/**
398
378
* the configured API key, if present
@@ -401,9 +381,9 @@ export class GraphQLApi extends GraphqlApiBase {
401
381
*/
402
382
public readonly apiKey ?: string ;
403
383
404
- private schemaMode : SchemaDefinition ;
384
+ private schemaResource : CfnGraphQLSchema ;
405
385
private api : CfnGraphQLApi ;
406
- private _apiKey ?: CfnApiKey ;
386
+ private apiKeyResource ?: CfnApiKey ;
407
387
408
388
constructor ( scope : Construct , id : string , props : GraphQLApiProps ) {
409
389
super ( scope , id ) ;
@@ -429,16 +409,16 @@ export class GraphQLApi extends GraphqlApiBase {
429
409
this . arn = this . api . attrArn ;
430
410
this . graphQlUrl = this . api . attrGraphQlUrl ;
431
411
this . name = this . api . name ;
432
- this . schemaMode = props . schemaDefinition ;
433
- this . schema = this . defineSchema ( props . schemaDefinitionFile ) ;
412
+ this . schema = props . schema ?? new Schema ( ) ;
413
+ this . schemaResource = this . schema . bind ( this ) ;
434
414
435
415
if ( modes . some ( ( mode ) => mode . authorizationType === AuthorizationType . API_KEY ) ) {
436
416
const config = modes . find ( ( mode : AuthorizationMode ) => {
437
417
return mode . authorizationType === AuthorizationType . API_KEY && mode . apiKeyConfig ;
438
418
} ) ?. apiKeyConfig ;
439
- this . _apiKey = this . createAPIKey ( config ) ;
440
- this . _apiKey . addDependsOn ( this . schema ) ;
441
- this . apiKey = this . _apiKey . attrApiKey ;
419
+ this . apiKeyResource = this . createAPIKey ( config ) ;
420
+ this . apiKeyResource . addDependsOn ( this . schemaResource ) ;
421
+ this . apiKey = this . apiKeyResource . attrApiKey ;
442
422
}
443
423
}
444
424
@@ -515,7 +495,7 @@ export class GraphQLApi extends GraphqlApiBase {
515
495
* @param construct the dependee
516
496
*/
517
497
public addSchemaDependency ( construct : CfnResource ) : boolean {
518
- construct . addDependsOn ( this . schema ) ;
498
+ construct . addDependsOn ( this . schemaResource ) ;
519
499
return true ;
520
500
}
521
501
@@ -584,29 +564,6 @@ export class GraphQLApi extends GraphqlApiBase {
584
564
} ) ;
585
565
}
586
566
587
- /**
588
- * Define schema based on props configuration
589
- * @param file the file name/s3 location of Schema
590
- */
591
- private defineSchema ( file ?: string ) : CfnGraphQLSchema {
592
- let definition ;
593
-
594
- if ( this . schemaMode === SchemaDefinition . FILE && ! file ) {
595
- throw new Error ( 'schemaDefinitionFile must be configured if using FILE definition mode.' ) ;
596
- } else if ( this . schemaMode === SchemaDefinition . FILE && file ) {
597
- definition = readFileSync ( file ) . toString ( 'utf-8' ) ;
598
- } else if ( this . schemaMode === SchemaDefinition . CODE && ! file ) {
599
- definition = '' ;
600
- } else if ( this . schemaMode === SchemaDefinition . CODE && file ) {
601
- throw new Error ( 'definition mode CODE is incompatible with file definition. Change mode to FILE/S3 or unconfigure schemaDefinitionFile' ) ;
602
- }
603
-
604
- return new CfnGraphQLSchema ( this , 'Schema' , {
605
- apiId : this . apiId ,
606
- definition,
607
- } ) ;
608
- }
609
-
610
567
/**
611
568
* Escape hatch to append to Schema as desired. Will always result
612
569
* in a newline.
@@ -617,31 +574,18 @@ export class GraphQLApi extends GraphqlApiBase {
617
574
*
618
575
* @experimental
619
576
*/
620
- public appendToSchema ( addition : string , delimiter ?: string ) : void {
621
- if ( this . schemaMode !== SchemaDefinition . CODE ) {
622
- throw new Error ( 'API cannot append to schema because schema definition mode is not configured as CODE.' ) ;
623
- }
624
- const sep = delimiter ?? '' ;
625
- this . schema . definition = `${ this . schema . definition } ${ sep } ${ addition } \n` ;
577
+ public addToSchema ( addition : string , delimiter ?: string ) : void {
578
+ this . schema . addToSchema ( addition , delimiter ) ;
626
579
}
627
580
628
581
/**
629
- * Add an object type to the schema
582
+ * Add type to the schema
630
583
*
631
- * @param name the name of the object type
632
- * @param props the definition
584
+ * @param type the intermediate type to add to the schema
633
585
*
634
586
* @experimental
635
587
*/
636
- public addType ( name : string , props : ObjectTypeProps ) : ObjectType {
637
- if ( this . schemaMode !== SchemaDefinition . CODE ) {
638
- throw new Error ( 'API cannot add type because schema definition mode is not configured as CODE.' ) ;
639
- } ;
640
- const type = new ObjectType ( name , {
641
- definition : props . definition ,
642
- directives : props . directives ,
643
- } ) ;
644
- this . appendToSchema ( type . toString ( ) ) ;
645
- return type ;
588
+ public addType ( type : IIntermediateType ) : IIntermediateType {
589
+ return this . schema . addType ( type ) ;
646
590
}
647
591
}
0 commit comments