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
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