question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pagination with Filtering in DynamoDB Template

See original GitHub issue

Schema:

type Query {
	getUserTwitterFeed(handle: String!): User
}

type Tweet {
	tweet_id: String!
	tweet: String!
	retweeted: Boolean!
	retweet_count: Int
	favorited: Boolean!
}

type TweetConnection {
	items: [Tweet]
	nextToken: String
}

type User {
	name: String!
	handle: String!
	location: String!
	description: String!
	followers_count: Int!
	friends_count: Int!
	favourites_count: Int!
	followers: [String]
	tweets(limit: Int, nextToken: String): TweetConnection
}

schema {
	query: Query
}

Request Mapping Template:

{
    "version": "2017-02-28",
    "operation": "Query",
    "query": {
        "expression": "handle = :handle",
        "expressionValues": {
            ":handle": {
                "S": "$context.source.handle"
            }
        }
    },    
    "limit": #if($context.arguments.limit) $context.arguments.limit #else 10 #end,
    "nextToken": #if($context.arguments.nextToken) "$context.arguments.nextToken" #else null #end
    #if(${context.arguments.keyword})
    ,"filter": {
        "expression": "contains (#tweet, :keyword)",
        "expressionNames" : {
          	"#tweet" : "tweet"
        },        
        "expressionValues": {
            ":keyword": { "S": "${context.arguments.keyword}" }
        }
      }
    #end   
}

Response Mapping Template:

{
    "items": $util.toJson($context.result.items),
    "nextToken": $util.toJson($context.result.nextToken)
}

Query:

screen shot 2018-01-21 at 6 38 24 pm

I am trying to write a mapping template to paginate tweets with a given filter. My dynamodb table has simple structure: hash key = handle, sort key = tweet_id

When I don’t pass keyword parameter, everything runs fine. But now I added another condition to the mapping template i.e if the keyword is an input then filter tweets by keyword. But I see weird error in the above screenshot. Took reference to add Query Filters from here

Any idea what is happening? Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

7reactions
mattiLeBlanccommented, Apr 16, 2020

I find this whole dynamodb / appsync / filtering stuff unworkable. If you filter you can’t trust the nextToken for pagination, because you don’t know if the next page has results that might be filtered. The only way around it would be to have something in your sortkey that you can sort on so you group the records you want to keep after the filter and you can do some kind of predictive pagination.

We are really hitting limits with Appsync and Dynamo. Imagine having pagination in the middle of a 5 function pipeline resolver. You would have to run the whole pipeline 5 times 😃

3reactions
stock1232commented, Oct 29, 2018

I am also interested in a solution for this. I would like to be able to filter a pagination result before the limit is taken into consideration

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pagination with Filtering using Query Operation in DynamoDB ...
Lets check your example - You have boolean and you have index over that field. Lets say 50% of items are false and...
Read more >
DynamoDB Pagination - The Ultimate Guide (with Example)
Pagination, in general, is a technique to split the data records retrieved from DynamoDB into multiple segments. As a database that supports ...
Read more >
Pagination with Filtering in DynamoDB Template #3209 - GitHub
I would like to be able to filter a pagination result before the limit is taken into consideration. I have already tried Global...
Read more >
Paginating table query results - Amazon DynamoDB
DynamoDB paginates the results from Query operations. With pagination, the Query results are divided into "pages" of data that are 1 MB in...
Read more >
The surprising properties of DynamoDB pagination
The filter expression is run after DynamoDB fetches the results, so it is almost the same as doing it on the client-side, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found