Home > Software design >  AWS Appsync and DynamoDB - Query using IN operator
AWS Appsync and DynamoDB - Query using IN operator

Time:12-01

I have a table in DynamoDB connected to an appsync API. The table looks something like...

ID State Value
0 NY 100
1 IL 0
2 TX 50

I am trying to use the appsync UI to query the table using the "IN" operator, but this does not seem to be an option via Appsync. For example, I might look to query rows where state is 'TX' or 'IL'. I would think the query might look like...

query MyQuery {
  listResults(filter: {state: {in: ["TX", "IL"]}}) {
    items {
      id
      state
      value
    }
  }

However, it seems like appsync does not support the IN operator. I have options for "eq", "gt", "lt", etc. but no option for the IN operator. Seems like a pretty standard use case but I am struggling to find documentation. If this is the case what are potential workarounds? I have tried using a custom resolver but have been able to find a definitive answer.

CodePudding user response:

The constraint relates to Amplify, which does not support the IN operator out of the box. As you know, the high-level Amplify is abstracts away some of the application plumbing you would otherwise have to do manually in AppSync and your data source. The cost is a lack of flexibility.

Appsync can handle your MyQuery just fine. AppSync does not have an in operator (or gt, lt or whatever) defined either. It's just that in AppSync, you are free to define your types and resolvers as you see fit. Flexibility, but more boilerplate.

If you stick with Amplify, you can modify the schema manually with your new input type. Then implement an Amplify custom resolver and link your new type to it.

  • Related