Skip to content

Commit 7f197b9

Browse files
authored
Merge pull request #608 from aws-amplify/main
Release Amplify Codegen 4.1.2
2 parents bb51593 + 957169c commit 7f197b9

File tree

7 files changed

+95
-18
lines changed

7 files changed

+95
-18
lines changed

.github/CODEOWNERS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
* @aws-amplify/amplify-data
1+
# Data team has general approval permissions
2+
* @aws-amplify/amplify-data
3+
4+
# API approval - public surface and dependencies.
5+
**/API.md @aws-amplify/amplify-data-admins

CONTRIBUTING.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,50 @@ Our work is done directly on Github and PR's are sent to the GitHub repo by core
1010

1111
This section should get you running with **Amplify Codegen**.
1212

13-
### Setting up for local development
13+
### Local Development
14+
15+
#### Environment Setup
1416

1517
1. You will need the latest version of [nodejs](https://nodejs.org/en/) on your system and developing locally also requires `yarn` workspaces. You can install it [here](https://classic.yarnpkg.com/en/docs/install#mac-stable).
1618

17-
2. Start by [Forking](https://help.github.com/en/github/getting-started-with-github/fork-a-repo) the main branch of [amplify-codegen](https://github.com/aws-amplify/amplify-codegen).
19+
1. Start by [Forking](https://help.github.com/en/github/getting-started-with-github/fork-a-repo) the main branch of [amplify-codegen](https://github.com/aws-amplify/amplify-codegen).
1820

1921
```sh
2022
$ git clone [email protected]:[username]/amplify-codegen.git
2123
```
2224

2325
> NOTE: Make sure to always [sync your fork](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork) with _main_ branch of amplify-codegen
2426
25-
3. Move into your project folder
27+
1. Move into your project folder
2628

2729
```sh
2830
$ cd amplify-codegen
2931
```
3032

31-
4. Run `setup-dev` script to installs dependencies and perform initial configuration. This command will also link a `amplify-dev` binary for your local testing.
33+
#### Building and Running Tests
34+
35+
1. To build local packages and verify your change is valid and doesn't break the build, you can run :
36+
37+
```sh
38+
yarn # Install all dependencies for the workspace
39+
yarn build # Build all packages in the repo
40+
yarn test # Run tests for all packages in the repo
41+
```
42+
43+
1. Note: once you've run an initial `yarn` unless you're changing dependencies in a package, re-running should not be necessary.
44+
1. After an initial build, if you're testing changes to a single package, you can run `yarn build` and `yarn test` specifically from that directory (e.g. `/packages/appsync-modelgen-plugin`) in order to speed up your iteration cycle.
45+
46+
#### Building the CLI Locally for functional testing
47+
48+
1. Run `setup-dev` script to installs dependencies and perform initial configuration. This command will also link a `amplify-dev` binary for your local testing.
3249

3350
```sh
3451
$ yarn setup-dev
3552
```
3653

3754
> NOTE: The `amplify-dev` binary is built based on the latest amplify cli from npm registry and your local codegen packages. All your local changes from codegen can be reflected (typescript files need to be build by `tsc`). In addition, if you are a developer of cli repo, you can run the same command to override the `amplify-dev` binary.
3855
39-
5. Ensure `amplify-dev` exists on your path.
56+
1. Ensure `amplify-dev` exists on your path.
4057

4158
```sh
4259
$ yarn global bin # retrieve yarn path

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,31 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
147147
});
148148
}
149149

150+
// CLI workflow to add codegen to non-Amplify JS project
151+
export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
152+
return new Promise((resolve, reject) => {
153+
const cmdOptions = ['codegen', 'add', '--apiId', 'mockapiid'];
154+
const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true });
155+
chain
156+
.wait("Choose the type of app that you're building")
157+
.sendCarriageReturn()
158+
.wait('What javascript framework are you using')
159+
.sendCarriageReturn()
160+
.wait('Choose the code generation language target').sendCarriageReturn()
161+
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
162+
.sendCarriageReturn()
163+
.wait('Do you want to generate/update all possible GraphQL operations')
164+
.sendLine('y')
165+
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
166+
.sendCarriageReturn();
167+
168+
chain.run((err: Error) => {
169+
if (!err) {
170+
resolve();
171+
} else {
172+
reject(err);
173+
}
174+
});
175+
});
176+
}
177+

packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
DEFAULT_JS_CONFIG,
66
createRandomName,
77
addApiWithoutSchema,
8-
updateApiSchema
8+
updateApiSchema,
9+
addCodegenNonAmplifyJS
910
} from "@aws-amplify/amplify-codegen-e2e-core";
10-
import { existsSync } from "fs";
11+
import { existsSync, writeFileSync } from "fs";
1112
import path from 'path';
1213
import { isNotEmptyDir } from '../utils';
1314
import { deleteAmplifyProject, testAddCodegen, testSetupBeforeAddCodegen,
@@ -72,4 +73,36 @@ describe('codegen add tests - JS', () => {
7273
it(`Adding codegen works as expected`, async () => {
7374
await testAddCodegen(config, projectRoot, schema);
7475
});
76+
77+
it(`Adding codegen outside of Amplify project`, async () => {
78+
// init project and add API category
79+
const testSchema = `
80+
type Query {
81+
echo: String
82+
}
83+
84+
type Mutation {
85+
mymutation: String
86+
}
87+
88+
type Subscription {
89+
mysub: String
90+
}
91+
`;
92+
93+
// Setup the non-amplify project with schema and pre-existing files
94+
const userSourceCodePath = testSetupBeforeAddCodegen(projectRoot, config);
95+
const schemaPath = path.join(projectRoot, 'schema.graphql');
96+
writeFileSync(schemaPath, testSchema);
97+
98+
// add codegen without init
99+
await expect(addCodegenNonAmplifyJS(projectRoot)).resolves.not.toThrow();
100+
101+
// pre-existing file should still exist
102+
expect(existsSync(userSourceCodePath)).toBe(true);
103+
// GraphQL statements are generated
104+
expect(isNotEmptyDir(path.join(projectRoot, config.graphqlCodegenDir))).toBe(true);
105+
// graphql configuration should be added
106+
expect(existsSync(getGraphQLConfigFilePath(projectRoot))).toBe(true);
107+
});
75108
});

packages/amplify-codegen/src/codegen-config/AmplifyCodeGenConfig.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ const fs = require('fs-extra');
77
class AmplifyCodeGenConfig {
88
static configFileName = '.graphqlconfig.yml';
99

10-
constructor(projectPath, withoutInit = false) {
10+
constructor(projectPath) {
1111
try {
1212
this.gqlConfig = graphQLConfig.getGraphQLConfig();
1313
this.fixOldConfig();
1414
} catch (e) {
1515
if (e instanceof graphQLConfig.ConfigNotFoundError) {
16-
let projectRoot;
17-
if (!withoutInit) {
18-
projectRoot = projectPath || process.cwd();
19-
} else {
20-
projectRoot = process.cwd();
21-
}
16+
const projectRoot = projectPath || process.cwd();
2217
const configPath = join(projectRoot, '.graphqlconfig.yml');
2318
if(fs.existsSync(configPath)) {
2419
this.gqlConfig = graphQLConfig.getGraphQLConfig(projectRoot);

packages/amplify-codegen/src/codegen-config/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ let config = null;
66

77
function loadConfig(context, withoutInit = false) {
88
if (!config) {
9-
const projectPath = context.amplify.getEnvInfo().projectPath;
10-
config = new AmplifyCodeGenConfig(projectPath, withoutInit);
9+
const projectPath = withoutInit ? undefined : context.amplify.getEnvInfo().projectPath;
10+
config = new AmplifyCodeGenConfig(projectPath);
1111
}
1212
return config;
1313
}

packages/amplify-codegen/tests/codegen-config/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const MOCK_CONTEXT = {
1313
describe('codegen-config', () => {
1414
it('is singleton', () => {
1515
loadConfig(MOCK_CONTEXT);
16-
expect(AmplifyCodeGenConfig).toHaveBeenCalledWith(MOCK_PROJECT_ROOT, false);
16+
expect(AmplifyCodeGenConfig).toHaveBeenCalledWith(MOCK_PROJECT_ROOT);
1717
loadConfig(MOCK_CONTEXT);
1818
expect(AmplifyCodeGenConfig).toHaveBeenCalledTimes(1);
1919
});

0 commit comments

Comments
 (0)