Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: codegen --apiId broken state #689

Merged
merged 6 commits into from
Sep 13, 2023
Merged

Conversation

dpilch
Copy link
Member

@dpilch dpilch commented Sep 7, 2023

Description of changes

The customer can end in a broken state if:

  1. amplify codegen add --apiId <api-id>
  2. rm -rf schema.json
  3. amplify codegen

There is no way the customer can use the codegen CLI to download the introspection schema.

$ amplify-dev codegen types
⠋ Generating🛑 ENOENT: no such file or directory, open '/Users/dppilche/amplify/apps/testnoamplify/schema.json'

Resolution: Please report this issue at https://github.com/aws-amplify/amplify-cli/issues and include the project identifier from: 'amplify diagnose --send-report'
Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/
⠹ Generating^Aborted!
$ amplify-dev codegen statements 
Cannot find GraphQL schema file: /Users/dppilche/amplify/apps/testnoamplify/schema.json
$ amplify codegen 
Please download the schema.graphql or schema.json and place in /Users/dppilche/amplify/apps/testnoamplify before adding codegen when not in an amplify project

amplify codegen, amplify codegen types, and amplify codegen statements now download the introspection schema if apiId is set in .graphqlconfig.yml and the project is without amplify init.

Issue #, if available

N/A

Description of how you validated changes

  • Unit testing.
  • E2E tests to come in later pr.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dpilch dpilch changed the base branch from main to codegen-apiid September 8, 2023 19:01
@dpilch dpilch force-pushed the codegen-apiid-broken-state branch from 71bba57 to d40c094 Compare September 8, 2023 19:04
@dpilch dpilch marked this pull request as ready for review September 8, 2023 20:01
@dpilch dpilch requested a review from a team as a code owner September 8, 2023 20:01
@dpilch dpilch changed the title Codegen apiid broken state fix: codegen --apiId broken state Sep 8, 2023
Comment on lines 36 to 40
} else if (project.amplifyExtension.apiId && project.amplifyExtension.region) {
const {
amplifyExtension: { apiId, region },
} = project;
apis = [await getAppSyncAPIInfo(context, apiId, region)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably refactor this check, destructuring statement, and retrieval into a single utility where given a project you return an optional api or something like that, since it's nearly identical to the code in statements.ts

Comment on lines 26 to 30
} else if (projects[0].amplifyExtension.apiId && projects[0].amplifyExtension.region) {
const {
amplifyExtension: { apiId, region },
} = projects[0];
apis = [await getAppSyncAPIInfo(context, apiId, region)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See prior comment on this shared code.


for (const cfg of projects) {
const includeFiles = path.join(projectPath, cfg.includes[0]);
const opsGenDirectory = cfg.amplifyExtension.docsFilePath
? path.join(projectPath, cfg.amplifyExtension.docsFilePath)
: path.dirname(path.dirname(includeFiles));
const schemaPath = path.join(projectPath, cfg.schema);
let region;
let frontend;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this reassigned at all, or can this be converted into a ternary const assignment?

const frontend = withoutInit ? cfg.amplifyExtension.frontend : getFrontEndHandler(context);

Comment on lines 32 to 36
} else if (projects[0].amplifyExtension.apiId && projects[0].amplifyExtension.region) {
const {
amplifyExtension: { apiId, region },
} = projects[0];
apis = [await getAppSyncAPIInfo(context, apiId, region)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar utility here I think.

@@ -4,9 +4,15 @@ const generateIntrospectionSchema = require('./generateIntrospectionSchema');
const downloadIntrospectionSchemaWithProgress = require('./generateIntrospectionSchemaWithProgress');

async function ensureIntrospectionSchema(context, schemaPath, apiConfig, region, forceDownloadSchema) {
const meta = context.amplify.getProjectMeta();
let meta;
let withoutInit = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be unused, unless I'm missing something.

@dpilch dpilch requested a review from alharris-at September 12, 2023 14:21
Base automatically changed from codegen-apiid to noinit-codegen September 13, 2023 21:31
@dpilch dpilch force-pushed the codegen-apiid-broken-state branch from f2dc0a8 to 5325040 Compare September 13, 2023 21:35
@dpilch dpilch merged commit b387177 into noinit-codegen Sep 13, 2023
1 of 2 checks passed
@dpilch dpilch deleted the codegen-apiid-broken-state branch September 13, 2023 21:35
alharris-at pushed a commit that referenced this pull request Sep 14, 2023
alharris-at added a commit that referenced this pull request Sep 19, 2023
…set up locally. (#702)

* feat: codegen add --region (#683)

* fix: download introspection schema when apiId is passed (#684)

* fix: codegen --apiId broken state (#689)

* chore: get existing e2e tests passing (#704)

* feat: add e2e tests for codegen add when in a non-amplify project (#705)

* feat: add e2e tests for codegen add when in a non-amplify project

* chore: add some additional test cases

* noinit codegen from graphqlconfig (#706)

* fix: add missing awaits

* chore: fail typegen if no queries are found

---------

Co-authored-by: Dane Pilcher <dppilche@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants