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

How to iterate over GraphQL models inside override.ts #13465

Closed
xonaman opened this issue Dec 8, 2023 · 3 comments
Closed

How to iterate over GraphQL models inside override.ts #13465

xonaman opened this issue Dec 8, 2023 · 3 comments
Labels
api-graphql Issues related to GraphQL resources in the API category override Issues related to resource override CDK functionality pending-triage Issue is pending triage question General question

Comments

@xonaman
Copy link

xonaman commented Dec 8, 2023

Amplify CLI Version

12.8.2

Question

Hello everyone,

I am trying to apply some settings to all my GraphQL models inside the override.ts file generated via amplify override api:

import { AmplifyApiGraphQlResourceStackTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyApiGraphQlResourceStackTemplate) {
  for (const model in resources.models) {
    resources.models[model].modelDDBTable.deletionProtectionEnabled = true;
    resources.models[model].modelDDBTable.pointInTimeRecoverySpecification = {
      pointInTimeRecoveryEnabled: true,
    };
    resources.models[model].modelDDBTable.timeToLiveSpecification = {
      attributeName: 'ttl',
      enabled: false,
    };
  }
}

However, when trying to push, I get the following error:

robertmainzer@Roberts-Air remonon_app % amplify push -y
✖ There was an error pulling the backend environment staging.
🛑 Executing overrides failed.
Cannot set properties of undefined (setting 'deletionProtectionEnabled')

How does one iterate over all models correctly?

@xonaman xonaman added pending-triage Issue is pending triage question General question labels Dec 8, 2023
@ykethan
Copy link
Member

ykethan commented Dec 8, 2023

Hey @xonaman, thank you for reaching out. On testing this a new application by running
amplify add api
amplify override api

for (const model in resources.models) {
    resources.models[model].modelDDBTable.pointInTimeRecoverySpecification = {
      pointInTimeRecoveryEnabled: true,
    };

    resources.models[model].modelDDBTable.timeToLiveSpecification = {
      attributeName: "ttl",
      enabled: false,
    };

    resources.models[model].modelDDBTable.deletionProtectionEnabled = true;
    console.log(resources.models[model].modelDDBTable); //ensure property is set
  }

the running push did not throw an error
image

@ykethan ykethan added pending-response Issue is pending response from the issue author api-graphql Issues related to GraphQL resources in the API category override Issues related to resource override CDK functionality labels Dec 8, 2023
@xonaman
Copy link
Author

xonaman commented Dec 12, 2023

Turns out that the "ConnectionStack" was also part of the resources.models array, which does indeed not have a "modelDDBTable" property. This code works for me now:

for (const model in resources.models) {
  if (!resources.models[model].modelDDBTable) {
    continue;
  }
  resources.models[model].modelDDBTable.deletionProtectionEnabled = true;
  resources.models[model].modelDDBTable.pointInTimeRecoverySpecification = {
    pointInTimeRecoveryEnabled: true,
  };
  resources.models[model].modelDDBTable.timeToLiveSpecification = {
    attributeName: '_ttl',
    enabled: false,
  };
}

@xonaman xonaman closed this as completed Dec 12, 2023
@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Dec 12, 2023
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-graphql Issues related to GraphQL resources in the API category override Issues related to resource override CDK functionality pending-triage Issue is pending triage question General question
Projects
None yet
Development

No branches or pull requests

2 participants