2

Is it possible in AWS appsync / amplify to combine filter operators, like:

const filterInput = {
        or:[
          {
          and: [
                  {createdById: { eq: userID }},
                  {chatWithId: { eq: chatWithUser.id }}
                ]
          },
          {
          and:  [
                  {createdById: { eq: chatWithUser.id }},
                  {chatWithId: { eq: userID }}
                ]
          }
        ]
      }

Because for me this is not filtering / working as expected.

  • Can you please provide a bit more information? What is not working as expected? Are you not seeing records that you expect to see? I suspect it may be due to DynamoDB filter semantics. Have you read this https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.FilterExpression? – mparis Jan 29 '19 at 22:37
  • What I try to achieve is combine operator filters. So I want to filter on the firsts AND - OR - on the second AND And if those don't exist than don't return records. I don't know if it is possible to achieve something like this and how that works with amplify or that I have to find another solution. – Ramon Postulart Jan 30 '19 at 06:05
  • This will combine filter operators, but if the data that satisfies the filter expression is scattered throughout your DynamoDB index, then the `Query.listX` might not return the data exactly as you expect due to the semantics of DynamoDB filter expressions. It is hard to tell from the details provided because I do not know what query you ran and what results you got in return that were not what you expected. It looks as if you are trying to model a many-to-many relationship. E.G. I want all the conversations that I am a member of. If this is the case, I can offer advice on that. – mparis Jan 30 '19 at 18:20
  • It's just a simple table in dynamoDB. Graphql (amplify) schema: type Chat { id: ID! createdAt: String createdById: String chatWithId: String messsages: [Message] } – Ramon Postulart Jan 31 '19 at 06:34

1 Answers1

0

Thanks for the extra information. With an Amplify schema that looks like this:

type Chat @model { 
  id: ID! 
  createdAt: String 
  createdById: String 
  chatWithId: String 
  messsages: [Message] 
}

By default, a table with a HASH key storing the id values is created to store the values, and there is no way to efficiently run the query you are trying to run with the default key structure alone. In the future, you will have more tools available to control a @model table's index structure, but for now the only way to do it is via @connection.

Here is an example schema that might help you get started building APIs that can query these relationships more effectively.

ChatQL React schema.graphql

mparis
  • 3,623
  • 1
  • 17
  • 16