aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.32k stars 248 forks source link

Cannot run appsync codegen for existing Appsync API #2449

Open saculNiot opened 1 year ago

saculNiot commented 1 year ago

Description

I have an existing AppSync API that I need to use in a new flutter app. After initializing amplify in my project, i run the command "amplify add codegen --apiId xxxxxxxxxxxxxxxxx"

This results in a message saying "Successfully added API xxxx to your amplify project" but then says "Flutter Only supports the command $amplify codegen models."

When I run amplify codegen models, it says No Appsync API configured. Please add an API. When I run amplify status, it shows the API ,but with no provider plugin. If I update the backend-config.json in amplify/backend directory with the awscloudformation provider plugin, and run either amplify push or amplify codegen models it tells me there is no API folder in amplify/backend

Has anyone had success getting codegen to work for an existing appsync api with Flutter? I have tried everything.

Categories

Steps to Reproduce

No response

Screenshots

No response

Platforms

Android Device/Emulator API Level

No response

Environment

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.9, on macOS 13.0.1 22A400 darwin-arm, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] VS Code (version 1.73.1)
[✓] VS Code (version 1.63.0)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

Dependencies

dependencies:
- amplify_api 0.6.10 [amplify_api_android amplify_api_ios amplify_core amplify_flutter aws_common collection flutter meta plugin_platform_interface]
- amplify_auth_cognito 0.6.10 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_core aws_common collection flutter meta plugin_platform_interface]
- amplify_flutter 0.6.10 [amplify_core amplify_datastore_plugin_interface amplify_flutter_android amplify_flutter_ios aws_common collection flutter meta plugin_platform_interface]
- another_flushbar 1.12.29 [flutter]
- cupertino_icons 1.0.5
- flex_color_scheme 6.1.1 [flex_seed_scheme flutter meta]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- flutter_markdown 0.6.13 [flutter markdown meta path]
- flutter_screenutil 5.6.0 [flutter]
- flutter_spinkit 5.1.0 [flutter]
- google_fonts 3.0.1 [flutter http path_provider crypto]
- provider 6.0.4 [collection flutter nested]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- string_validator 0.3.0

Device

IPhone 14

OS

IOS 14

Deployment Method

Amplify CLI

CLI Version

No response

Additional Context

No response

Amplify Config

const amplifyconfig = ''' { "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "auth": { "plugins": { "awsCognitoAuthPlugin": { "UserAgent": "aws-amplify-cli/0.1.0", "Version": "0.1.0", "IdentityManager": { "Default": {} }, "CognitoUserPool": { "Default": { "PoolId": "", "AppClientId": "", "Region": "" } }, "Auth": { "Default": { "OAuth": { "WebDomain": "", "AppClientId": "", "SignInRedirectURI": "http://localhost:3000/", "SignOutRedirectURI": "http://localhost:3000/signout", "Scopes": [ "email", "openid", "profile" ] }, "authenticationFlowType": "USER_SRP_AUTH", "socialProviders": [], "usernameAttributes": [ "EMAIL" ], "signupAttributes": [], "passwordProtectionSettings": { "passwordPolicyMinLength": 8, "passwordPolicyCharacters": [ "REQUIRES_LOWERCASE", "REQUIRES_UPPERCASE" ] }, "mfaConfiguration": "OFF", "mfaTypes": [], "verificationMechanisms": [ "EMAIL" ] } } } } } }''';

HuiSF commented 1 year ago

Hi @saculNiot thanks for opening this issue. Sorry for the confusion - first of all, I don't think amplify add codegen works with Amplify platform libraries, it's used with the AWS AppSync SDKs. Second, amplify codegen models command requires adding API category to your Amplify App (which creates the API folder in amplify/backend you saw in the error message).

For your use case - using existing AppSync resource with Amplify platform libraries, you should follow this documentation. With this, you can start communicating with your API by manually composing GraphQL document string.

I haven't been able to generate native models directly using the CLI. If you'd like to leverage the native model helper, you probably need extra manual steps. Here's what I tried as a workaround

  1. Get a copy of the model schema you composed when initially created the AppSync API, make sure the schema conform the specification of data modeling
  2. run amplify add api following the steps
  3. Now you should be able to find this file <your-project-root>/amplify/backend/api/<your-api-name>/schema.graphql
  4. Empty the file and copy your existing API schema into this file and save
  5. run amplify codegen models, you should get the native models under lib/models as well as the ModelProvider

Please note if you run amplify push it will create a new AppSync API, so before you push, you many want to do amplify remove api.

I will dig around to see if there is a simple way to do this. Will update.

saculNiot commented 1 year ago

Thanks for your reply. Sure, I will go and try it out as well. Waiting for your good news. I think this feature is quite essential XD as I think most web developers who are familiar with Appsync in their web projects will need this as well in mobile development. Anyway, appreciate your help.

HuiSF commented 1 year ago

It seems amplify add codegen works correctly with amplify-swift. There may be a feature gap here in CLI not supporting amplify-flutter in this use case.

Nic-DB commented 1 year ago

I have the same issue the workaround works but is not ideal. An update to the CLI to support the codegen with an existing endpoint also in flutter would be perfect

oyatrides commented 1 year ago

Could we have an update on support an equivalent of amplify codegen models for non-amplify appsync apis for flutter please ?

abdallahshaban557 commented 1 year ago

Hello everyone, we are investigating supporting this. We do not have a timeline yet, but we will provide updates when we have next steps identified.

Lock128 commented 1 year ago

Thats great.

Can we then also fix the problem that codegen requires @model annotations which is then not supported by AppSync in the same code?