1

This is a similar question to this one, but since no one answered it I want to see if I can get some answers about it.

I am starting to explore graphql and amplify since firestore has some limitations that I consider to restrictive to my application. But I have come to this situation where I want to make a query with limited results and the outcome is not what I expected. My database has only 13 records. And I need to get 12 elements each time.

So, I if I run this query:

query MyQuery {
  listItems(filter: {type: {eq: "ropa"}}) {
    items {
      price
      status
      type
      id
      name
    }
    nextToken
  }
}

This will return 4 items with a type value that equals to "ropa". So far so good. But if I add a limit of 12, like this:

query MyQuery {
  listItems(filter: {type: {eq: "ropa"}}, limit: 12) {
    items {
      price
      status
      type
      id
      name
    }
    nextToken
  }
}

I will only get 3 of those results and a nextToken, so I will have to pass the same query again but this time with the nextToken value to get the last result.

What I understand that is happening, is that when you limit your results, the query first takes the amount of elements specified in your limit and filters them to match your query.

In a scenario where I have to get 12 elements to fill the screen every time I reach the end of the list, I would have to call this query multiple times and would have to device a workaround to fill a temporary list so when it makes it to 12 items then I push them into my array and so on. And if I add a sort direction or other filters, the results reduce and the process becomes more complicated and inefficient to my perspective.

Is this the only way to do so?

Carlos Andres
  • 89
  • 2
  • 14

1 Answers1

0

I'm not sure how you connect graphql and dynamodb, so I can't really comment on that.

However, depending on how you've structured your dynamo tables, you have to adhere by the following strict limitation:

  • You can query by partition key and sort key.
  • You have to specify the partition key
  • You can do some limited dynamic queries on the sort key, like begins_with, larger/smaller than, etc.

If you need to do additional filtering, you can't do that "server side" in Dynamo. So in the cases where your dynamodb table does not match your access patterns fully, you need to fetch a bunch of extra data to the client side and do the filtering there.

If that becomes cumbersome and slow, you need to redesign your tables so that it fits your access patterns better and enables you to do more work in the PK/SK filtering end of the query.

August Lilleaas
  • 54,010
  • 13
  • 102
  • 111