Skip to content

Commit 2ba971d

Browse files
authored
Fix amplifyApiEnvironmentName in non sandbox environments (#2730)
1 parent 91e353d commit 2ba971d

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

.changeset/cool-olives-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-data': patch
3+
---
4+
5+
Fix amplifyApiEnvironmentName in non sandbox environments

packages/backend-data/src/convert_js_resolvers.test.ts

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ const testSchema = /* GraphQL */ `
2424
}
2525
`;
2626

27-
const createStackAndSetContext = (): Stack => {
27+
const createStackAndSetContext = (backendType: 'sandbox' | 'branch'): Stack => {
2828
const app = new App();
2929
app.node.setContext('amplify-backend-name', 'testEnvName');
3030
app.node.setContext('amplify-backend-namespace', 'testBackendId');
31-
app.node.setContext('amplify-backend-type', 'branch');
31+
app.node.setContext('amplify-backend-type', backendType);
3232
const stack = new Stack(app);
3333
return stack;
3434
};
@@ -60,13 +60,13 @@ void describe('defaultJsResolverCode', () => {
6060
});
6161
});
6262

63-
void describe('convertJsResolverDefinition', () => {
63+
void describe('convertJsResolverDefinition - branch deployment', () => {
6464
let stack: Stack;
6565
let amplifyApi: AmplifyData;
6666
const authorizationModes = { apiKeyConfig: { expires: Duration.days(7) } };
6767

6868
void beforeEach(() => {
69-
stack = createStackAndSetContext();
69+
stack = createStackAndSetContext('branch');
7070
amplifyApi = new AmplifyData(stack, 'amplifyData', {
7171
apiName: 'amplifyData',
7272
definition: AmplifyDataDefinition.fromString(testSchema),
@@ -191,6 +191,69 @@ void describe('convertJsResolverDefinition', () => {
191191
template.resourceCountIs('AWS::AppSync::Resolver', 1);
192192
});
193193

194+
void it('adds api id and environment name to stash for non-sandbox deployment', () => {
195+
const absolutePath = resolve(
196+
fileURLToPath(import.meta.url),
197+
'../../lib/assets',
198+
'js_resolver_handler.js',
199+
);
200+
201+
const schema = a.schema({
202+
customQuery: a
203+
.query()
204+
.authorization((allow) => allow.publicApiKey())
205+
.returns(a.string())
206+
.handler(
207+
a.handler.custom({
208+
entry: absolutePath,
209+
}),
210+
),
211+
});
212+
const { jsFunctions } = schema.transform();
213+
convertJsResolverDefinition(stack, amplifyApi, jsFunctions);
214+
215+
const template = Template.fromStack(stack);
216+
template.hasResourceProperties('AWS::AppSync::Resolver', {
217+
Runtime: {
218+
Name: 'APPSYNC_JS',
219+
RuntimeVersion: '1.0.0',
220+
},
221+
Kind: 'PIPELINE',
222+
TypeName: 'Query',
223+
FieldName: 'customQuery',
224+
Code: {
225+
'Fn::Join': [
226+
'',
227+
[
228+
"/**\n * Pipeline resolver request handler\n */\nexport const request = (ctx) => {\n ctx.stash.awsAppsyncApiId = '",
229+
{
230+
'Fn::GetAtt': [
231+
Match.stringLikeRegexp('amplifyDataGraphQLAPI.*'),
232+
'ApiId',
233+
],
234+
},
235+
"';\n ctx.stash.amplifyApiEnvironmentName = 'testEnvName';\n return {};\n};\n/**\n * Pipeline resolver response handler\n */\nexport const response = (ctx) => {\n return ctx.prev.result;\n};\n",
236+
],
237+
],
238+
},
239+
});
240+
});
241+
});
242+
243+
void describe('convertJsResolverDefinition - sandbox deployment', () => {
244+
let stack: Stack;
245+
let amplifyApi: AmplifyData;
246+
const authorizationModes = { apiKeyConfig: { expires: Duration.days(7) } };
247+
248+
void beforeEach(() => {
249+
stack = createStackAndSetContext('sandbox');
250+
amplifyApi = new AmplifyData(stack, 'amplifyData', {
251+
apiName: 'amplifyData',
252+
definition: AmplifyDataDefinition.fromString(testSchema),
253+
authorizationModes,
254+
});
255+
});
256+
194257
void it('adds api id and environment name to stash', () => {
195258
const absolutePath = resolve(
196259
fileURLToPath(import.meta.url),

packages/backend-data/src/convert_js_resolvers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url';
77
import { readFileSync } from 'fs';
88
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
99
import { resolveEntryPath } from './resolve_entry_path.js';
10+
import { CDKContextKey } from '@aws-amplify/platform-core';
1011

1112
const APPSYNC_PIPELINE_RESOLVER = 'PIPELINE';
1213
const APPSYNC_JS_RUNTIME_NAME = 'APPSYNC_JS';
@@ -78,8 +79,11 @@ export const convertJsResolverDefinition = (
7879

7980
const resolverName = `Resolver_${resolver.typeName}_${resolver.fieldName}`;
8081

81-
const amplifyApiEnvironmentName =
82-
scope.node.tryGetContext('amplifyEnvironmentName') ?? 'NONE';
82+
const isSandboxDeployment =
83+
scope.node.tryGetContext(CDKContextKey.DEPLOYMENT_TYPE) === 'sandbox';
84+
const amplifyApiEnvironmentName = isSandboxDeployment
85+
? 'NONE'
86+
: scope.node.tryGetContext(CDKContextKey.BACKEND_NAME);
8387
new CfnResolver(scope, resolverName, {
8488
apiId: amplifyApi.apiId,
8589
fieldName: resolver.fieldName,

0 commit comments

Comments
 (0)