aws-appsync-community icon indicating copy to clipboard operation
aws-appsync-community copied to clipboard

GraphQL batchGet returns 'null' from DynamoDB

Open mmz15 opened this issue 2 years ago • 1 comments

This post comes from spending 3 hours with AWS premium support for the past 2 days, in addition to time spent individually debugging. Perhaps I could get some clarity in this forum.

I'm trying to create a GraphQL API that leverages the batchGet functionality. Regardless of configuration, however, I consistently get the following from the AppSync console:

{
  "data": {
    "batchGet": null
  }
}

I'm using this request:

query MyQuery {
  batchGet(ids: ["value5", "value6"]) {
    growth
  }
}

This is my DynamoDB table: Screen Shot 2021-07-14 at 16 41

And this is my schema.graphql in React:

type Value @model {
  id: ID!
  result: [[Float]]!
  growth: Float
}

type Query {
  batchGet(ids: [ID]): [Value]
}

I went through the recommended tutorial. Also, I have the proper IAM permissions for batch commands.

Finally, here are the respective request and response mappings, taking from the schema section of the AppSync console:

#set($ids = [])
#foreach($id in ${ctx.args.ids})
    #set($map = {})
    $util.qr($map.put("id", $util.dynamodb.toString($id)))
    $util.qr($ids.add($map))
#end

{
    "version" : "2018-05-29",
    "operation" : "BatchGetItem",
    "tables" : {
        "Value-ctwvdp7ygfhyjhq25uvkfzftfy-devtwo": {
            "keys": $util.toJson($ids),
            "consistentRead": true
        }
    }
}
$util.toJson($ctx.result.data.Value)

After days of trying to solve this, I'm beginning to suspect that it's an AppSync bug rather than an error in my code. That said, hopefully I'll stand corrected.

mmz15 avatar Jul 14 '21 20:07 mmz15

Result returns by table name. $util.toJson($ctx.result.data.get("Value-ctwvdp7ygfhyjhq25uvkfzftfy-devtwo"))

P.s. Also I advice to enable debug logging to cloudwatch - it helps a lot

kyptov avatar Jul 16 '21 06:07 kyptov