Skip to content

Commit 0832428

Browse files
authored
feat: Add Environment Support (#628)
1 parent dbaf899 commit 0832428

File tree

12 files changed

+92
-21
lines changed

12 files changed

+92
-21
lines changed

doc/general-config.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ appSync:
4444
- `dataSources`: See [DataSources](dataSources.md)
4545
- `resolvers`: See [Resolvers](resolvers.md)
4646
- `pipelineFunctions`: See [Pipeline functions](pipeline-functions.md)
47-
- `substitutions`: See [Substitutions](substitutions.md)
47+
- `substitutions`: See [Substitutions](substitutions.md). Deprecated: Use environment variables.
48+
- `environment`: A list of environment variables for the API. See [Official Documentation](https://docs.aws.amazon.com/appsync/latest/devguide/environmental-variables.html)
4849
- `caching`: See [Cacing](caching.md)
4950
- `waf`: See [Web Application Firefall](WAF.md)
5051
- `logging`: See [Logging](#Logging)

doc/resolvers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ appSync:
2929
- `code`: The path of the JavaScript resolver handler file, relative to `serverless.yml`. If not specified, a [minimalistic default](#javascript-vs-vtl) is used.
3030
- `request`: The path to the VTL request mapping template file, relative to `serverless.yml`.
3131
- `response`: The path to the VTL response mapping template file, relative to `serverless.yml`.
32-
- `substitutions`: See [Variable Substitutions](substitutions.md)
32+
- `substitutions`: See [Variable Substitutions](substitutions.md). Deprecated: Use [environment variables](./general-config.md) instead.
3333
- `caching`: [See below](#Caching)
3434
- `sync`: [See SyncConfig](syncConfig.md)
3535

doc/substitutions.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> ⚠️ Substitutions are deprecated. Use [Environment Variables](./general-config.md) instead.
2+
13
# Substitutions
24

35
`Substitutions` is a feature that allows you to replace some variables in your VTL mapping templates or JS resolvers with dynamic values.
@@ -30,27 +32,29 @@ appSync:
3032
<details open>
3133
<summary>VTL mapping template</summary>
3234
33-
```vtl
34-
{
35-
"version" : "2018-05-29",
36-
"operation" : "BatchPutItem",
37-
"tables" : {
38-
"${postsTable}": [...]
39-
}
40-
}
41-
```
35+
```vtl
36+
{
37+
"version" : "2018-05-29",
38+
"operation" : "BatchPutItem",
39+
"tables" : {
40+
"${postsTable}": [...]
41+
}
42+
}
43+
```
44+
4245
</details>
4346

4447
<details open>
4548
<summary>JS Resolvers</summary>
46-
47-
```js
48-
const tableName = '#postsTable#';
49-
return {
50-
operation: "BatchGetItem",
51-
tables: {
52-
[tableName]: { keys },
53-
},
54-
};
55-
```
49+
50+
```js
51+
const tableName = '#postsTable#';
52+
return {
53+
operation: 'BatchGetItem',
54+
tables: {
55+
[tableName]: { keys },
56+
},
57+
};
58+
```
59+
5660
</details>

src/__tests__/__snapshots__/api.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Object {
2020
},
2121
],
2222
"AuthenticationType": "API_KEY",
23+
"EnvironmentVariables": undefined,
2324
"Name": "MyApi",
2425
"Tags": Array [
2526
Object {
@@ -47,6 +48,7 @@ Object {
4748
"GraphQlApi": Object {
4849
"Properties": Object {
4950
"AuthenticationType": "AWS_LAMBDA",
51+
"EnvironmentVariables": undefined,
5052
"LambdaAuthorizerConfig": Object {
5153
"AuthorizerResultTtlInSeconds": undefined,
5254
"AuthorizerUri": Object {

src/__tests__/api.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('Api', () => {
1515
"GraphQlApi": Object {
1616
"Properties": Object {
1717
"AuthenticationType": "API_KEY",
18+
"EnvironmentVariables": undefined,
1819
"Name": "MyApi",
1920
"Tags": Array [
2021
Object {
@@ -42,6 +43,7 @@ describe('Api', () => {
4243
"GraphQlApi": Object {
4344
"Properties": Object {
4445
"AuthenticationType": "API_KEY",
46+
"EnvironmentVariables": undefined,
4547
"Name": "MyApi",
4648
"Tags": Array [
4749
Object {
@@ -72,6 +74,7 @@ describe('Api', () => {
7274
"GraphQlApi": Object {
7375
"Properties": Object {
7476
"AuthenticationType": "API_KEY",
77+
"EnvironmentVariables": undefined,
7578
"IntrospectionConfig": "DISABLED",
7679
"Name": "MyApi",
7780
"QueryDepthLimit": 10,
@@ -90,6 +93,44 @@ describe('Api', () => {
9093
`);
9194
});
9295

96+
it('should compile the Api Resource with Environments', () => {
97+
const api = new Api(
98+
given.appSyncConfig({
99+
environment: {
100+
TABLE_NAME: 'MyTable',
101+
OTHER_TABLE: {
102+
Ref: 'OtherTable',
103+
},
104+
},
105+
}),
106+
plugin,
107+
);
108+
expect(api.compileEndpoint()).toMatchInlineSnapshot(`
109+
Object {
110+
"GraphQlApi": Object {
111+
"Properties": Object {
112+
"AuthenticationType": "API_KEY",
113+
"EnvironmentVariables": Object {
114+
"OTHER_TABLE": Object {
115+
"Ref": "OtherTable",
116+
},
117+
"TABLE_NAME": "MyTable",
118+
},
119+
"Name": "MyApi",
120+
"Tags": Array [
121+
Object {
122+
"Key": "stage",
123+
"Value": "Dev",
124+
},
125+
],
126+
"XrayEnabled": false,
127+
},
128+
"Type": "AWS::AppSync::GraphQLApi",
129+
},
130+
}
131+
`);
132+
});
133+
93134
it('should compile the Api Resource with logs enabled', () => {
94135
const api = new Api(
95136
given.appSyncConfig({
@@ -106,6 +147,7 @@ describe('Api', () => {
106147
"GraphQlApi": Object {
107148
"Properties": Object {
108149
"AuthenticationType": "API_KEY",
150+
"EnvironmentVariables": undefined,
109151
"LogConfig": Object {
110152
"CloudWatchLogsRoleArn": Object {
111153
"Fn::GetAtt": Array [
@@ -215,6 +257,7 @@ describe('Api', () => {
215257
},
216258
],
217259
"AuthenticationType": "AMAZON_COGNITO_USER_POOLS",
260+
"EnvironmentVariables": undefined,
218261
"Name": "MyApi",
219262
"Tags": Array [
220263
Object {

src/__tests__/validation/__snapshots__/base.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ exports[`Valdiation should validate 1`] = `
6060
/introspection: must be boolean
6161
/queryDepthLimit: must be integer
6262
/resolverCountLimit: must be integer
63+
/environment: must be a valid environment definition
6364
/esbuild: must be an esbuild config object or false"
6465
`;
6566

src/__tests__/validation/base.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ describe('Valdiation', () => {
1212
queryDepthLimit: 10,
1313
resolverCountLimit: 10,
1414
xrayEnabled: true,
15+
environment: {
16+
MY_TABLE: 'my-table',
17+
MY_OTHER_TABLE: { Ref: 'MyOtherTable' },
18+
},
1519
tags: {
1620
foo: 'bar',
1721
},
@@ -32,6 +36,7 @@ describe('Valdiation', () => {
3236
xrayEnabled: 'BAR',
3337
unknownPorp: 'foo',
3438
esbuild: 'bad',
39+
environment: 'Bad',
3540
});
3641
}).toThrowErrorMatchingSnapshot();
3742

src/resources/Api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class Api {
7676
Name: this.config.name,
7777
XrayEnabled: this.config.xrayEnabled || false,
7878
Tags: this.getTagsConfig(),
79+
EnvironmentVariables: this.config.environment,
7980
},
8081
};
8182

src/types/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export type SyncConfig = {
128128
} & LambdaConfig;
129129

130130
export type Substitutions = Record<string, string | IntrinsicFunction>;
131+
export type EnvironmentVariables = Record<string, string | IntrinsicFunction>;
131132

132133
export type DsDynamoDBConfig = {
133134
type: 'AMAZON_DYNAMODB';

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
DsOpenSearchConfig,
1616
DsRelationalDbConfig,
1717
SyncConfig,
18+
EnvironmentVariables,
1819
} from './common';
1920
export * from './common';
2021

@@ -33,6 +34,7 @@ export type AppSyncConfig = {
3334
| Record<string, DataSourceConfig>[]
3435
| Record<string, DataSourceConfig>;
3536
substitutions?: Substitutions;
37+
environment?: EnvironmentVariables;
3638
xrayEnabled?: boolean;
3739
logging?: LoggingConfig;
3840
caching?: CachingConfig;

0 commit comments

Comments
 (0)