2

When trying to execute the query from the AppSync console in AWS, the following error keeps popping up, as seen in the logs of CloudWatch: 'ValidationException: Query key condition not supported'

I have tried changing the schema with different types and the Global Secondary Index where I swap the Partition Key and Sort Key. Also changing the code to scan rather then query works fine.

Using AWS-CDK I have setup an GraphQL API. For the DynamoDB table I want to use a Global Secondary Index. For that I have setup in my schema.graphql:

type ObjectName {
  id: ID!
  trackTimeStampUtc: Int!
  channel: String!
  ...
}

with:

channel (String!) as Partition Key

trackTimeStampUtc (Int!) as Sort key

as so in Stack.ts:

const table = new ddb.Table(this, 'tableName', {
  billingMode: ddb.BillingMode.PAY_PER_REQUEST,
  timeToLiveAttribute: 'ttl',
  partitionKey: {
    name: 'id',
    type: ddb.AttributeType.STRING,
  },
});

table.addGlobalSecondaryIndex({
  indexName: 'search-index',
  partitionKey: {
    name: 'channel',
    type: ddb.AttributeType.STRING,
  },
  sortKey: {
    name: 'trackTimeStampUtc',
    type: ddb.AttributeType.NUMBER,
  },
});

And in the Lambda handler with the function consuming the Params:

params:  {
  Index: 'search-index',
  TableName: 'tableName',
  KeyConditionExpression: '#ch = :channel and #ts between :start and :end',
  ExpressionAttributeNames: { 
    '#ch': 'channel', 
    '#ts': 'trackTimeStampUtc' 
  },
  ExpressionAttributeValues: { 
    ':channel': 'test', 
    ':start': 1610971398, 
    ':end': 1611667818 
  }
}

Which is sent using await docClient.query(params).promise();

While I am sure I am doing something wrong I just can't seem to figure it out. Setting this up using .scan with some slight adjustment works fine, but having a GSI would be better. Please can anyone help?

Update 27-01 11:52:

I realize that the combination of Partition Key and Sort Key might not be the most efficient for the purpose it serves, regardless I wouldn't have any other combination work anyway. So getting this to work, for me, is understanding how it should work.

Update 29-01 15:18:

The issue is solved. Turned out I had Index in the Params for refering to the index name, instead of IndexName. Changed that fixed the issue and the query worked. Thank you again Peter.

Bob Meijwaard
  • 388
  • 9
  • 20
  • 1
    Can you confirm the parameters you are providing to `docClient.query(...)` are using `IndexName` and not `Index` to identify what index you are querying? – Peter Wagener Jan 28 '21 at 22:53
  • Oh my, I do have been using `Index` (as seen in the Params properties), rather then `IndexName`. Will try it later and report back. Good catch, hopeful it will work. – Bob Meijwaard Jan 29 '21 at 09:02
  • If that continues to fail please update the question -- I'm not deep into AppSync, but the DynamoDB details here look solid. I was able to replicate your query above successfully. – Peter Wagener Jan 29 '21 at 14:02
  • So I have just changed the property `Index` into `IndexName` and the query work straight away. In this case I am the fool, since the documentation on it was also pretty clear. So thank you very much @PeterWagener for finding the culprit. – Bob Meijwaard Jan 29 '21 at 14:17
  • Very welcome! If you dig deeper into using DynamoDB, using TypeScript and V3 of the AWS SDK helps avoid issues like this. The parameters are complex objects at this point, so it’s easy to miss something. – Peter Wagener Jan 30 '21 at 17:44

0 Answers0