Skip to content

Commit 47fc504

Browse files
author
Dane Pilcher
authored
Merge pull request #715 from aws-amplify/main
Release swift mult files fix
2 parents 23efa07 + a901362 commit 47fc504

File tree

8 files changed

+91
-56
lines changed

8 files changed

+91
-56
lines changed

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

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const constants = require('../constants');
77
const { loadConfig } = require('../codegen-config');
88
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require('../utils');
99
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
10-
const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
10+
const { generate, extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
1111

1212
async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') {
1313
let frontend = decoupleFrontend;
@@ -57,25 +57,24 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
5757
const target = cfg.amplifyExtension.codeGenTarget;
5858

5959
const excludes = cfg.excludes.map(pattern => `!${pattern}`);
60-
const queryFiles = glob
61-
.sync([...includeFiles, ...excludes], {
62-
cwd: projectPath,
63-
absolute: true,
64-
})
65-
.map(queryFilePath => {
66-
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
67-
if (
68-
queryFilePath.endsWith('.jsx') ||
69-
queryFilePath.endsWith('.js') ||
70-
queryFilePath.endsWith('.tsx') ||
71-
queryFilePath.endsWith('.ts')
72-
) {
73-
return extractDocumentFromJavascript(fileContents, '');
74-
}
75-
return fileContents;
76-
});
60+
const queryFilePaths = glob.sync([...includeFiles, ...excludes], {
61+
cwd: projectPath,
62+
absolute: true,
63+
});
64+
const queryFiles = queryFilePaths.map(queryFilePath => {
65+
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
66+
if (
67+
queryFilePath.endsWith('.jsx') ||
68+
queryFilePath.endsWith('.js') ||
69+
queryFilePath.endsWith('.tsx') ||
70+
queryFilePath.endsWith('.ts')
71+
) {
72+
return extractDocumentFromJavascript(fileContents, '');
73+
}
74+
return fileContents;
75+
});
7776
if (queryFiles.length === 0) {
78-
throw new Error('No queries found to generate types for, you may need to run \'codegen statements\' first');
77+
throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first");
7978
}
8079
const queries = queryFiles.join('\n');
8180

@@ -96,22 +95,30 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
9695
const introspection = path.extname(schemaPath) === '.json';
9796

9897
try {
99-
const output = await generateTypesHelper({
100-
schema,
101-
queries,
102-
target,
103-
introspection,
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);
98+
if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) {
99+
generate(queryFilePaths, schemaPath, outputPath, '', target, '', {
100+
addTypename: true,
101+
complexObjectSupport: 'auto',
102+
});
111103
} else {
112-
outputs.forEach(([filepath, contents]) => {
113-
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
104+
const output = await generateTypesHelper({
105+
schema,
106+
queries,
107+
target,
108+
introspection,
109+
multipleSwiftFiles: false,
114110
});
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+
}
115122
}
116123
codeGenSpinner.succeed(`${constants.INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS} ${path.relative(path.resolve('.'), outputPath)}`);
117124
} catch (err) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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');
45
const fs = require('fs-extra');
56

67
const { loadConfig } = require('../../src/codegen-config');
@@ -20,6 +21,7 @@ const MOCK_CONTEXT = {
2021

2122
jest.mock('glob-all');
2223
jest.mock('@aws-amplify/graphql-generator');
24+
jest.mock('@aws-amplify/graphql-types-generator');
2325
jest.mock('../../src/codegen-config');
2426
jest.mock('../../src/utils');
2527
jest.mock('fs-extra');
@@ -81,9 +83,35 @@ describe('command - types', () => {
8183
schema: 'schema',
8284
target: 'TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE',
8385
introspection: false,
86+
multipleSwiftFiles: false,
8487
});
8588
});
8689

90+
it('should use legacy types generation when generating multiple swift files', async () => {
91+
MOCK_PROJECT.amplifyExtension.codeGenTarget = 'swift';
92+
MOCK_PROJECT.amplifyExtension.generatedFileName = 'typesDirectory';
93+
const forceDownload = false;
94+
fs.readFileSync
95+
.mockReturnValueOnce('query 1')
96+
.mockReturnValueOnce('query 2')
97+
.mockReturnValueOnce('schema');
98+
fs.existsSync.mockReturnValueOnce(true);
99+
fs.statSync.mockReturnValueOnce({
100+
isDirectory: jest.fn().mockReturnValue(true),
101+
});
102+
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+
);
113+
});
114+
87115
it('should not generate type if the frontend is android', async () => {
88116
const forceDownload = false;
89117
getFrontEndHandler.mockReturnValue('android');

packages/appsync-modelgen-plugin/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
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-
"@types/node": "^12.12.6",
33-
"@types/pluralize": "0.0.29",
3432
"chalk": "^3.0.0",
3533
"change-case": "^4.1.1",
3634
"lower-case-first": "^2.0.1",
@@ -43,7 +41,9 @@
4341
"@graphql-codegen/typescript": "^2.8.3",
4442
"graphql": "^15.5.0",
4543
"java-ast": "^0.3.0",
46-
"ts-json-schema-generator": "1.0.0"
44+
"ts-json-schema-generator": "1.0.0",
45+
"@types/node": "^12.12.6",
46+
"@types/pluralize": "0.0.29"
4747
},
4848
"peerDependencies": {
4949
"graphql": "^15.5.0"

packages/graphql-generator/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type GenerateModelsOptions = {
2222
directives: string;
2323
generateIndexRules?: boolean;
2424
emitAuthProvider?: boolean;
25-
useExperimentalPipelinedTranformer?: boolean;
25+
useExperimentalPipelinedTransformer?: boolean;
2626
transformerVersion?: boolean;
2727
respectPrimaryKeyAttributesOnConnectionField?: boolean;
2828
generateModelsForLazyLoadAndCustomSelectionSet?: boolean;

packages/graphql-generator/src/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function generateModels(options: GenerateModelsOptions): Promise<Ge
1313
// feature flags
1414
generateIndexRules = true,
1515
emitAuthProvider = true,
16-
useExperimentalPipelinedTranformer = true,
16+
useExperimentalPipelinedTransformer = true,
1717
transformerVersion = true,
1818
respectPrimaryKeyAttributesOnConnectionField = true,
1919
generateModelsForLazyLoadAndCustomSelectionSet = true,
@@ -34,7 +34,7 @@ export async function generateModels(options: GenerateModelsOptions): Promise<Ge
3434
emitAuthProvider,
3535
generateIndexRules,
3636
handleListNullabilityTransparently,
37-
usePipelinedTransformer: useExperimentalPipelinedTranformer,
37+
usePipelinedTransformer: useExperimentalPipelinedTransformer,
3838
transformerVersion,
3939
respectPrimaryKeyAttributesOnConnectionField,
4040
generateModelsForLazyLoadAndCustomSelectionSet,

packages/graphql-generator/src/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type GenerateModelsOptions = {
2222
// feature flags
2323
generateIndexRules?: boolean;
2424
emitAuthProvider?: boolean;
25-
useExperimentalPipelinedTranformer?: boolean;
25+
useExperimentalPipelinedTransformer?: boolean;
2626
transformerVersion?: boolean;
2727
respectPrimaryKeyAttributesOnConnectionField?: boolean;
2828
generateModelsForLazyLoadAndCustomSelectionSet?: boolean;

packages/graphql-types-generator/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
"dependencies": {
3838
"@babel/generator": "7.0.0-beta.4",
3939
"@babel/types": "7.0.0-beta.4",
40-
"@types/babel-generator": "^6.25.0",
41-
"@types/fs-extra": "^8.1.0",
42-
"@types/prettier": "^1.19.0",
43-
"@types/rimraf": "^3.0.0",
4440
"babel-generator": "^6.26.1",
4541
"babel-types": "^6.26.0",
4642
"change-case": "^4.1.1",
@@ -60,7 +56,11 @@
6056
"@types/glob": "^7.1.1",
6157
"@types/inflected": "^1.1.29",
6258
"@types/node": "^10.17.13",
63-
"@types/yargs": "^15.0.1"
59+
"@types/yargs": "^15.0.1",
60+
"@types/babel-generator": "^6.25.0",
61+
"@types/fs-extra": "^8.1.0",
62+
"@types/prettier": "^1.19.0",
63+
"@types/rimraf": "^3.0.0"
6464
},
6565
"publishConfig": {
6666
"access": "public"

yarn.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,9 +3744,9 @@
37443744
value-or-promise "^1.0.12"
37453745

37463746
"@graphql-tools/utils@^10.0.0":
3747-
version "10.0.5"
3748-
resolved "https://registry.npmjs.org/@graphql-tools/utils/-/utils-10.0.5.tgz#b76c7b5b7fc3f67da734b51cf6e33c52dee09974"
3749-
integrity sha512-ZTioQqg9z9eCG3j+KDy54k1gp6wRIsLqkx5yi163KVvXVkfjsrdErCyZjrEug21QnKE9piP4tyxMpMMOT1RuRw==
3747+
version "10.0.6"
3748+
resolved "https://registry.npmjs.org/@graphql-tools/utils/-/utils-10.0.6.tgz#8a809d6bc0df27ffe8964696f182af2383b5974b"
3749+
integrity sha512-hZMjl/BbX10iagovakgf3IiqArx8TPsotq5pwBld37uIX1JiZoSbgbCIFol7u55bh32o6cfDEiiJgfAD5fbeyQ==
37503750
dependencies:
37513751
"@graphql-typed-document-node/core" "^3.1.1"
37523752
dset "^3.1.2"
@@ -6128,17 +6128,17 @@
61286128
integrity sha512-AyQEn5hIPV7Ze+xFoXVU3QTHXVbWPrzaOkxtENMPMuNL6VVHrp4hHfDt9nrQpjO7BgvuM95dMtkycX5M/DZR3w==
61296129

61306130
"@whatwg-node/fetch@^0.9.0":
6131-
version "0.9.9"
6132-
resolved "https://registry.npmjs.org/@whatwg-node/fetch/-/fetch-0.9.9.tgz#65e68aaf8353755c20657b803f2fe983dbdabf91"
6133-
integrity sha512-OTVoDm039CNyAWSRc2WBimMl/N9J4Fk2le21Xzcf+3OiWPNNSIbMnpWKBUyraPh2d9SAEgoBdQxTfVNihXgiUw==
6131+
version "0.9.13"
6132+
resolved "https://registry.npmjs.org/@whatwg-node/fetch/-/fetch-0.9.13.tgz#1d084cd546b9cd425ae89cbb1252a3e47a9a2e1c"
6133+
integrity sha512-PPtMwhjtS96XROnSpowCQM85gCUG2m7AXZFw0PZlGbhzx2GK7f2iOXilfgIJ0uSlCuuGbOIzfouISkA7C4FJOw==
61346134
dependencies:
6135-
"@whatwg-node/node-fetch" "^0.4.8"
6135+
"@whatwg-node/node-fetch" "^0.4.17"
61366136
urlpattern-polyfill "^9.0.0"
61376137

6138-
"@whatwg-node/node-fetch@^0.4.8":
6139-
version "0.4.13"
6140-
resolved "https://registry.npmjs.org/@whatwg-node/node-fetch/-/node-fetch-0.4.13.tgz#523b046f9511e6e62ac98365653b5ce63175614b"
6141-
integrity sha512-Wijn8jtXq6VBX6EttABXHJIQBcoOP6RRQllXbiaHGORACTDr1xg6g2UnkoggY3dbDkm1VsMjdSe7NVBPc4ukYg==
6138+
"@whatwg-node/node-fetch@^0.4.17":
6139+
version "0.4.19"
6140+
resolved "https://registry.npmjs.org/@whatwg-node/node-fetch/-/node-fetch-0.4.19.tgz#29c72ff65a8e450949238612ff17a3d3717736d3"
6141+
integrity sha512-AW7/m2AuweAoSXmESrYQr/KBafueScNbn2iNO0u6xFr2JZdPmYsSm5yvAXYk6yDLv+eDmSSKrf7JnFZ0CsJIdA==
61426142
dependencies:
61436143
"@whatwg-node/events" "^0.1.0"
61446144
busboy "^1.6.0"

0 commit comments

Comments
 (0)