Skip to content

Commit 8279f35

Browse files
alharris-atDane Pilcher
andauthored
feat: support running codegen without an initialized amplify backend set up locally. (#702)
* feat: codegen add --region (#683) * fix: download introspection schema when apiId is passed (#684) * fix: codegen --apiId broken state (#689) * chore: get existing e2e tests passing (#704) * feat: add e2e tests for codegen add when in a non-amplify project (#705) * feat: add e2e tests for codegen add when in a non-amplify project * chore: add some additional test cases * noinit codegen from graphqlconfig (#706) * fix: add missing awaits * chore: fail typegen if no queries are found --------- Co-authored-by: Dane Pilcher <[email protected]>
1 parent f44358a commit 8279f35

File tree

21 files changed

+1990
-138
lines changed

21 files changed

+1990
-138
lines changed

.codebuild/e2e_workflow.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,23 @@ batch:
121121
depend-on:
122122
- publish_to_local_registry
123123
- identifier: >-
124-
build_app_ts_uninitialized_project_modelgen_android_uninitialized_project_modelgen_flutter_uninitialized_project_modelgen_ios
124+
build_app_ts_uninitialized_project_codegen_js_uninitialized_project_modelgen_android_uninitialized_project_modelgen_flutter
125125
buildspec: .codebuild/run_e2e_tests.yml
126126
env:
127127
compute-type: BUILD_GENERAL1_LARGE
128128
variables:
129129
TEST_SUITE: >-
130-
src/__tests__/build-app-ts.test.ts|src/__tests__/uninitialized-project-modelgen-android.test.ts|src/__tests__/uninitialized-project-modelgen-flutter.test.ts|src/__tests__/uninitialized-project-modelgen-ios.test.ts
130+
src/__tests__/build-app-ts.test.ts|src/__tests__/uninitialized-project-codegen-js.test.ts|src/__tests__/uninitialized-project-modelgen-android.test.ts|src/__tests__/uninitialized-project-modelgen-flutter.test.ts
131131
CLI_REGION: ap-southeast-1
132132
depend-on:
133133
- publish_to_local_registry
134-
- identifier: uninitialized_project_modelgen_js
134+
- identifier: uninitialized_project_modelgen_ios_uninitialized_project_modelgen_js
135135
buildspec: .codebuild/run_e2e_tests.yml
136136
env:
137137
compute-type: BUILD_GENERAL1_LARGE
138138
variables:
139-
TEST_SUITE: src/__tests__/uninitialized-project-modelgen-js.test.ts
139+
TEST_SUITE: >-
140+
src/__tests__/uninitialized-project-modelgen-ios.test.ts|src/__tests__/uninitialized-project-modelgen-js.test.ts
140141
CLI_REGION: ap-southeast-2
141142
depend-on:
142143
- publish_to_local_registry

packages/amplify-codegen-e2e-core/src/categories/codegen.ts

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,40 @@ export const generateModelsWithOptions = (cwd: string, options: Record<string, a
3030
});
3131
});
3232

