Search code examples
amazon-dynamodbgraphqlaws-appsync

AppSync DynamoDb, provided key element does not match the schema


I'm trying to create a custom query type in AppSync to query on a field other than the default id.

Note: I don't have a sort key or a global secondary index configured.

Data source is connected to DynamoDB GameStats table. The record exists in the table but when I query, I get "The provided key element does not match the schema".

query getGameByMeetupId {
  getGameByMeetupId(meetupId: "259028625") {
    name
    field
    year
    month
  }
}

Query: getGameByMeetupId(meetupId: String): GameStats

Request mapping template:

    {
        "version": "2017-02-28",
        "operation": "GetItem",
        "key": {
            "meetupId": $util.dynamodb.toDynamoDBJson($ctx.args.meetupId)
        }
    }

Response mapping template: $utils.toJson($ctx.result)

Error message:

{
  "data": {
    "getGameByMeetupId": null
  },
  "errors": [
    {
      "path": [
        "getGameByMeetupId"
      ],
      "data": null,
      "errorType": "DynamoDB:AmazonDynamoDBException",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: MC71RFG6U85PBICS2OA714TLB7VV4KQNSO5AEMVJF66Q9ASUAAJG)"
    }
  ]
}

Solution

  • To use a DynamoDB Query, you have to specify a value for the partition key.

    You mentioned your table has a 'default id' attribute. Is that your partition key? If so, you have two choices:

    1) Change the Query to a Scan, and specify a filter on the meetupId attribute

    2) Add a GSI, setting meetupId as the partition key, and then update the Query you already have to target the GSI, not the base table