aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 79 forks source link

A hyphen in an enumeration value in a Amplify data resource.ts cause generation phase to fail. #2669

Open sbelbin opened 4 months ago

sbelbin commented 4 months ago

Environment information

> amplify-vite-react-template@0.0.0 npx
> ampx info

System:
  OS: macOS 14.5
  CPU: (11) arm64 Apple M3 Pro
  Memory: 417.50 MB / 18.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.11.0 - /usr/local/bin/node
  Yarn: 1.22.21 - /usr/local/bin/yarn
  npm: 10.6.0 - /usr/local/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 1.0.2
  @aws-amplify/backend-cli: 1.0.3
  aws-amplify: 6.3.4
  aws-cdk: 2.143.0
  aws-cdk-lib: 2.143.0
  typescript: 5.4.5

> amplify-vite-react-template@0.0.0 npx
> cdk doctor --no-notices --  --no-color

AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

In amplify/data/resource.ts file, I defined a field having an enumeration types in which put string values.

const schema = a.schema({ Downloads: a .model({ ... kind: a.enum(['aws-s3', 'azure-blob']), However, when a string value contains the hyphen - character the error during the generator phase is cryptic (see below). For some reason the generation phase is attempting to perform an mathematical operation on the enumeration strings.

AmplifyError [InvalidSchemaError]: Syntax Error: Invalid number, expected digit but got: "s". 58 at DataGenerator.generateContainerEntry (/codebuild/output/src3878792534/src/amplify-vite-react-template/node_modules/@aws-amplify/backend-data/src/factory.ts:165:13)

Fortunately, I can workaround this by replacing hyphen - character by an underscore _.

However, this is a bug during parsing of the enumeration values.

P.S. I'll state that the failure of the error messages not providing the text or line numbers which fail to parse made the experience that much more unpleasant. Especially since "s" is rather a common letter.

edwardfoyle commented 4 months ago

Hi @sbelbin, this fails because dashes are not valid enum values in the GraphQL spec. However, as you mention there is an opportunity to throw a more descriptive error message in this case. Marking as a feature request to improve the error message.