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 77 forks source link

Adding a field with required value creates issues #2710

Open cabcookie opened 3 months ago

cabcookie commented 3 months ago

Environment information

System:
  OS: macOS 14.5
  CPU: (10) arm64 Apple M1 Pro
  Memory: 1.74 GB / 32.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
  Yarn: undefined - undefined
  npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-cli: 1.1.0
  aws-amplify: 6.4.0
  aws-cdk: 2.148.1
  aws-cdk-lib: 2.148.1
  typescript: 5.5.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Data packages

personal-crm@1.25.0 /Users/carskoch/Development/personal-crm
├─┬ @aws-amplify/backend-cli@1.1.0
│ └─┬ @aws-amplify/schema-generator@1.2.0
│   └── @aws-amplify/graphql-schema-generator@0.9.2
└─┬ @aws-amplify/backend@1.0.4
  └─┬ @aws-amplify/backend-data@1.1.0
    └── @aws-amplify/data-construct@1.9.1

Description

I have a model "Activity" in my schema definition. I created several records in the database. Now, I wanted to add a property to the model "hasOpenTasks". I wanted to add a secondary index to this property so I had to add the .required() option to it. However, I have a lot of records where this field is missing. I want Amplify to treat it with the value "false" per default (i.e., hasOpenTasks: a.string().required().default("false")). However, when I retrieve data from the table I receive the following error:

[
  {
    "path": ["getActivity", "hasOpenTasks"],
    "locations": null,
    "message": "Cannot return null for non-nullable type: 'String' within parent 'Activity' (/getActivity/hasOpenTasks)"
  }
]

I need a way to add required fields later to the project. My recommendation is to raise an exception when you add a field to the model which is required but doesn't have a default value. I guess for now, I need to manually add the field to each record by running a script. If it is not possible to add a "default" value for new required fields, I appreciate any functionality that applies changes to existing records when I add required fields so I do not need to run scripts manually.

AnilMaktala commented 3 months ago

Hey @cabcookie, Thanks for bringing this up. It makes sense to update existing records when adding a default for a required field. I'll review this with the engineering team and get back to you.