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
81 stars 71 forks source link

Can't set customType as required #2663

Open jpangburn opened 4 days ago

jpangburn commented 4 days ago

Environment information

System:
  OS: macOS 14.5
  CPU: (10) arm64 Apple M1 Pro
  Memory: 155.58 MB / 32.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node
  Yarn: undefined - undefined
  npm: 10.7.0 - ~/.nvm/versions/node/v22.2.0/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 1.0.3
  @aws-amplify/backend-cli: 1.0.4
  aws-amplify: 6.3.7
  aws-cdk: 2.147.0
  aws-cdk-lib: 2.147.0
  typescript: 5.4.5
AWS environment variables:
  AWS_PROFILE = amplify-deploy
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Data packages

entrancecode@0.0.0 /Users/jpangburn/Documents/vsc_workspaces/entrancecode
├─┬ @aws-amplify/backend-cli@1.0.4
│ └─┬ @aws-amplify/schema-generator@1.0.0
│   └── @aws-amplify/graphql-schema-generator@0.8.6
└─┬ @aws-amplify/backend@1.0.3
  └─┬ @aws-amplify/backend-data@1.0.2
    └── @aws-amplify/data-construct@1.8.5

Description

Suppose I have the following model or customType:

NotificationSubscription: a
    .customType({
      endpoint: a.string().required(),
      keys: a.customType({
        p256dh: a.string().required(),
        auth: a.string().required(),
      }),
    }),

I want the keys property to be required, but there's no required() method on customType. So the only way to achieve this is to extract it to its own customType and use a ref to it:

  SubscriptionKeys: a
    .customType({
      p256dh: a.string().required(),
      auth: a.string().required(),
    }),
  NotificationSubscription: a
    .customType({
      endpoint: a.string().required(),
      keys: a.ref('SubscriptionKeys').required(),
    }),

As discussed heavily in other issues, this is also the only way to have a nested customType be an array or to set authorization rules on it.

If a data model is even moderately hierarchical this rapidly becomes hard to read, hard to maintain, and generally unwieldy. I think this really needs to be setup so we can do:

NotificationSubscription: a
    .customType({
      endpoint: a.string().required(),
      keys: a.customType({
        p256dh: a.string().required(),
        auth: a.string().required(),
      }).array().required().authorization(), // etc.
    }),

If it's possible with the verbose syntax, it should be possible with simplified syntax allowing a more complicated model that's stored in one table to be expressed much more succinctly.

Thank you for your consideration!

chrisbonifacio commented 3 days ago

Hi @jpangburn 👋 thanks for raising this issue. I'll mark it as a feature request for the team to consider since it seems more like a potential improvement to DX than a bug.