Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying a custom Logical ID for the AWS::AppSync::GraphQLApi resource #618

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/general-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/resources/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions src/resources/Naming.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -104,6 +106,6 @@ export class Naming {
}

getAuthenticationEmbeddedLamdbaName() {
return `${this.apiName}Authorizer`;
return `${this.config.name}Authorizer`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Unless I am missing something, we probably could get away with having a
apiLogicalId that overrides getLogicalId.

The rest of the logical ids should be safe. i.e. They might be re-created but they are not stateful.

That would simplify everything.

}
}
1 change: 1 addition & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type AppSyncConfig = {
pipelineFunctions: Record<string, PipelineFunctionConfig>;
substitutions?: Substitutions;
xrayEnabled?: boolean;
logicalIdPrefix?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my other comment, could this be named apiLogicalId?

logging?: LoggingConfig;
caching?: CachingConfig;
waf?: WafConfig;
Expand Down
1 change: 1 addition & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down