amplify-js: Data live in DynamoDB however AppSync list query doesn't show some of the data
Describe the bug I have an AWS Amplify app in which I add a GraghQL backend and create the model like this:
type Item @model @auth(rules: [{allow: owner}]) {
id: ID!
inventoryType: String!
itemName: String!
imgKey: String
}
Inside my app I allow users to create an item via submitting a form, by calling API.graphqlOperation(createItemMutation, newItem). This works well, because after users submit the form, I can find new items be created in DynamoDB.
After this, I go to AppSync console, I login via Cognito user pool as user A, the following query doesn’t show some of created items:
query {
listItems {
items {
id
inventoryType
itemName
}
}
}
What I get:
{
"data": {
"listItems": {
"items": [
{
"itemName": "2015 book"
},
{
"itemName": "Sundaya 0845"
},
...
]
}
}
}
Some newly created item will be shown, while some not (e.g., one with item name “Sunday 0845 2”) - and I guarantee they are from the same user so that auth is not the root cause. I cannot figure out the show/not show pattern. However, I can always show the created item by get with id:
query {
getItem(id: "72eee5e9-c1f5-4860-8a52-d846ec7d7a54") {
id
inventoryType
itemName
}
}
What I get:
{
"data": {
"getItem": {
"id": "72eee5e9-c1f5-4860-8a52-d846ec7d7a54",
"inventoryType": "apparels",
"itemName": "Sunday 0845 2"
}
}
}
To Reproduce I am not sure how can one reproduce. The result is somewhat un-deterministic.
You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (3 by maintainers)
This has to be a joke. Working with dynamoDb along with appsync in amplify always seem to be doing un-intuitive things. If i’m passing filter expression along with limit, i would want to limit the filtered result instead of filtering the limited result. Can anyone provide an alternate resolver mappings? @YikSanChan
Current default mapping templates in amplify are : - query.listRounds.req.vtl:-
#set( $limit = $util.defaultIfNull($context.args.limit, 10) ) #set( $ListRequest = { “version”: “2017-02-28”, “limit”: $limit } ) #if( $context.args.nextToken ) #set( $ListRequest.nextToken = “$context.args.nextToken” ) #end #if( $context.args.filter ) #set( $ListRequest.filter = $util.parseJson(“$util.transform.toDynamoDBFilterExpression($ctx.args.filter)”) ) #end #if( !$util.isNull($modelQueryExpression) && !$util.isNullOrEmpty($modelQueryExpression.expression) ) $util.qr($ListRequest.put(“operation”, “Query”)) $util.qr($ListRequest.put(“query”, $modelQueryExpression)) #else $util.qr($ListRequest.put(“operation”, “Scan”)) #end $util.toJson($ListRequest)
and query.listRounds.res.vtl:- $util.toJson($ctx.result)
Hey, @manueliglesias I believe I figured out the reason.
I list items filtered by inventory type and I expect to apply the limit after I get all items fit the criteria (
{inventoryType: {eq: "books"}}). However, dynamodb apply the limit before I get all items. Same issue as described here.Any walkaround other than setting a very large limit and pray it works? Thanks!
Why is this closed? I have the same issue as @s-hood. I have set the limit way above my table size and can’t find certain results. Any ideas?
This could be because AppSync limits the amount of iterations for VTL forEach loops to 1,000. So, limit values higher than this won’t have any effect. Instead, you should keep limit to 1,000 and paginate through your results with the nextToken value returned in the response.
Agreed @artista7. it’s a critical and common use case that we’d want to sort by the most recent 1000 items and have that returned. Postfiltering will result in gaps in our users’ histories that will make them (and us) pretty unhappy.
@manueliglesias @YikSanChan I’m new to all this so apologies if I have this confused. I have set a limit well beyond the size of my table (so that all items would be retrieved before the filter is applied), but still do not get all the results matching my filter (I get 4 of the 18 that are in the table). Would paginating results yield more items than setting a large limit, or are these the same thing?
Hi @YikSanChan
It could be that you need to paginate through the results of your
listItemsquery. Relations and pagination might be a good way to start.Also take a look at this helpful medium post. It has this helpful bit:
Let us know if pagination was not the issue