Skip to content

Commit

Permalink
feat(appsync): expose the AppSyncDomain of the custom domain of an Ap…
Browse files Browse the repository at this point in the history
…pSync api (#21554)

When configuring a custom domain on an AppSync api you couldn't easily get the AppSyncDomainName attribute of the associated AWS::AppSync::DomainName. This fix will expose that attribute so that the provided example doesn't throw errors.

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
AKoetsier authored Aug 11, 2022
1 parent c81068b commit d1097b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ const zone = route53.HostedZone.fromHostedZoneAttributes(this, `HostedZone`, {
new route53.CnameRecord(this, `CnameApiRecord`, {
recordName: 'api',
zone,
domainName: myDomainName,
domainName: api.appSyncDomainName,
});
```

Expand Down
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
}
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit d1097b5

Please sign in to comment.