Skip to content

Commit

Permalink
fix: download on amplify codegen types
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch committed Sep 7, 2023
1 parent de8287c commit 71bba57
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/amplify-codegen/src/commands/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs-extra');

const constants = require('../constants');
const { loadConfig } = require('../codegen-config');
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails } = require('../utils');
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfo } = require('../utils');
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');

Expand All @@ -22,9 +22,18 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
if (frontend !== 'android') {
const config = loadConfig(context, withoutInit);
const projects = config.getProjects();
if (!projects.length && withoutInit) {
context.print.info(constants.ERROR_CODEGEN_NO_API_CONFIGURED);
return;
}
let apis = [];
if (!withoutInit) {
apis = getAppSyncAPIDetails(context);
} else if (projects[0].amplifyExtension.apiId && projects[0].amplifyExtension.region) {
const {
amplifyExtension: { apiId, region },
} = projects[0];
apis = [await getAppSyncAPIInfo(context, apiId, region)];
}
if (!projects.length || !apis.length) {
if (!withoutInit) {
Expand Down Expand Up @@ -70,10 +79,13 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
const schemaPath = path.join(projectPath, cfg.schema);

const outputPath = path.join(projectPath, generatedFileName);
let region;
if (!withoutInit) {
({ region } = cfg.amplifyExtension);
if (apis.length) {
const { region } = cfg.amplifyExtension;
await ensureIntrospectionSchema(context, schemaPath, apis[0], region, forceDownloadSchema);
} else {
if (!fs.existsSync(schemaPath)) {
throw new Error(`Cannot find GraphQL schema file: ${schemaPath}`);
}
}
const codeGenSpinner = new Ora(constants.INFO_MESSAGE_CODEGEN_GENERATE_STARTED);
codeGenSpinner.start();
Expand Down

0 comments on commit 71bba57

Please sign in to comment.