Skip to content

Commit fd984bc

Browse files
author
Dane Pilcher
authored
Merge pull request #720 from aws-amplify/main
Release mult swift files fix and dep fix
2 parents 348f87c + 1e484af commit fd984bc

File tree

16 files changed

+330
-721
lines changed

16 files changed

+330
-721
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ module.exports = {
190190
'@typescript-eslint/interface-name-prefix': 'off',
191191
'no-throw-literal': 'off',
192192
'react/static-property-placement': 'off',
193-
'import/no-extraneous-dependencies': 'off',
193+
'import/no-extraneous-dependencies': ['error', {devDependencies: true}],
194194
'spaced-comment': 'off',
195195
'@typescript-eslint/no-array-constructor': 'off',
196196
'prefer-rest-params': 'off',

packages/amplify-codegen-e2e-core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
"clean": "rimraf ./lib"
2323
},
2424
"dependencies": {
25+
"aws-sdk": "^2.1465.0",
2526
"chalk": "^3.0.0",
27+
"dotenv": "^8.6.0",
2628
"execa": "^4.1.0",
2729
"fs-extra": "^8.1.0",
30+
"glob": "^10.3.9",
2831
"ini": "^3.0.1",
32+
"jest-circus": "^27.5.1",
2933
"jest-environment-node": "^26.6.2",
3034
"lodash": "^4.17.19",
3135
"node-pty": "beta",

packages/amplify-codegen-e2e-tests/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"@aws-amplify/amplify-codegen-e2e-core": "1.6.0",
2626
"@aws-amplify/graphql-schema-test-library": "^1.1.18",
27+
"amazon-cognito-identity-js": "^6.3.6",
2728
"aws-amplify": "^5.3.3",
2829
"aws-appsync": "^4.1.9",
2930
"aws-sdk": "^2.1413.0",
@@ -33,7 +34,9 @@
3334
"graphql-tag": "^2.10.1",
3435
"js-yaml": "^4.0.0",
3536
"lodash": "^4.17.19",
37+
"node-fetch": "^3.3.2",
3638
"uuid": "^3.4.0",
39+
"ws": "^8.14.2",
3740
"yargs": "^15.1.0"
3841
},
3942
"devDependencies": {

packages/amplify-codegen/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"dependencies": {
2424
"@aws-amplify/graphql-generator": "0.1.2",
2525
"@aws-amplify/graphql-types-generator": "3.4.1",
26+
"@aws-amplify/graphql-docs-generator": "4.2.0",
2627
"@graphql-codegen/core": "2.6.6",
28+
"aws-sdk": "^2.1465.0",
2729
"chalk": "^3.0.0",
2830
"fs-extra": "^8.1.0",
2931
"glob-all": "^3.1.0",

packages/amplify-codegen/src/commands/types.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ const path = require('path');
22
const fs = require('fs-extra');
33
const Ora = require('ora');
44
const glob = require('glob-all');
5+
const { Source } = require('graphql');
56

67
const constants = require('../constants');
78
const { loadConfig } = require('../codegen-config');
89
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require('../utils');
910
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
10-
const { generate, extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
11+
const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
1112

1213
async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') {
1314
let frontend = decoupleFrontend;
@@ -61,7 +62,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
6162
cwd: projectPath,
6263
absolute: true,
6364
});
64-
const queryFiles = queryFilePaths.map(queryFilePath => {
65+
const queries = queryFilePaths.map(queryFilePath => {
6566
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
6667
if (
6768
queryFilePath.endsWith('.jsx') ||
@@ -71,12 +72,11 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
7172
) {
7273
return extractDocumentFromJavascript(fileContents, '');
7374
}
74-
return fileContents;
75+
return new Source(fileContents, queryFilePath);
7576
});
76-
if (queryFiles.length === 0) {
77+
if (queries.length === 0) {
7778
throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first");
7879
}
79-
const queries = queryFiles.join('\n');
8080

8181
const schemaPath = path.join(projectPath, cfg.schema);
8282

@@ -93,32 +93,25 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
9393
codeGenSpinner.start();
9494
const schema = fs.readFileSync(schemaPath, 'utf8');
9595
const introspection = path.extname(schemaPath) === '.json';
96-
96+
const multipleSwiftFiles = target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory();
9797
try {
98-
if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) {
99-
generate(queryFilePaths, schemaPath, outputPath, '', target, '', {
100-
addTypename: true,
101-
complexObjectSupport: 'auto',
102-
});
98+
const output = await generateTypesHelper({
99+
schema,
100+
queries,
101+
target,
102+
introspection,
103+
multipleSwiftFiles,
104+
});
105+
const outputs = Object.entries(output);
106+
107+
const outputPath = path.join(projectPath, generatedFileName);
108+
if (outputs.length === 1) {
109+
const [[, contents]] = outputs;
110+
fs.outputFileSync(path.resolve(outputPath), contents);
103111
} else {
104-
const output = await generateTypesHelper({
105-
schema,
106-
queries,
107-
target,
108-
introspection,
109-
multipleSwiftFiles: false,
112+
outputs.forEach(([filepath, contents]) => {
113+
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
110114
});
111-
const outputs = Object.entries(output);
112-
113-
const outputPath = path.join(projectPath, generatedFileName);
114-
if (outputs.length === 1) {
115-
const [[, contents]] = outputs;
116-
fs.outputFileSync(path.resolve(outputPath), contents);
117-
} else {
118-
outputs.forEach(([filepath, contents]) => {
119-
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
120-
});
121-
}
122115
}
123116
codeGenSpinner.succeed(`${constants.INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS} ${path.relative(path.resolve('.'), outputPath)}`);
124117
} catch (err) {

packages/amplify-codegen/tests/commands/types.test.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const { sync } = require('glob-all');
22
const path = require('path');
33
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
4-
const { generate: legacyGenerate } = require('@aws-amplify/graphql-types-generator');
54
const fs = require('fs-extra');
5+
const { Source } = require('graphql');
66

77
const { loadConfig } = require('../../src/codegen-config');
88
const generateTypes = require('../../src/commands/types');
@@ -60,6 +60,9 @@ describe('command - types', () => {
6060
beforeEach(() => {
6161
jest.clearAllMocks();
6262
fs.existsSync.mockReturnValue(true);
63+
fs.statSync.mockReturnValue({
64+
isDirectory: jest.fn().mockReturnValue(false),
65+
});
6366
getFrontEndHandler.mockReturnValue('javascript');
6467
loadConfig.mockReturnValue({
6568
getProjects: jest.fn().mockReturnValue([MOCK_PROJECT]),
@@ -79,15 +82,15 @@ describe('command - types', () => {
7982
expect(loadConfig).toHaveBeenCalledWith(MOCK_CONTEXT, false);
8083
expect(sync).toHaveBeenCalledWith([MOCK_INCLUDE_PATH, `!${MOCK_EXCLUDE_PATH}`], { cwd: MOCK_PROJECT_ROOT, absolute: true });
8184
expect(generateTypesHelper).toHaveBeenCalledWith({
82-
queries: 'query 1\nquery 2',
85+
queries: [new Source('query 1', 'q1.gql'), new Source('query 2', 'q2.gql')],
8386
schema: 'schema',
8487
target: 'TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE',
8588
introspection: false,
8689
multipleSwiftFiles: false,
8790
});
8891
});
8992

90-
it('should use legacy types generation when generating multiple swift files', async () => {
93+
it('should use generate multiple swift files', async () => {
9194
MOCK_PROJECT.amplifyExtension.codeGenTarget = 'swift';
9295
MOCK_PROJECT.amplifyExtension.generatedFileName = 'typesDirectory';
9396
const forceDownload = false;
@@ -100,16 +103,6 @@ describe('command - types', () => {
100103
isDirectory: jest.fn().mockReturnValue(true),
101104
});
102105
await generateTypes(MOCK_CONTEXT, forceDownload);
103-
expect(generateTypesHelper).not.toHaveBeenCalled();
104-
expect(legacyGenerate).toHaveBeenCalledWith(
105-
['q1.gql', 'q2.gql'],
106-
'MOCK_PROJECT_ROOT/INTROSPECTION_SCHEMA.JSON',
107-
'MOCK_PROJECT_ROOT/typesDirectory',
108-
'',
109-
'swift',
110-
'',
111-
{ addTypename: true, complexObjectSupport: 'auto' },
112-
);
113106
});
114107

115108
it('should not generate type if the frontend is android', async () => {
@@ -121,6 +114,7 @@ describe('command - types', () => {
121114

122115
it('should download the schema if forceDownload flag is passed', async () => {
123116
const forceDownload = true;
117+
fs.readFileSync.mockReturnValueOnce('query 1').mockReturnValueOnce('query 2');
124118
await generateTypes(MOCK_CONTEXT, forceDownload);
125119
expect(ensureIntrospectionSchema).toHaveBeenCalledWith(
126120
MOCK_CONTEXT,
@@ -134,6 +128,7 @@ describe('command - types', () => {
134128
it('should download the schema if the schema file is missing', async () => {
135129
fs.existsSync.mockReturnValue(false);
136130
const forceDownload = false;
131+
fs.readFileSync.mockReturnValueOnce('query 1').mockReturnValueOnce('query 2');
137132
await generateTypes(MOCK_CONTEXT, forceDownload);
138133
expect(ensureIntrospectionSchema).toHaveBeenCalledWith(
139134
MOCK_CONTEXT,

packages/appsync-modelgen-plugin/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@
2929
"@graphql-codegen/plugin-helpers": "^1.18.8",
3030
"@graphql-codegen/visitor-plugin-common": "^1.22.0",
3131
"@graphql-tools/utils": "^6.0.18",
32+
"ajv": "^6.10.0",
3233
"chalk": "^3.0.0",
3334
"change-case": "^4.1.1",
35+
"graphql-transformer-common": "^4.25.1",
3436
"lower-case-first": "^2.0.1",
3537
"pluralize": "^8.0.0",
3638
"strip-indent": "^3.0.0",
37-
"ts-dedent": "^1.1.0"
39+
"ts-dedent": "^1.1.0",
40+
"ts-json-schema-generator": "1.0.0"
3841
},
3942
"devDependencies": {
4043
"@graphql-codegen/testing": "^1.17.7",
4144
"@graphql-codegen/typescript": "^2.8.3",
45+
"@types/fs-extra": "^8.1.2",
4246
"@types/node": "^12.12.6",
4347
"@types/pluralize": "0.0.29",
48+
4449
"graphql": "^15.5.0",
45-
"java-ast": "^0.3.0",
46-
"ts-json-schema-generator": "1.0.0"
50+
"java-ast": "^0.3.0"
4751
},
4852
"peerDependencies": {
4953
"graphql": "^15.5.0"

packages/graphql-generator/API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
```ts
66

7+
import { Source } from 'graphql';
78
import { Target } from '@aws-amplify/appsync-modelgen-plugin';
89
import { Target as Target_2 } from '@aws-amplify/graphql-types-generator';
910

@@ -49,7 +50,7 @@ export function generateTypes(options: GenerateTypesOptions): Promise<GeneratedO
4950
export type GenerateTypesOptions = {
5051
schema: string;
5152
target: TypesTarget;
52-
queries: string;
53+
queries: string | Source[];
5354
introspection?: boolean;
5455
multipleSwiftFiles?: boolean;
5556
};

packages/graphql-generator/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"@graphql-tools/apollo-engine-loader": "^8.0.0",
3232
"graphql": "^15.5.0"
3333
},
34+
"devDependencies": {
35+
"@types/prettier": "^1.0.0"
36+
},
3437
"typescript": {
3538
"definition": "lib/index.d.ts"
3639
},

0 commit comments

Comments
 (0)