diff --git a/doc/general-config.md b/doc/general-config.md index e91972d3..9ef60380 100644 --- a/doc/general-config.md +++ b/doc/general-config.md @@ -48,6 +48,7 @@ appSync: - `caching`: See [Cacing](caching.md) - `waf`: See [Web Application Firefall](WAF.md) - `logging`: See [Logging](#Logging) +- `logicalIdPrefix`: Optional string. Used to prefix the generated CloudFormation Logical ID for the `AWS::AppSync::GraphQLApi` and related resources - `xrayEnabled`: Boolean. Enable or disable X-Ray tracing. - `visibility`: Optional. `GLOBAL` or `PRIVATE`. **Changing this value requires the replacement of the API.** - `tags`: A key-value pair for tagging this AppSync API diff --git a/src/__tests__/api.test.ts b/src/__tests__/api.test.ts index c8389f11..84fd6600 100644 --- a/src/__tests__/api.test.ts +++ b/src/__tests__/api.test.ts @@ -30,6 +30,36 @@ describe('Api', () => { `); }); + describe('logicalId', () => { + const logicalIdPrefix = 'Logicalidprefix'; + it('should override the logical ID if provided', () => { + const api = new Api( + given.appSyncConfig({ + logicalIdPrefix, + }), + plugin, + ); + expect(api.compileEndpoint()).toMatchInlineSnapshot(` + Object { + "${logicalIdPrefix}GraphQlApi": Object { + "Properties": Object { + "AuthenticationType": "API_KEY", + "Name": "MyApi", + "Tags": Array [ + Object { + "Key": "stage", + "Value": "Dev", + }, + ], + "XrayEnabled": false, + }, + "Type": "AWS::AppSync::GraphQLApi", + }, + } + `); + }); + }); + it('should compile the Api Resource for a private endpoint', () => { const api = new Api( given.appSyncConfig({ diff --git a/src/index.ts b/src/index.ts index 6d93c2ea..c9b4b7c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -958,7 +958,7 @@ class ServerlessAppsyncPlugin { } } const config = getAppSyncConfig(appSync); - this.naming = new Naming(appSync.name); + this.naming = new Naming(config); this.api = new Api(config, this); } diff --git a/src/resources/Api.ts b/src/resources/Api.ts index 95e41923..eadb0184 100644 --- a/src/resources/Api.ts +++ b/src/resources/Api.ts @@ -34,7 +34,7 @@ export class Api { public config: AppSyncConfig, public plugin: ServerlessAppsyncPlugin, ) { - this.naming = new Naming(this.config.name); + this.naming = new Naming(this.config); } compile() { diff --git a/src/resources/Naming.ts b/src/resources/Naming.ts index d3b3cacb..9b18057f 100644 --- a/src/resources/Naming.ts +++ b/src/resources/Naming.ts @@ -1,18 +1,20 @@ import { + AppSyncConfig, DataSourceConfig, PipelineFunctionConfig, ResolverConfig, } from '../types/plugin'; export class Naming { - constructor(private apiName: string) {} + constructor(private config: AppSyncConfig) {} getCfnName(name: string) { return name.replace(/[^a-zA-Z0-9]/g, ''); } getLogicalId(name: string): string { - return this.getCfnName(name); + const logicalIdPrefix = this.config.logicalIdPrefix || ''; + return this.getCfnName(`${logicalIdPrefix}${name}`); } getApiLogicalId() { @@ -66,7 +68,7 @@ export class Naming { // Warning: breaking change. // api name added getDataSourceLogicalId(name: string) { - return `GraphQlDs${this.getLogicalId(name)}`; + return this.getLogicalId(`GraphQlDs${name}`); } getDataSourceRoleLogicalId(name: string) { @@ -104,6 +106,6 @@ export class Naming { } getAuthenticationEmbeddedLamdbaName() { - return `${this.apiName}Authorizer`; + return `${this.config.name}Authorizer`; } } diff --git a/src/types/plugin.ts b/src/types/plugin.ts index ea56c8b5..1b528a11 100644 --- a/src/types/plugin.ts +++ b/src/types/plugin.ts @@ -12,6 +12,7 @@ export type AppSyncConfig = { pipelineFunctions: Record; substitutions?: Substitutions; xrayEnabled?: boolean; + logicalIdPrefix?: string; logging?: LoggingConfig; caching?: CachingConfig; waf?: WafConfig; diff --git a/src/validation.ts b/src/validation.ts index a1870492..0e770fc8 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -677,6 +677,7 @@ export const appSyncSchema = { 'when using CloudFormation, you must provide either certificateArn or hostedZoneId.', }, }, + logicalIdPrefix: { type: 'string' }, xrayEnabled: { type: 'boolean' }, visibility: { type: 'string',