From 0786cb03801a54fccea05ce4718f2741bb35606e Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Mon, 25 Sep 2023 10:46:54 -0600 Subject: [PATCH 1/4] fix: swift file names --- .../amplify-codegen/src/commands/types.js | 73 ++++++++++--------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/packages/amplify-codegen/src/commands/types.js b/packages/amplify-codegen/src/commands/types.js index 5da70cddf..3a8d88bff 100644 --- a/packages/amplify-codegen/src/commands/types.js +++ b/packages/amplify-codegen/src/commands/types.js @@ -7,7 +7,7 @@ const constants = require('../constants'); const { loadConfig } = require('../codegen-config'); const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require('../utils'); const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator'); -const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator'); +const { generate, extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator'); async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') { let frontend = decoupleFrontend; @@ -57,25 +57,24 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, const target = cfg.amplifyExtension.codeGenTarget; const excludes = cfg.excludes.map(pattern => `!${pattern}`); - const queryFiles = glob - .sync([...includeFiles, ...excludes], { - cwd: projectPath, - absolute: true, - }) - .map(queryFilePath => { - const fileContents = fs.readFileSync(queryFilePath, 'utf8'); - if ( - queryFilePath.endsWith('.jsx') || - queryFilePath.endsWith('.js') || - queryFilePath.endsWith('.tsx') || - queryFilePath.endsWith('.ts') - ) { - return extractDocumentFromJavascript(fileContents, ''); - } - return fileContents; - }); + const queryFilePaths = glob.sync([...includeFiles, ...excludes], { + cwd: projectPath, + absolute: true, + }); + const queryFiles = queryFilePaths.map(queryFilePath => { + const fileContents = fs.readFileSync(queryFilePath, 'utf8'); + if ( + queryFilePath.endsWith('.jsx') || + queryFilePath.endsWith('.js') || + queryFilePath.endsWith('.tsx') || + queryFilePath.endsWith('.ts') + ) { + return extractDocumentFromJavascript(fileContents, ''); + } + return fileContents; + }); if (queryFiles.length === 0) { - throw new Error('No queries found to generate types for, you may need to run \'codegen statements\' first'); + throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first"); } const queries = queryFiles.join('\n'); @@ -96,22 +95,30 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, const introspection = path.extname(schemaPath) === '.json'; try { - const output = await generateTypesHelper({ - schema, - queries, - target, - introspection, - }); - const outputs = Object.entries(output); - - const outputPath = path.join(projectPath, generatedFileName); - if (outputs.length === 1) { - const [[, contents]] = outputs; - fs.outputFileSync(path.resolve(outputPath), contents); + if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) { + generate(queryFilePaths, schemaPath, path.join(projectPath, generatedFileName), '', target, '', { + addTypename: true, + complexObjectSupport: 'auto', + }); } else { - outputs.forEach(([filepath, contents]) => { - fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents); + const output = await generateTypesHelper({ + schema, + queries, + target, + introspection, + multipleSwiftFiles: false, }); + const outputs = Object.entries(output); + + const outputPath = path.join(projectPath, generatedFileName); + if (outputs.length === 1) { + const [[, contents]] = outputs; + fs.outputFileSync(path.resolve(outputPath), contents); + } else { + outputs.forEach(([filepath, contents]) => { + fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents); + }); + } } codeGenSpinner.succeed(`${constants.INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS} ${path.relative(path.resolve('.'), outputPath)}`); } catch (err) { From f8b1baf8fde6c2dae67f6df1ab0e5d55008976fa Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Mon, 25 Sep 2023 10:55:22 -0600 Subject: [PATCH 2/4] test: update expected test --- packages/amplify-codegen/tests/commands/types.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/amplify-codegen/tests/commands/types.test.js b/packages/amplify-codegen/tests/commands/types.test.js index a9cd3c36f..c1f5144dc 100644 --- a/packages/amplify-codegen/tests/commands/types.test.js +++ b/packages/amplify-codegen/tests/commands/types.test.js @@ -81,6 +81,7 @@ describe('command - types', () => { schema: 'schema', target: 'TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE', introspection: false, + multipleSwiftFiles: false, }); }); From 532ebbbd9215f8638a2f4a2b5ad827fab102e3f9 Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Mon, 25 Sep 2023 11:00:25 -0600 Subject: [PATCH 3/4] test: legacy generate path --- .../tests/commands/types.test.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/amplify-codegen/tests/commands/types.test.js b/packages/amplify-codegen/tests/commands/types.test.js index c1f5144dc..b492e0d55 100644 --- a/packages/amplify-codegen/tests/commands/types.test.js +++ b/packages/amplify-codegen/tests/commands/types.test.js @@ -1,6 +1,7 @@ const { sync } = require('glob-all'); const path = require('path'); const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator'); +const { generate: legacyGenerate } = require('@aws-amplify/graphql-types-generator'); const fs = require('fs-extra'); const { loadConfig } = require('../../src/codegen-config'); @@ -20,6 +21,7 @@ const MOCK_CONTEXT = { jest.mock('glob-all'); jest.mock('@aws-amplify/graphql-generator'); +jest.mock('@aws-amplify/graphql-types-generator'); jest.mock('../../src/codegen-config'); jest.mock('../../src/utils'); jest.mock('fs-extra'); @@ -85,6 +87,31 @@ describe('command - types', () => { }); }); + it('should use legacy types generation when generating multiple swift files', async () => { + MOCK_PROJECT.amplifyExtension.codeGenTarget = 'swift'; + MOCK_PROJECT.amplifyExtension.generatedFileName = 'typesDirectory'; + const forceDownload = false; + fs.readFileSync + .mockReturnValueOnce('query 1') + .mockReturnValueOnce('query 2') + .mockReturnValueOnce('schema'); + fs.existsSync.mockReturnValueOnce(true); + fs.statSync.mockReturnValueOnce({ + isDirectory: jest.fn().mockReturnValue(true), + }); + await generateTypes(MOCK_CONTEXT, forceDownload); + expect(generateTypesHelper).not.toHaveBeenCalled(); + expect(legacyGenerate).toHaveBeenCalledWith( + ['q1.gql', 'q2.gql'], + 'MOCK_PROJECT_ROOT/INTROSPECTION_SCHEMA.JSON', + 'MOCK_PROJECT_ROOT/typesDirectory', + '', + 'swift', + '', + { addTypename: true, complexObjectSupport: 'auto' }, + ); + }); + it('should not generate type if the frontend is android', async () => { const forceDownload = false; getFrontEndHandler.mockReturnValue('android'); From 934b3ccf86d14345324a12364a07b42e9f52807a Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Mon, 25 Sep 2023 11:10:41 -0600 Subject: [PATCH 4/4] style: reuse outputPath --- packages/amplify-codegen/src/commands/types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amplify-codegen/src/commands/types.js b/packages/amplify-codegen/src/commands/types.js index 3a8d88bff..ee28bf6ff 100644 --- a/packages/amplify-codegen/src/commands/types.js +++ b/packages/amplify-codegen/src/commands/types.js @@ -96,7 +96,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, try { if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) { - generate(queryFilePaths, schemaPath, path.join(projectPath, generatedFileName), '', target, '', { + generate(queryFilePaths, schemaPath, outputPath, '', target, '', { addTypename: true, complexObjectSupport: 'auto', });