amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

E-commerce like multiple filter operations using appsync resolver

Open artista7 opened this issue 7 years ago • 7 comments

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).

artista7 avatar Sep 08 '19 10:09 artista7

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

artista7 avatar Sep 08 '19 10:09 artista7

@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 avatar Sep 08 '19 10:09 artista7

@artista7 Have you looked at the @searchable directive to solve this issue? It is quite flexible in filtering and searching items.

RossWilliams avatar Sep 11 '19 14:09 RossWilliams

@RossWilliams using elastic search along with separate EC2 instances adds additional cost and application complexity which is what we are trying to avoid

artista7 avatar Sep 15 '19 04:09 artista7

@artista7 There is a free tier for Elasticsearch service for the first 12 months if that makes things easier.

RossWilliams avatar Sep 15 '19 21:09 RossWilliams

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.

attilah avatar Sep 30 '19 16:09 attilah

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.

github-actions[bot] avatar May 27 '21 00:05 github-actions[bot]