franco phong
franco phong

Reputation: 2219

AppSync + DynamoDB: Query with filter doesn't return all valid items

I have Appsync API connecting to a Dynamo table.

Dynamo table has data : ("id" is key and "year" is sort key)

|--------------|-------------|-------------|-------------|-------------| 
|      id      |     year    |     name    |    class    |   subject   |    
|--------------|-------------|-------------|-------------|-------------|
|   001        |    2017     |     Tom     |     E1      |     Math    |
|--------------|-------------|-------------|-------------|-------------|
|   002        |    2017     |     Mary    |     E1      |     Math    |
|--------------|-------------|-------------|-------------|-------------|
|   003        |    2017     |     Peter   |     E1      |     Math    |
|--------------|-------------|-------------|-------------|-------------|

the schema

type Query {
    listStudents(filter: TableStudentFilterInput, limit: Int, nextToken: String): StudentConnection
}

type StudentConnection {
    items: [Student]
    nextToken: String
}

input TableStudentFilterInput {
    id: TableStringFilterInput
    year: TableStringFilterInput
    name: TableStringFilterInput
    class: TableStringFilterInput
    subject: TableStringFilterInput
}

type Student {
    id: String!
    year: String!
    name: String
    class: String
    subject: String
}

Query:

query listStudentByYear {
  listStudents (filter:{year:{eq:"2017"}}) {
    items {
      id
      year
      name
      class
      subject
    }
  }
}

The issue: The query return 001 and 002, but not 003.

When I tried to update "id" from 003 to 004, then the query returns correctly 001, 002, 004.

This weird issue happens quite frequently, after some times, the AppSync query returns an incomplete result (missing some).

Any suggestion is appreciated.

Upvotes: 0

Views: 1289

Answers (1)

Reade Lobdill
Reade Lobdill

Reputation: 21

Check out this thread from amplify-js issues.

Essentially what is happening is a limit is applied before the filter. So if you have a limit of 20 and 003 is entry number 21 it will not be included in the filter operation.

A workaround here is to remove the limit from you resolver in the AWS AppSync Console

So change this:

#set( $ListRequest = {
  "version": "2017-02-28",
  "limit": $limit
})

to this:

#set( $ListRequest = {
  "version": "2017-02-28",
} )

Now this isn't a graceful workaround as the DynamoDB Scan will only return 1MB of data meaning this solution will not work for large (practical) implementations.

Upvotes: 2

Related Questions