Skip to content

Multiple Sort Key Fields on AWS Amplify GraphQL Index Not Working as Expected

0

Hello, I've encountered an issue with AWS GraphQL index service. When I create an index with multiple sortKeyFields like this: @index( name: "listTopicsByTypeAndRegion" queryField: "listTopicsByTypeAndRegion" sortKeyFields: ["topicType", "regionLevelThree"] ) And try to query in AppSync: query MyQuery { listTopicsByTypeAndRegion( status: ACTIVE topicTypeRegionLevelThree: {eq: {topicType: "1", regionLevelThree: "2-1-1"}} sortDirection: ASC nextToken: null ) { nextToken items { id status topicType regionLevelThree } } } I always get empty results: { "data": { "listTopicsByTypeAndRegion": { "nextToken": null, "items": [] } } }

The query returns no data even without the topicTypeRegionLevelThree parameter. However, when I remove one of the sortKeyFields, it works normally. The AWS documentation doesn't provide detailed explanation about this behavior.

I found a similar issue (#1438) on GitHub but it remains unresolved: https://github.com/aws-amplify/amplify-category-api/issues/1438

Has anyone encountered similar issues or knows how to resolve this? Any help would be appreciated.

asked 2 years ago616 views

1 Answer
0

The issue you're experiencing with multiple sort key fields in your AWS AppSync GraphQL API is not uncommon. When using multiple sort key fields in an @index directive, there are some limitations and specific behaviors to be aware of.

First, it's important to note that when you define multiple sort key fields, they are combined into a single composite sort key. In your case, "topicType" and "regionLevelThree" are combined into a composite sort key named "topicTypeRegionLevelThree".

When querying an index with multiple sort key fields, you need to provide conditions for all the fields in the order they are defined. In your query, you're using the "eq" operator, which requires an exact match for both fields. This might be too restrictive and could be the reason you're getting empty results.

To troubleshoot this issue:

  1. Ensure that you have data in your table that exactly matches the conditions you're querying for.

  2. Try using a more flexible query condition. Instead of "eq", you could use "beginsWith" to match partial values:

    topicTypeRegionLevelThree: {beginsWith: {topicType: "1", regionLevelThree: "2-1-1"}}

  3. If you need to query based on only one of the sort key fields, you might need to create separate indexes for each field.

  4. Double-check that the data types of your sort key fields match what's defined in your schema.

  5. Verify that the status field is indeed part of your partition key as implied by your query.

If these suggestions don't resolve the issue, you might be encountering a limitation or bug in the current implementation of multiple sort key fields in AWS Amplify. In that case, you may need to consider alternative index designs or reach out to AWS support for further assistance.
Sources
Query - Amazon DynamoDB
Use Amplify with MySQL | AWS re:Post
Unauthorized running AppSync Query | AWS re:Post

answered 2 years ago

  • Thank you for the suggestions, but I've already tried all these approaches without success:

    I've verified the data exists and matches the query conditions I've tested both eq and beginsWith operators I've confirmed the data types are correct I've even tried querying without any conditions The core issue seems to be that queries with multiple sort key fields (sortKeyFields) consistently return empty results, while the same queries work perfectly when using only one sort key field. This behavior appears to be systematic rather than data-related.

    Looking at GitHub issue #1438, others have reported the same problem, suggesting this might be a limitation or bug in the current AWS Amplify implementation.

    Could you provide any specific working examples of multiple sort key fields queries? Or suggest alternative approaches that have been proven to work?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.