diff --git a/packages/@aws-cdk/aws-appsync/README.md b/packages/@aws-cdk/aws-appsync/README.md index 32d44938c8eed..1b2717610eb4f 100644 --- a/packages/@aws-cdk/aws-appsync/README.md +++ b/packages/@aws-cdk/aws-appsync/README.md @@ -330,7 +330,7 @@ const zone = route53.HostedZone.fromHostedZoneAttributes(this, `HostedZone`, { new route53.CnameRecord(this, `CnameApiRecord`, { recordName: 'api', zone, - domainName: myDomainName, + domainName: api.appSyncDomainName, }); ``` diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts index d384713310581..5736f888fbf73 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts @@ -478,6 +478,7 @@ export class GraphqlApi extends GraphqlApiBase { private schemaResource: CfnGraphQLSchema; private api: CfnGraphQLApi; private apiKeyResource?: CfnApiKey; + private domainNameResource?: CfnDomainName; constructor(scope: Construct, id: string, props: GraphqlApiProps) { super(scope, id); @@ -510,18 +511,17 @@ export class GraphqlApi extends GraphqlApiBase { this.schemaResource = this.schema.bind(this); if (props.domainName) { - const domainName = new CfnDomainName(this, 'DomainName', { + this.domainNameResource = new CfnDomainName(this, 'DomainName', { domainName: props.domainName.domainName, certificateArn: props.domainName.certificate.certificateArn, description: `domain for ${this.name} at ${this.graphqlUrl}`, }); - const domainNameAssociation = new CfnDomainNameApiAssociation(this, 'DomainAssociation', { domainName: props.domainName.domainName, apiId: this.apiId, }); - domainNameAssociation.addDependsOn(domainName); + domainNameAssociation.addDependsOn(this.domainNameResource); } if (modes.some((mode) => mode.authorizationType === AuthorizationType.API_KEY)) { @@ -774,4 +774,15 @@ export class GraphqlApi extends GraphqlApiBase { public addSubscription(fieldName: string, field: ResolvableField): ObjectType { return this.schema.addSubscription(fieldName, field); } + + + /** + * The AppSyncDomainName of the associated custom domain + */ + public get appSyncDomainName(): string { + if (!this.domainNameResource) { + throw new Error('Cannot retrieve the appSyncDomainName without a domainName configuration'); + } + return this.domainNameResource.attrAppSyncDomainName; + } } diff --git a/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts b/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts index 6e445085e4264..ddc3041670fa0 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts +++ b/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts @@ -40,4 +40,30 @@ describe('Tests of AppSync Domain Name', () => { }, ); }); + + test('appSyncDomainName exposes the domain of the associated AWS::AppSync::DomainName', () => { + const api = new appsync.GraphqlApi(stack, 'baseApi', { + name: 'api', + schema: appsync.Schema.fromAsset( + path.join(__dirname, 'appsync.test.graphql'), + ), + domainName: { + certificate, + domainName: 'aws.amazon.com', + }, + }); + + expect(stack.resolve(api.appSyncDomainName)).toEqual({ 'Fn::GetAtt': ['baseApiDomainName52E3D63D', 'AppSyncDomainName'] }); + }); + + test('appSyncDomainName should throw an error when no custom domain has been configured', () => { + const api = new appsync.GraphqlApi(stack, 'baseApi', { + name: 'api', + schema: appsync.Schema.fromAsset( + path.join(__dirname, 'appsync.test.graphql'), + ), + }); + + expect(() => api.appSyncDomainName).toThrow('Cannot retrieve the appSyncDomainName without a domainName configuration'); + }); });