From c3bc588d65d5ee75066b3487ed1d7c81ec85e821 Mon Sep 17 00:00:00 2001 From: Andres Koetsier Date: Thu, 11 Aug 2022 13:50:23 +0200 Subject: [PATCH 1/3] Expose the AppSyncDomain of the custom domain --- packages/@aws-cdk/aws-appsync/README.md | 2 +- packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts | 9 ++++++++- .../aws-appsync/test/appsync-domain.test.ts | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/README.md b/packages/@aws-cdk/aws-appsync/README.md index 32d44938c8eed..ab5e78014866a 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.domainNameAppSyncDomainName, }); ``` diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts index d384713310581..2fe1ca5f39935 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts @@ -470,6 +470,13 @@ export class GraphqlApi extends GraphqlApiBase { */ public readonly apiKey?: string; + /** + * the associated custom AppSync domain, if present + * + * @default - no custom associated domain + */ + public readonly domainNameAppSyncDomainName?: string; + /** * the CloudWatch Log Group for this API */ @@ -515,7 +522,7 @@ export class GraphqlApi extends GraphqlApiBase { certificateArn: props.domainName.certificate.certificateArn, description: `domain for ${this.name} at ${this.graphqlUrl}`, }); - + this.domainNameAppSyncDomainName = domainName.attrAppSyncDomainName; const domainNameAssociation = new CfnDomainNameApiAssociation(this, 'DomainAssociation', { domainName: props.domainName.domainName, apiId: this.apiId, 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..bd0f3504ee720 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,19 @@ describe('Tests of AppSync Domain Name', () => { }, ); }); + + test('domainNameAppSyncDomainName 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.domainNameAppSyncDomainName)).toEqual({ 'Fn::GetAtt': ['baseApiDomainName52E3D63D', 'AppSyncDomainName'] }); + }); }); From a251d8bc3a00318dc7ec7792fd6197de2e30d0b2 Mon Sep 17 00:00:00 2001 From: Andres Koetsier Date: Thu, 11 Aug 2022 15:43:14 +0200 Subject: [PATCH 2/3] Rename attribute and fix example --- packages/@aws-cdk/aws-appsync/README.md | 2 +- packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts | 4 ++-- packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/README.md b/packages/@aws-cdk/aws-appsync/README.md index ab5e78014866a..60e360f2312f5 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: api.domainNameAppSyncDomainName, + domainName: api.appSyncDomainName!, }); ``` diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts index 2fe1ca5f39935..2155e3a37b417 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts @@ -475,7 +475,7 @@ export class GraphqlApi extends GraphqlApiBase { * * @default - no custom associated domain */ - public readonly domainNameAppSyncDomainName?: string; + public readonly appSyncDomainName?: string; /** * the CloudWatch Log Group for this API @@ -522,7 +522,7 @@ export class GraphqlApi extends GraphqlApiBase { certificateArn: props.domainName.certificate.certificateArn, description: `domain for ${this.name} at ${this.graphqlUrl}`, }); - this.domainNameAppSyncDomainName = domainName.attrAppSyncDomainName; + this.appSyncDomainName = domainName.attrAppSyncDomainName; const domainNameAssociation = new CfnDomainNameApiAssociation(this, 'DomainAssociation', { domainName: props.domainName.domainName, apiId: this.apiId, 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 bd0f3504ee720..279a24e8be1d3 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts +++ b/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts @@ -53,6 +53,6 @@ describe('Tests of AppSync Domain Name', () => { }, }); - expect(stack.resolve(api.domainNameAppSyncDomainName)).toEqual({ 'Fn::GetAtt': ['baseApiDomainName52E3D63D', 'AppSyncDomainName'] }); + expect(stack.resolve(api.appSyncDomainName)).toEqual({ 'Fn::GetAtt': ['baseApiDomainName52E3D63D', 'AppSyncDomainName'] }); }); }); From 74c045ecb99bcc31f648420b68c6d7dcbb324f88 Mon Sep 17 00:00:00 2001 From: Andres Koetsier Date: Thu, 11 Aug 2022 18:03:35 +0200 Subject: [PATCH 3/3] Throw an error when no domainName is configured --- packages/@aws-cdk/aws-appsync/README.md | 2 +- .../@aws-cdk/aws-appsync/lib/graphqlapi.ts | 24 +++++++++++-------- .../aws-appsync/test/appsync-domain.test.ts | 13 +++++++++- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/README.md b/packages/@aws-cdk/aws-appsync/README.md index 60e360f2312f5..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: api.appSyncDomainName!, + domainName: api.appSyncDomainName, }); ``` diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts index 2155e3a37b417..5736f888fbf73 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts @@ -470,13 +470,6 @@ export class GraphqlApi extends GraphqlApiBase { */ public readonly apiKey?: string; - /** - * the associated custom AppSync domain, if present - * - * @default - no custom associated domain - */ - public readonly appSyncDomainName?: string; - /** * the CloudWatch Log Group for this API */ @@ -485,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); @@ -517,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}`, }); - this.appSyncDomainName = domainName.attrAppSyncDomainName; 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)) { @@ -781,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 279a24e8be1d3..ddc3041670fa0 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts +++ b/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts @@ -41,7 +41,7 @@ describe('Tests of AppSync Domain Name', () => { ); }); - test('domainNameAppSyncDomainName exposes the domain of the associated AWS::AppSync::DomainName', () => { + test('appSyncDomainName exposes the domain of the associated AWS::AppSync::DomainName', () => { const api = new appsync.GraphqlApi(stack, 'baseApi', { name: 'api', schema: appsync.Schema.fromAsset( @@ -55,4 +55,15 @@ describe('Tests of AppSync Domain Name', () => { 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'); + }); });