Skip to content

Commit

Permalink
feat: Add Environment Support (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure authored Feb 11, 2024
1 parent dbaf899 commit 0832428
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 21 deletions.
3 changes: 2 additions & 1 deletion doc/general-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ appSync:
- `dataSources`: See [DataSources](dataSources.md)
- `resolvers`: See [Resolvers](resolvers.md)
- `pipelineFunctions`: See [Pipeline functions](pipeline-functions.md)
- `substitutions`: See [Substitutions](substitutions.md)
- `substitutions`: See [Substitutions](substitutions.md). Deprecated: Use environment variables.
- `environment`: A list of environment variables for the API. See [Official Documentation](https://docs.aws.amazon.com/appsync/latest/devguide/environmental-variables.html)
- `caching`: See [Cacing](caching.md)
- `waf`: See [Web Application Firefall](WAF.md)
- `logging`: See [Logging](#Logging)
Expand Down
2 changes: 1 addition & 1 deletion doc/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ appSync:
- `code`: The path of the JavaScript resolver handler file, relative to `serverless.yml`. If not specified, a [minimalistic default](#javascript-vs-vtl) is used.
- `request`: The path to the VTL request mapping template file, relative to `serverless.yml`.
- `response`: The path to the VTL response mapping template file, relative to `serverless.yml`.
- `substitutions`: See [Variable Substitutions](substitutions.md)
- `substitutions`: See [Variable Substitutions](substitutions.md). Deprecated: Use [environment variables](./general-config.md) instead.
- `caching`: [See below](#Caching)
- `sync`: [See SyncConfig](syncConfig.md)

Expand Down
42 changes: 23 additions & 19 deletions doc/substitutions.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> ⚠️ Substitutions are deprecated. Use [Environment Variables](./general-config.md) instead.
# Substitutions

`Substitutions` is a feature that allows you to replace some variables in your VTL mapping templates or JS resolvers with dynamic values.
Expand Down Expand Up @@ -30,27 +32,29 @@ appSync:
<details open>
<summary>VTL mapping template</summary>
```vtl
{
"version" : "2018-05-29",
"operation" : "BatchPutItem",
"tables" : {
"${postsTable}": [...]
}
}
```
```vtl
{
"version" : "2018-05-29",
"operation" : "BatchPutItem",
"tables" : {
"${postsTable}": [...]
}
}
```

</details>

<details open>
<summary>JS Resolvers</summary>

```js
const tableName = '#postsTable#';
return {
operation: "BatchGetItem",
tables: {
[tableName]: { keys },
},
};
```

```js
const tableName = '#postsTable#';
return {
operation: 'BatchGetItem',
tables: {
[tableName]: { keys },
},
};
```

</details>
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Object {
},
],
"AuthenticationType": "API_KEY",
"EnvironmentVariables": undefined,
"Name": "MyApi",
"Tags": Array [
Object {
Expand Down Expand Up @@ -47,6 +48,7 @@ Object {
"GraphQlApi": Object {
"Properties": Object {
"AuthenticationType": "AWS_LAMBDA",
"EnvironmentVariables": undefined,
"LambdaAuthorizerConfig": Object {
"AuthorizerResultTtlInSeconds": undefined,
"AuthorizerUri": Object {
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Api', () => {
"GraphQlApi": Object {
"Properties": Object {
"AuthenticationType": "API_KEY",
"EnvironmentVariables": undefined,
"Name": "MyApi",
"Tags": Array [
Object {
Expand Down Expand Up @@ -42,6 +43,7 @@ describe('Api', () => {
"GraphQlApi": Object {
"Properties": Object {
"AuthenticationType": "API_KEY",
"EnvironmentVariables": undefined,
"Name": "MyApi",
"Tags": Array [
Object {
Expand Down Expand Up @@ -72,6 +74,7 @@ describe('Api', () => {
"GraphQlApi": Object {
"Properties": Object {
"AuthenticationType": "API_KEY",
"EnvironmentVariables": undefined,
"IntrospectionConfig": "DISABLED",
"Name": "MyApi",
"QueryDepthLimit": 10,
Expand All @@ -90,6 +93,44 @@ describe('Api', () => {
`);
});

it('should compile the Api Resource with Environments', () => {
const api = new Api(
given.appSyncConfig({
environment: {
TABLE_NAME: 'MyTable',
OTHER_TABLE: {
Ref: 'OtherTable',
},
},
}),
plugin,
);
expect(api.compileEndpoint()).toMatchInlineSnapshot(`
Object {
"GraphQlApi": Object {
"Properties": Object {
"AuthenticationType": "API_KEY",
"EnvironmentVariables": Object {
"OTHER_TABLE": Object {
"Ref": "OtherTable",
},
"TABLE_NAME": "MyTable",
},
"Name": "MyApi",
"Tags": Array [
Object {
"Key": "stage",
"Value": "Dev",
},
],
"XrayEnabled": false,
},
"Type": "AWS::AppSync::GraphQLApi",
},
}
`);
});

it('should compile the Api Resource with logs enabled', () => {
const api = new Api(
given.appSyncConfig({
Expand All @@ -106,6 +147,7 @@ describe('Api', () => {
"GraphQlApi": Object {
"Properties": Object {
"AuthenticationType": "API_KEY",
"EnvironmentVariables": undefined,
"LogConfig": Object {
"CloudWatchLogsRoleArn": Object {
"Fn::GetAtt": Array [
Expand Down Expand Up @@ -215,6 +257,7 @@ describe('Api', () => {
},
],
"AuthenticationType": "AMAZON_COGNITO_USER_POOLS",
"EnvironmentVariables": undefined,
"Name": "MyApi",
"Tags": Array [
Object {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/validation/__snapshots__/base.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ exports[`Valdiation should validate 1`] = `
/introspection: must be boolean
/queryDepthLimit: must be integer
/resolverCountLimit: must be integer
/environment: must be a valid environment definition
/esbuild: must be an esbuild config object or false"
`;

Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/validation/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe('Valdiation', () => {
queryDepthLimit: 10,
resolverCountLimit: 10,
xrayEnabled: true,
environment: {
MY_TABLE: 'my-table',
MY_OTHER_TABLE: { Ref: 'MyOtherTable' },
},
tags: {
foo: 'bar',
},
Expand All @@ -32,6 +36,7 @@ describe('Valdiation', () => {
xrayEnabled: 'BAR',
unknownPorp: 'foo',
esbuild: 'bad',
environment: 'Bad',
});
}).toThrowErrorMatchingSnapshot();

Expand Down
1 change: 1 addition & 0 deletions src/resources/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class Api {
Name: this.config.name,
XrayEnabled: this.config.xrayEnabled || false,
Tags: this.getTagsConfig(),
EnvironmentVariables: this.config.environment,
},
};

Expand Down
1 change: 1 addition & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export type SyncConfig = {
} & LambdaConfig;

export type Substitutions = Record<string, string | IntrinsicFunction>;
export type EnvironmentVariables = Record<string, string | IntrinsicFunction>;

export type DsDynamoDBConfig = {
type: 'AMAZON_DYNAMODB';
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DsOpenSearchConfig,
DsRelationalDbConfig,
SyncConfig,
EnvironmentVariables,
} from './common';
export * from './common';

Expand All @@ -33,6 +34,7 @@ export type AppSyncConfig = {
| Record<string, DataSourceConfig>[]
| Record<string, DataSourceConfig>;
substitutions?: Substitutions;
environment?: EnvironmentVariables;
xrayEnabled?: boolean;
logging?: LoggingConfig;
caching?: CachingConfig;
Expand Down
2 changes: 2 additions & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DsEventBridgeConfig,
DsNone,
Substitutions,
EnvironmentVariables,
} from './common';
export * from './common';

Expand All @@ -29,6 +30,7 @@ export type AppSyncConfig = {
resolvers: Record<string, ResolverConfig>;
pipelineFunctions: Record<string, PipelineFunctionConfig>;
substitutions?: Substitutions;
environment?: EnvironmentVariables;
xrayEnabled?: boolean;
logging?: LoggingConfig;
caching?: CachingConfig;
Expand Down
9 changes: 9 additions & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ export const appSyncSchema = {
required: [],
errorMessage: 'must be a valid substitutions definition',
},
environment: {
type: 'object',
additionalProperties: {
$ref: '#/definitions/stringOrIntrinsicFunction',
},
required: [],
errorMessage: 'must be a valid environment definition',
},
dataSource: {
if: { type: 'object' },
then: { $ref: '#/definitions/dataSourceConfig' },
Expand Down Expand Up @@ -687,6 +695,7 @@ export const appSyncSchema = {
queryDepthLimit: { type: 'integer', minimum: 1, maximum: 75 },
resolverCountLimit: { type: 'integer', minimum: 1, maximum: 1000 },
substitutions: { $ref: '#/definitions/substitutions' },
environment: { $ref: '#/definitions/environment' },
waf: {
type: 'object',
properties: {
Expand Down

0 comments on commit 0832428

Please sign in to comment.