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

Inability to Sort by Multiple Keys or Define Custom Sorting in GraphQL Queries #3086

Closed
amirpoudel opened this issue Dec 19, 2024 · 2 comments
Assignees
Labels
data-schema question Further information is requested

Comments

@amirpoudel
Copy link

Environment information

npx ampx info

Data packages

npm list @aws-amplify/data-construct @aws-amplify/graphql-schema-generator

Description

Currently, I am unable to sort data based on multiple keys or specify which key should be used for sorting in GraphQL queries. While it's possible to create indexes with sort keys, the approach is restrictive and requires separate indexes for each sorting field, which becomes cumbersome and unscalable.

Use Case

My application requires sorting ticket data based on different fields in ascending or descending order, depending on the query. These fields include:

  1. createdAt
  2. totalPurchaseAmount
  3. ticketRedemptionDateTime

Approach Taken

I attempted to create separate indexes and query fields for each sort key, as shown below:

**index('type').sortKeys(['createdAt']).queryField("listTicketByTypeCreatedAt"),
index('type').sortKeys(['ticketRedemptionDateTime']).queryField("listTicketByTypeRedemptionDateTime"),
index('type').sortKeys(['totalPurchaseAmount']).queryField("listTicketByTypeTotalPurchaseAmount"),

index('eventId').sortKeys(['createdAt']).queryField("listTicketByEventIdCreatedAt"),
index('eventId').sortKeys(['totalPurchaseAmount']).queryField("listTicketByEventIdTotalPurchaseAmount"),
index('eventId').sortKeys(['ticketRedemptionDateTime']).queryField("listTicketByEventIdRedemptionDateTime"),

index('organizerId').sortKeys(['createdAt']).queryField("listTicketByOrganizerIdCreatedAt"),
index('organizerId').sortKeys(['totalPurchaseAmount']).queryField("listTicketByOrganizerIdTotalPurchaseAmount"),
index('organizerId').sortKeys(['ticketRedemptionDateTime']).queryField("listTicketByOrganizerIdRedemptionDateTime"),**

Problems with the Current Implementation

Destructive : Adding new fields for sorting requires creating additional indexes and query fields, which leads to significant redundancy and this is destructive operation.

Single Key Limitation: Each query can only sort by one key at a time, even if multiple sort keys are declared.
Lack of Flexibility: I cannot dynamically specify the sorting field or direction (ascending/descending) without creating new indexes.

Expected Behavior

The ability to dynamically sort data by specifying the field and direction in the query (e.g., createdAt, totalPurchaseAmount, or ticketRedemptionDateTime).
Support for specifying sort direction (asc or desc) at runtime.
A scalable approach to sorting that avoids the need for creating separate indexes for each field.
Steps to Reproduce
Define multiple indexes with sort keys in the schema, as shown above.
Attempt to sort data by dynamically specifying a key or direction in a GraphQL query.
Observe that sorting is only possible for predefined keys and requires separate queries.

Environment

Backend: Using GraphQL with auto-generated queries and custom indexes.
Use Case: Ticket management system with sorting requirements based on purchase amount, creation time, and redemption time.

@chrisbonifacio
Copy link
Member

Hi @amirpoudel 👋 thanks for raising this issue. Unfortunately, it's not possible to independently apply a sortDirection to each of these fields. This is a limitation of how DynamoDB handles sort keys and is outside of the control of either Amplify or AppSync.

Please see this comment on GitHub for an explanation. Note - that particular issue deals with filtering, not sorting, but the underlying cause is the same.

@chrisbonifacio chrisbonifacio closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2024
Copy link

This issue is now closed. 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.

@chrisbonifacio chrisbonifacio added data-schema question Further information is requested and removed pending-triage labels Dec 19, 2024
@chrisbonifacio chrisbonifacio self-assigned this Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data-schema question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants