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

Amplify Client generator creating array type for string #506

Open
kannu-19 opened this issue Jan 21, 2025 · 3 comments
Open

Amplify Client generator creating array type for string #506

kannu-19 opened this issue Jan 21, 2025 · 3 comments

Comments

@kannu-19
Copy link

Environment information

Amplify Gen2

Describe the bug

Cx is experiencing issues related to the schema and type handling in his application setup, specifically with how TypeScript interprets the transactionId property in the TransactionMetadata model[schema given below]

The array is being converted into a string with square brackets (showing as "[transaction-id]"), but the desired format should be just "transaction-id" without the brackets. Cx is confused if this is appears to be an issue with how the TypeScript client is being generated.

Reproduction steps

==============
Customer current schema :

Backend

const schema = a.schema({
  Waitlist: a
    .model({
      email: a.string(),
      name: a.string(),
      company: a.string(),
    })
    .authorization((allow) => [allow.publicApiKey().to(["create"])]),
  
  TransactionMetadata: a
    .model({
      transactionId: a.string().required(), // Unique identifier for the transaction
      owners: a.string().array().required(), // User who has access to the transaction
      documentsAwaitingApproval: a.string().array(),
      documentsApproved: a.string().array(),
      progress: a.float().default(0.0),
      address: a.string().default("42 Wallaby Way"),
      city: a.string().default("Katonah"),
      state: a.string().default("NY"),
      zip: a.string().default("10536"),
      amount: a.float().default(1800000.0),
    })
    .authorization(allow => [allow.ownersDefinedIn('owners')]),
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: "apiKey",
    // API Key is used for a.allow.public() rules
    apiKeyAuthorizationMode: {
      expiresInDays: 30,
    },
  },
});

In amplify_outputs.json:

"transactionId": {
"name": "transactionId",
"isArray": false,
"type": "String",
"isRequired": true,
"attributes": []
}

Frontend

await client.models.TransactionMetadata.create(
        {
          transactionId: [transactionId], // comment to force redeployment
          owners: [userId]
        },
        {
          authMode: 'userPool',
        }
      );

==============
Approches we tried to resolve it:

  1. Tried adding/removing attributes in the data model as well as changing the name of the data model, still the data is being saved as array.

Is the issue with the client generator?

@ykethan
Copy link
Member

ykethan commented Jan 22, 2025

Hey,👋 thanks for raising this! I'm going to transfer this over to our data repository for better assistance 🙂

@ykethan ykethan transferred this issue from aws-amplify/amplify-backend Jan 22, 2025
@ptimons44
Copy link

Hello,

Here is the additional requested information:

>> npx ampx info
(node:32448) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
System:
  OS: macOS 14.1.1
  CPU: (10) arm64 Apple M1 Max
  Memory: 138.50 MB / 32.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 23.2.0 - /opt/homebrew/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 10.9.0 - /opt/homebrew/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.5.1
  @aws-amplify/backend: 1.13.0
  @aws-amplify/backend-auth: 1.4.2
  @aws-amplify/backend-cli: 1.4.7
  @aws-amplify/backend-data: 1.4.0
  @aws-amplify/backend-deployer: 1.1.14
  @aws-amplify/backend-function: 1.12.0
  @aws-amplify/backend-output-schemas: 1.4.0
  @aws-amplify/backend-output-storage: 1.1.4
  @aws-amplify/backend-secret: 1.1.5
  @aws-amplify/backend-storage: 1.2.4
  @aws-amplify/cli-core: 1.2.2
  @aws-amplify/client-config: 1.5.5
  @aws-amplify/deployed-backend-client: 1.5.0
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.12
  @aws-amplify/platform-core: 1.6.0
  @aws-amplify/plugin-types: 1.8.0
  @aws-amplify/sandbox: 1.2.9
  @aws-amplify/schema-generator: 1.2.6
  aws-amplify: 6.12.1
  aws-cdk: 2.176.0
  aws-cdk-lib: 2.176.0
  typescript: 5.7.3
(node:32480) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!                                                                                                                      !!
!!  This software has not been tested with node v23.2.0.                                                                !!
!!  Should you encounter odd runtime issues, please try using one of the supported release before filing a bug report.  !!
!!                                                                                                                      !!
!!  This software is currently running on node v23.2.0.                                                                 !!
!!  As of the current release of this software, supported node releases are:                                            !!
!!  - ^22.0.0 (Planned end-of-life: 2027-04-30)                                                                         !!
!!  - ^20.0.0 (Planned end-of-life: 2026-04-30)                                                                         !!
!!  - ^18.0.0 (Planned end-of-life: 2025-04-30)                                                                         !!
!!                                                                                                                      !!
!!  This warning can be silenced by setting the JSII_SILENCE_WARNING_UNTESTED_NODE_VERSION environment variable.        !!
!!                                                                                                                      !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
No AWS environment variables
No CDK environment variables

Finally, some additional information that I think would be helpful, is that the following works locally, but the TypeScript compiler complains, making it unable to be deployed to amplify.

try {
      await client.models.TransactionMetadata.create(
        {
          transactionId: transactionId, // comment to force redeployment
          owners: [userId],
          address: "string",
          amount: 0,
          city: "string",
          state: "string",
          zip: "string",
        },
        {
          authMode: 'userPool',
        }
      );
      await fetchTransactions();
    } catch (error) {
      console.error("Error creating transaction:", error);
    }

The compiler believes that all types should be string[], even though this is not the case.

Thank you for your help,
Patrick

@chrisbonifacio
Copy link
Member

Hi @kannu-19 👋 can you run the following command and let us know what version of @aws-amplify/data-schema you have installed on your project?

npm update @aws-amplify/data-schema

@stocaaro stocaaro added the bug Something isn't working label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants