Skip to content

Conversation

@ahmedhamouda78
Copy link
Member

@ahmedhamouda78 ahmedhamouda78 commented Oct 20, 2025

Description of changes

This PR adds projection support for the @index directive in Amplify GraphQL API. The changes enable developers to specify which fields should be projected in DynamoDB Global Secondary Indexes (GSI) when using the @index directive, providing better control over query performance and cost optimization.

Key changes:

  • Enhanced index directive to support projection configuration
  • Updated GraphQL index transformer to handle projection parameters
  • Modified resolvers to work with projected fields
CDK / CloudFormation Parameters Changed

Issue #, if available

aws-amplify/amplify-data#604

Description of how you validated changes

  • Added unit tests covering projection scenarios in amplify-graphql-index-transformer.test.ts
  • Verified snapshot tests pass with updated index directive behavior
  • Tested resolver functionality with projected fields in sample app

Checklist

  • PR description included
  • yarn test passes
  • E2E test run linked
  • Tests are changed or added
  • Any CDK or CloudFormation parameter changes are called out explicitly

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Member

@svidgen svidgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure offhand what is or isn't required for the AppSync/resolver side of this. But, I suspect it's more than this. I would actually kind of expect to see some new generated GraphQL types.

Sufficiently thorough e2e tests would give me some confidence this is working as expected either way though.

field: definition,
directive,
} as IndexDirectiveConfiguration,
} as Required<IndexDirectiveConfiguration>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Quick question - just trying to understand why we're setting this as Required<> when IndexDirectiveConfiguration properties are defined as optional. Maybe some comments could be added to explain this decision in the file

Copy link
Member Author

@ahmedhamouda78 ahmedhamouda78 Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to the introduction of the possibly undefined projection property in the IndexDirectiveConfiguration type (found here). The getArguments function signature is getArguments = <T>(defaultValue: Required<T>,...), so the first argument has to be cast as Required.

const partitionKeyType = attrDefs.find((attr) => attr.attributeName === partitionKeyName)?.attributeType ?? 'S';
const sortKeyType = sortKeyName ? attrDefs.find((attr) => attr.attributeName === sortKeyName)?.attributeType ?? 'S' : undefined;

const projectionType = projection?.type || 'ALL';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to check if this is intended.

js allows any false values with the || operator. That is, if we had an undefined value, then it would default to ALL (as we want). However, empty or invalid strings, or null values would also end up defaulting to ALL from my understanding. Do we want to add checks for correct types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to keep this as loose as possible while defaulting to ALL in most error cases.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be having stricter checks. In the case an unintended value is sent, we want to catch that ideally instead of defaulting to ALL (and not letting the user know). @svidgen what do you think?

Ahmed Hamouda added 3 commits November 4, 2025 13:39
- Add comprehensive E2E tests for KEYS_ONLY, INCLUDE, and ALL projection types
- Tests validate GSI creation and query functionality for each projection type
- Addresses reviewer feedback requesting E2E test coverage
@ahmedhamouda78 ahmedhamouda78 force-pushed the feat/support-gsi-projection-types branch from 8ac302f to 24d86a1 Compare November 4, 2025 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants