Skip to content

Commit 06de3f3

Browse files
committed
fix(amplify-graphql-api-construct-tests): pin cdk init CLI and type e2e lambda scaffolds
Fixes the deterministic custom_query_mutation_extension and admin_role e2e failures. The e2e scratch-project scaffolder pinned aws-cdk-lib but ran `npx cdk init` with a floating CLI. The upstream template has since changed the cdk.json synth command from `npx ts-node --prefer-ts-exts bin/app.ts` to `npx tsc && npx tsx bin/app.ts` on TypeScript ~7.0 with strict/noImplicitAny and no tsconfig include, so synth now begins with a whole-project typecheck of every .ts in the scratch project. Backend templates are copied wholesale into bin/, including lambda entry points that are only ever referenced by esbuild as a path string and never imported by app.ts. Those files are now typechecked from a directory they were never written to resolve from, failing before synth: - custom-query-mutation-extension/authorizer.ts:1:26 TS7006 (untyped event) - admin-role/apiInvoker.ts:6:51 TS2307 ('../../../lambda-request' escapes the project) - Pin the aws-cdk CLI so the template cannot drift again. The CLI and aws-cdk-lib have used separate version lines since CLI v2.1000.0, so the CLI is pinned to its own constant rather than to cdkVersion (no aws-cdk release matches aws-cdk-lib 2.260.0, and cdkVersion may legitimately be 'latest'). - Drop the whole-project typecheck from the generated synth command, restoring the historical behavior where synth only loads the app's import graph. The runtime invocation is unchanged. - Type the authorizer handler event, and inline the response type in apiInvoker.ts so the bundled lambda entry is self-contained. All previously exported names remain exported.
1 parent 2f81117 commit 06de3f3

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

packages/amplify-graphql-api-construct-tests/src/__tests__/backends/admin-role/apiInvoker.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,24 @@ import { defaultProvider } from '@aws-sdk/credential-provider-node';
33
import { SignatureV4 } from '@aws-sdk/signature-v4';
44
import { HttpRequest } from '@aws-sdk/protocol-http';
55
import { default as fetch, Request } from 'node-fetch';
6-
import type { GraphqlProxiedLambdaResponse } from '../../../lambda-request';
6+
7+
/**
8+
* Shape of the response this lambda returns to callers.
9+
*
10+
* Intentionally declared inline rather than imported from the test helpers: this file is bundled as a standalone lambda entry point and is
11+
* copied into a scratch CDK project, so any relative import reaching outside its own directory cannot be resolved.
12+
*/
13+
export type GraphqlProxiedLambdaResponse<ResponseDataType> = {
14+
statusCode: number;
15+
body: {
16+
errors: Array<{
17+
status?: number;
18+
message: string;
19+
stack: string[];
20+
}>;
21+
data: ResponseDataType;
22+
};
23+
};
724

825
if (!process.env.GRAPHQL_URL) throw new Error('GRAPHQL_URL not found in environment variables');
926
const graphqlEndpoint = new URL(process.env.GRAPHQL_URL);

packages/amplify-graphql-api-construct-tests/src/__tests__/backends/custom-query-mutation-extension/authorizer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
exports.handler = async (event) => {
1+
type CustomAuthorizerEvent = {
2+
authorizationToken?: string;
3+
};
4+
5+
exports.handler = async (event: CustomAuthorizerEvent) => {
26
const { authorizationToken } = event;
37
const response = {
48
isAuthorized: authorizationToken === 'custom-authorized',

packages/amplify-graphql-api-construct-tests/src/commands.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,38 @@ const appendToCDKContext = (projectPath: string, additionalContext: Record<strin
6565
writeFileSync(cdkJsonPath, JSON.stringify(cdkJson, null, 2));
6666
};
6767

68+
/**
69+
* Pinned version of the `aws-cdk` CLI used to scaffold e2e test projects.
70+
*
71+
* The CLI must be pinned independently of `aws-cdk-lib`: the two have used separate version lines since CLI v2.1000.0, so there is no
72+
* `aws-cdk` release matching a modern `aws-cdk-lib` version. Leaving the CLI floating means `cdk init` silently picks up upstream template
73+
* changes, which has broken e2e groups before (the template switched the synth command from `ts-node` to `tsc && tsx`, turning synth into a
74+
* whole-project typecheck).
75+
*/
76+
const CDK_CLI_VERSION = '2.1134.0';
77+
78+
/**
79+
* Removes the whole-project `tsc` typecheck from the generated `cdk.json` synth command.
80+
*
81+
* Backend templates are copied wholesale into the scratch project's `bin/` directory, and some of those files are lambda entry points that
82+
* are only ever referenced by esbuild as a path string -- they are never imported by `app.ts`. A whole-project typecheck compiles them
83+
* anyway, in a directory they were never written to resolve from, failing synth before it starts. Transpiling only the import graph (the
84+
* historical behavior) keeps synth scoped to code the app actually loads.
85+
*/
86+
const removeWholeProjectTypecheckFromSynth = (projectPath: string): void => {
87+
const cdkJsonPath = path.join(projectPath, 'cdk.json');
88+
const cdkJson = JSON.parse(readFileSync(cdkJsonPath, 'utf-8'));
89+
if (typeof cdkJson.app !== 'string') {
90+
return;
91+
}
92+
const appWithoutTypecheck = cdkJson.app.replace(/^\s*npx\s+tsc\s*&&\s*/, '');
93+
if (appWithoutTypecheck === cdkJson.app) {
94+
return;
95+
}
96+
cdkJson.app = appWithoutTypecheck;
97+
writeFileSync(cdkJsonPath, JSON.stringify(cdkJson, null, 2));
98+
};
99+
68100
export type InitCDKProjectProps = {
69101
construct?: CdkConstruct;
70102
cdkContext?: Record<string, string>;
@@ -82,7 +114,7 @@ export type InitCDKProjectProps = {
82114
export const initCDKProject = async (cwd: string, templatePath: string, props?: InitCDKProjectProps): Promise<string> => {
83115
const { cdkVersion = '2.260.0', additionalDependencies = [] } = props ?? {};
84116

85-
await spawn(getNpxPath(), ['cdk', 'init', 'app', '--language', 'typescript'], {
117+
await spawn(getNpxPath(), [`aws-cdk@${CDK_CLI_VERSION}`, 'init', 'app', '--language', 'typescript'], {
86118
cwd,
87119
stripColors: true,
88120
// npx cdk does not work on verdaccio
@@ -93,6 +125,8 @@ export const initCDKProject = async (cwd: string, templatePath: string, props?:
93125
.sendYes()
94126
.runAsync();
95127

128+
removeWholeProjectTypecheckFromSynth(cwd);
129+
96130
if (props?.cdkContext) {
97131
appendToCDKContext(cwd, props.cdkContext);
98132
}

0 commit comments

Comments
 (0)