Amplify-cli: Use amplify codegen without setting up amplify project

Created on 2 Oct 2018  ·  22Comments  ·  Source: aws-amplify/amplify-cli

Is your feature request related to a problem? Please describe.
We have an AppSync project with an AppSync schema set up. We want to use the 'codegen' feature to generate API code or type annotations based on a GraphQL schema and query documents. However, in order to use that, the Amplify CLI requires to set up an Amplify project, which sets up the infrastructure of deploymentBucket, cloudformation stack etc, which we already have. We want to have the amplify codegen without setting up those infrastructures.

Describe the solution you'd like
We setup all infrastructure, appsync schema by ourselves. We run a command like 'amplify add codegen --apiId xxxxxxxxxxxxxxxxxx', it will generate the API.ts file and queries.ts, mutations.ts files locally.

Describe alternatives you've considered
None

Additional context
None

code-gen enhancement feature-request pending-review

Most helpful comment

+1 for this.
We have what I imagine is a typical dev set up of backend separate to frontend.
Backend has created the appsync implementation and has shared the schema, frontend can't generate code without creating their own separate stacks first?
Seems like overkill.

All 22 comments

We will investigate how best we can support codegen without setting up the amplify project.

How ever in the mean time you could try using amplify-graphql-docs-generator for generating statements and aws-appsync-codegen for generation of types . Both these packages have cli interfaces.

The only thing that would be missing compared to that of amplify cli is that you will have to download the introspection schemas

Hi @yuth,

We tried using aws-appsync-codegen but it has an error for generating types for 'AWSDateTime'. We had a thread there and get the suggestion to request a feature here:
https://github.com/awslabs/aws-appsync-codegen/issues/17

@wyjiao Are you using the introspection schema to generate types?

Could you share a sample schema, that fails so that I can dig a little deeper

The same is happening to me. I already have the project deployed in AppSync and I don't want to create a new CloudFormation template. Is there any way in Amplify cli to create a copy of the remote environment, modify it and then publish the changes.

Hi @yuth,
Any structure including AWSDateTime would break the code-gen, for example:
type Data{ localTime: AWSDateTime }

@wyjiao thanks for reporting this. We are in the process of forking aws-appsync-codegen and once its forked we will add support for all AWS scalar types.

Closing this issue. We currently have the ability to run the command 'amplify add codegen --apiId xxxxxxxxxxxxxxxxxx to do codegen for an already provisioned GraphQL API. Also we forked the aws-appsync-codegen package and have support for AWS scaler types.

@kaustavghosh06 Can you point to me where is the forked aws-appsync-codegen package which supports AWS scalar types? The one I am looking at is still 5 months old which will fail for generating 'AWSDateTime' types:
aws-appsync-codegen

It would be great reopening this issues. Because I hope that Amazon team will implement the functionality requested.

@kaustavghosh06 reopening. Let's chat about implementation when I'm back in office.

+1 for this.
We have what I imagine is a typical dev set up of backend separate to frontend.
Backend has created the appsync implementation and has shared the schema, frontend can't generate code without creating their own separate stacks first?
Seems like overkill.

Is there any news on the feature?

We don't have anything to report yet. I have tagged the issue and will speak to the team to get this prioritized

Hi all! Any updates? We started to develop React Native app with
"aws-amplify": "0.4.1",
"aws-amplify-react-native": "0.2.11",
"aws-appsync": "1.7.0",
"aws-appsync-react": "1.2.5",
"aws-sdk": "2.383.0"

And now we want to update. But without setting amplify-cli, because we have backend separately done. Settings of amplify-cli made weird things with app :(

If I understand the issue correctly, I believe you can just copy the generated .graphqlconfig.yml file to your base directory. This will allow you to run amplify codegen without setting up the project.

Assuming my understanding is correct, that basically full admin privs are required just to do codegen when all that should be required is appsync:GetIntrospectionSchema, and there's no obvious way to get around this requirement, this is a very serious flaw in the design of the CLI.

I find this particularly concerning because this is a tool written by AWS. It's my feeling that if you referred this design to any of your colleagues who have a decent understanding of cloud security, I believe they would raise similar concerns.

I'm not saying this to shame anyone - it's just that this flaw can put your users in a really bad position as their application matures and the project develops separate front-end and back-end teams. I'm currently responsible for AWS infrastructure on a project and now I have to choose between allowing much broader IAM access than I am comfortable with, or telling a mobile development team that they will have to move to Apollo, which I really hope generates very similar code to Amplify, or else they will have to do significant rework.

@jkodroff You could potentially limit your different teams to assume different IAM roles (With different set of policies) for more advanced scenarios and also have advanced security settings like token validation/expiration.
We have covered one scenarios out here - https://aws-amplify.github.io/docs/cli-toolchain/usage#step-by-step-guide-to-create-and-assume-an-iam-role

@kaustavghosh06 The docs above explain how to create and assume a role using the AWS CLI (which I already know how to do), but they do not seem to explain how to use that role to avoid having to set up broad permissions just to do codegen with Amplify.

Could you maybe provide some additional info on how to use an existing role?

Is there any way to use Amplify's codegen feature by itself against an existing AppSync (or other GraphQL) endpoint without granting such broad permissions and deploying actual infrastructure via CloudFormation?

I can't find a way to use codegen without setting up an Amplify project

I added a .graphqlconfig.yml to my base directory

projects:
  My AppSync App:
    schemaPath: src/graphql/schema.json
    includes:
      - src/graphql/**/*.js
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: javascript
        generatedFileName: ''
        docsFilePath: src/graphql
        region: us-east-1
        maxDepth: 2

...

❯ amplify codegen                                       
You are not working inside a valid amplify project.
Use 'amplify init' in the root of your app directory to initialize your project with Amplify

...

❯ amplify configure codegen
You are not working inside a valid amplify project.
Use 'amplify init' in the root of your app directory to initialize your project with Amplify

...

❯ amplify add codegen --apiId xxxxxxxxxxxxxxxxxxxxxxxxxxx
You are not working inside a valid amplify project.
Use 'amplify init' in the root of your app directory to initialize your project with Amplify

We release support for this as a part of our CLI version 3.8.
Please take a look at Flow 5 - https://aws-amplify.github.io/docs/cli-toolchain/graphql#workflows- out here for more information around codegen without init flow.

@kaustavghosh06 The provided link is dead. could you update it so it can be found :)

Found it after some digging, but still does not allow amplify codegen models without a project setup. :(
https://docs.amplify.aws/cli/graphql-transformer/codegen#workflows

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicksmithr picture nicksmithr  ·  3Comments

MageMasher picture MageMasher  ·  3Comments

gabriel-wilkes picture gabriel-wilkes  ·  3Comments

nason picture nason  ·  3Comments

ReidWeb picture ReidWeb  ·  3Comments