33-
export function generateStatementsAndTypes(cwd: string) : Promise<void> {
33+
export function generateStatementsAndTypes(cwd: string, errorMessage?: string) : Promise<void> {
3434
return new Promise((resolve, reject) => {
35-
spawn(getCLIPath(), ['codegen'], { cwd, stripColors: true })
35+
const chain = spawn(getCLIPath(), ['codegen'], { cwd, stripColors: true })
36+
37+
if (errorMessage) {
38+
chain.wait(errorMessage);
39+
}
40+
41+
return chain.run((err: Error) => {
42+
if (!err) {
43+
resolve();
44+
} else {
45+
reject(err);
46+
}
47+
})
48+
});
49+
}
50+
51+
export function generateStatements(cwd: string) : Promise<void> {
52+
return new Promise((resolve, reject) => {
53+
spawn(getCLIPath(), ['codegen', 'statements'], { cwd, stripColors: true })
54+
.run((err: Error) => {
55+
if (!err) {
56+
resolve();
57+
} else {
58+
reject(err);
59+
}
60+
})
61+
});
62+
}
63+
64+
export function generateTypes(cwd: string) : Promise<void> {
65+
return new Promise((resolve, reject) => {
66+
spawn(getCLIPath(), ['codegen', 'types'], { cwd, stripColors: true })
3667
.run((err: Error) => {
3768
if (!err) {
3869
resolve();
@@ -46,7 +77,10 @@ export function generateStatementsAndTypes(cwd: string) : Promise<void> {
4677
// CLI workflow to add codegen to Amplify project
4778
export function addCodegen(cwd: string, settings: any = {}): Promise<void> {
4879
return new Promise((resolve, reject) => {
49-
const chain = spawn(getCLIPath(), ['codegen', 'add'], { cwd, stripColors: true });
80+
const params = settings.params
81+
? ['codegen', 'add', ...settings.params]
82+
: ['codegen', 'add'];
83+
const chain = spawn(getCLIPath(), params, { cwd, stripColors: true });
5084
if (settings.isAPINotAdded) {
5185
chain.wait("There are no GraphQL APIs available.");
5286
chain.wait("Add by running $amplify api add");
@@ -166,22 +200,26 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
166200
}
167201

168202
// CLI workflow to add codegen to non-Amplify JS project
169-
export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
203+
export function addCodegenNonAmplifyJS(cwd: string, params: Array<string>, initialFailureMessage?: string): Promise<void> {
170204
return new Promise((resolve, reject) => {
171-
const cmdOptions = ['codegen', 'add', '--apiId', 'mockapiid'];
172-
const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true });
173-
chain
174-
.wait("Choose the type of app that you're building")
175-
.sendCarriageReturn()
176-
.wait('What javascript framework are you using')
177-
.sendCarriageReturn()
178-
.wait('Choose the code generation language target').sendCarriageReturn()
179-
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
180-
.sendCarriageReturn()
181-
.wait('Do you want to generate/update all possible GraphQL operations')
182-
.sendLine('y')
183-
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
184-
.sendCarriageReturn();
205+
const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true });
206+
207+
if (initialFailureMessage) {
208+
chain.wait(initialFailureMessage)
209+
} else {
210+
chain
211+
.wait("Choose the type of app that you're building")
212+
.sendCarriageReturn()
213+
.wait('What javascript framework are you using')
214+
.sendCarriageReturn()
215+
.wait('Choose the code generation language target').sendCarriageReturn()
216+
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
217+
.sendCarriageReturn()
218+
.wait('Do you want to generate/update all possible GraphQL operations')
219+
.sendLine('y')
220+
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
221+
.sendCarriageReturn();
222+
}
185223

186224
chain.run((err: Error) => {
187225
if (!err) {
@@ -192,3 +230,31 @@ export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
192230
});
193231
});
194232
}
233+
234+
export function addCodegenNonAmplifyTS(cwd: string, params: Array<string>, initialFailureMessage?: string): Promise<void> {
235+
return new Promise((resolve, reject) => {
236+
const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true });
237+
238+
if (initialFailureMessage) {
239+
chain.wait(initialFailureMessage)
240+
} else {
241+
chain
242+
.wait("Choose the type of app that you're building").sendCarriageReturn()
243+
.wait('What javascript framework are you using').sendCarriageReturn()
244+
.wait('Choose the code generation language target').sendKeyDown().sendCarriageReturn()
245+
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions').sendCarriageReturn()
246+
.wait('Do you want to generate/update all possible GraphQL operations').sendLine('y')
247+
.wait('Enter maximum statement depth [increase from default if your schema is deeply').sendCarriageReturn()
248+
.wait('Enter the file name for the generated code').sendCarriageReturn()
249+
.wait('Do you want to generate code for your newly created GraphQL API').sendCarriageReturn();
250+
}
251+
252+
chain.run((err: Error) => {
253+
if (!err) {
254+
resolve();
255+
} else {
256+
reject(err);
257+
}
258+
});
259+
});
260+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type Query {
2+
echo: String
3+
}
4+
5+
type Mutation {
6+
mymutation: String
7+
}
8+
9+
type Subscription {
10+
mysub: String
11+
}

0 commit comments

Comments
 (0)