Multiple OR/AND filter in AWS Amplify GraphQL

Multiple OR/AND filter in AWS Amplify GraphQL


1

with a simple schema like this:

type Ticket @model{
  id: ID! @primaryKey
  title: String!
  description: String!
  state: State @default(value: "new")
  priority: Priority
}
enum Priority {
  low
  medium
  high
  emergency
}
enum State {
  new
  pending
  closed
  waiting
  suspended
}

How can I filter my query in order to get the tickets that have priority equal "low" OR "high" and also state equal "new" OR "waiting" for example?

In the documentation there is an example for a condition with AND and OR together, but not for multiple OR.

Filters are provided through a JS Object, I can’t use multiple OR keys in this way for example:

let graphQLObject = {
  query: queries.listTickets,
  variables: {
    filter: {
      or: [
        { priority: {eq: "low"} },
        { priority: {eq: "high"} }
      ],
      or: [
        { state: {eq: "new"} },
        { state: {eq: "waiting"} }
      ]
    }
  }
};
await API.graphql(graphQLObject);

1 Answer
1


0

You can nest your conditions simply like this

let graphQLObject = {
  query: queries.listTickets,
  variables: {
    filter: {
      and: [
        {
          or: [{ priority: { eq: 'low' } }, { priority: { eq: 'high' } }],
        },
        { or: [{ state: { eq: 'new' } }, { state: { eq: 'waiting' } }] },
      ],
    },
  },
}

Works of course also, if you want to combine multiple AND conditions together with OR etc.

New contributor

Chris M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *