From caad51047aeb47593c4451c990597bc681cfbe87 Mon Sep 17 00:00:00 2001 From: Al Harris <91494052+alharris-at@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:06:23 -0700 Subject: [PATCH] noinit codegen from graphqlconfig (#706) * fix: add missing awaits * chore: fail typegen if no queries are found --- .../src/commands/generateStatementsAndType.js | 2 +- packages/amplify-codegen/src/commands/statements.js | 2 +- packages/amplify-codegen/src/commands/types.js | 11 +++++++---- .../src/utils/getAppSyncAPIInfoFromProject.js | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/amplify-codegen/src/commands/generateStatementsAndType.js b/packages/amplify-codegen/src/commands/generateStatementsAndType.js index 5ca783ce0..f04b9c240 100644 --- a/packages/amplify-codegen/src/commands/generateStatementsAndType.js +++ b/packages/amplify-codegen/src/commands/generateStatementsAndType.js @@ -34,7 +34,7 @@ async function generateStatementsAndTypes(context, forceDownloadSchema, maxDepth if (!withoutInit) { apis = getAppSyncAPIDetails(context); } else { - const api = getAppSyncAPIInfoFromProject(context, project); + const api = await getAppSyncAPIInfoFromProject(context, project); if (api) { apis = [api]; } diff --git a/packages/amplify-codegen/src/commands/statements.js b/packages/amplify-codegen/src/commands/statements.js index cd66ac064..86184b91d 100644 --- a/packages/amplify-codegen/src/commands/statements.js +++ b/packages/amplify-codegen/src/commands/statements.js @@ -30,7 +30,7 @@ async function generateStatements(context, forceDownloadSchema, maxDepth, withou if (!withoutInit) { apis = getAppSyncAPIDetails(context); } else { - const api = getAppSyncAPIInfoFromProject(context, projects[0]); + const api = await getAppSyncAPIInfoFromProject(context, projects[0]); if (api) { apis = [api]; } diff --git a/packages/amplify-codegen/src/commands/types.js b/packages/amplify-codegen/src/commands/types.js index 6cbd73235..5da70cddf 100644 --- a/packages/amplify-codegen/src/commands/types.js +++ b/packages/amplify-codegen/src/commands/types.js @@ -30,7 +30,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, if (!withoutInit) { apis = getAppSyncAPIDetails(context); } else { - const api = getAppSyncAPIInfoFromProject(context, projects[0]); + const api = await getAppSyncAPIInfoFromProject(context, projects[0]); if (api) { apis = [api]; } @@ -57,7 +57,7 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, const target = cfg.amplifyExtension.codeGenTarget; const excludes = cfg.excludes.map(pattern => `!${pattern}`); - const queries = glob + const queryFiles = glob .sync([...includeFiles, ...excludes], { cwd: projectPath, absolute: true, @@ -73,8 +73,11 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false, return extractDocumentFromJavascript(fileContents, ''); } return fileContents; - }) - .join('\n'); + }); + if (queryFiles.length === 0) { + throw new Error('No queries found to generate types for, you may need to run \'codegen statements\' first'); + } + const queries = queryFiles.join('\n'); const schemaPath = path.join(projectPath, cfg.schema); diff --git a/packages/amplify-codegen/src/utils/getAppSyncAPIInfoFromProject.js b/packages/amplify-codegen/src/utils/getAppSyncAPIInfoFromProject.js index 18c1cb5c5..083f54941 100644 --- a/packages/amplify-codegen/src/utils/getAppSyncAPIInfoFromProject.js +++ b/packages/amplify-codegen/src/utils/getAppSyncAPIInfoFromProject.js @@ -3,7 +3,7 @@ const getAppSyncAPIInfo = require('./getAppSyncAPIInfo'); /* Get AppSync api info if api id and region are avialable. * Otherwise return undefined. */ -function getAppSyncAPIInfoFromProject(context, project) { +async function getAppSyncAPIInfoFromProject(context, project) { if (project.amplifyExtension.apiId && project.amplifyExtension.region) { const { amplifyExtension: { apiId, region },