E-commerce like multiple filter operations using appsync resolver
Hi, I'm building a web app which will show a list of products. I want to show a filter option on UI. I am looking for a direction on to how to implement this. Basically how would ui interaction convert to appsync API payload and how should data resolver be written (to handle multiple & optional filter params).
Also, I have another doubt for a long time. One of our resolvers looks like this:-
#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)
When does Query is used instead of scan? I have not been able to get what modelQueryExpression is.
For example when i do the following query:-
query listSID{
listStudentInDrives(filter:{
driveId:{eq:"testCollege1Drive1"},
registrationCode:{contains:"8"}
}, limit:1000){
items{
id
studentName
registrationCode
studentPhone
}
}
}
it uses scan by default, leading to problems
@kaustavghosh06 If you can help me here, that would be tons of help. Filtering, searching and pagination is the heart of our application. If we could not get around this, we might be moving from appsync to other environment like django/SQL
@artista7 Have you looked at the @searchable directive to solve this issue? It is quite flexible in filtering and searching items.
@RossWilliams using elastic search along with separate EC2 instances adds additional cost and application complexity which is what we are trying to avoid
@artista7 There is a free tier for Elasticsearch service for the first 12 months if that makes things easier.
When does Query is used instead of scan? I have not been able to get what modelQueryExpression is.
When you're using a @key directive that will generate a resolver code that builds up $modelQueryExpression, since the query will run against a LSI or GSI, without that you can only scan the table in DynamoDB.
I'd suggest to take a look at the @key directive, that can help with your queries, and a new feature is already within the CLI which enriches the @connection directive with a fields parameter so it can be used together with @key. The documentation for that is not yet merged, but you can find it here in this PR: https://github.com/aws-amplify/docs/pull/900
As @RossWilliams suggested @searchable enables a lot more scenarios just for searching, so please consider that as well.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.