Replies: 8 comments 8 replies
-
Unfortunately no; only a top-level We’d need to first add support for more advanced conditional syntax to element queries, before we could add it to GraphQL. I’ll mark this as an enhancement and we can look into the feasibility of it. |
Beta Was this translation helpful? Give feedback.
-
Okay, thanks Brandon. Do you think there is any other way to achieve the intended functionality in GraphQL at the moment, or would it only be possibly in Twig/PHP? |
Beta Was this translation helpful? Give feedback.
-
It’s only possible via yii\db\Query::andWhere() currently. Twig: {% set products = craft.entries()
.andWhere([
'or',
['and', 'field_price >= 0', 'field_price < 50'],
['and', 'field_price >= 200', 'field_price < 250'],
])
.all() %} PHP: $products = \craft\elements\Entry::find()
->andWhere([
'or',
['and', 'field_price >= 0', 'field_price < 50'],
['and', 'field_price >= 200', 'field_price < 250'],
])
->all(); |
Beta Was this translation helpful? Give feedback.
-
Just came here after running into similar issues, and having this type of advanced where clause in GQL would be really great. I created an entry type "Event" with a "Start" and "End" date field. I wanted to create a listing of events either starting after today OR ending after today, but I cannot currently add this kind of grouped OR condition to the queries. For reference, this is how the ligthouse gql package for Laravel approaches this: I believe it uses the same underlying gql php library as Craft does, so perhaps this could be implemented in a similar way. Implementing that in Craft would make the example @brandonkelly posted above look something like this in GraphQL: {
entries(
type: "products",
where: {
OR: [
{
AND: [
{ column: FIELD_PRICE, operator: GTE, value: "0" }
{ column: FIELD_PRICE, operator: LT, value: "50" }
]
},
{
AND: [
{ column: FIELD_PRICE, operator: GTE, value: "200" }
{ column: FIELD_PRICE, operator: LT, value: "250" }
]
}
]
}
) {
id, title
}
} |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? Is this something on the roadmap? |
Beta Was this translation helpful? Give feedback.
-
Just an FYI I ended up writing this custom plugin for Craft v3.x and I think it would work with Craft 4 as well - though I haven't updated it as such. |
Beta Was this translation helpful? Give feedback.
-
Hi, any update on this in Craft 5? |
Beta Was this translation helpful? Give feedback.
-
Description
I'm fetching entries via GraphQL, and I am trying to filter them so that only entries with a certain field within a specified set of ranges are returned. Here is a simplified version of my GraphQL query, with the tail omitted for brevity:
I am able to specify a single range by setting my GraphQL variables like so:
Which suggests to me that it works like the Yii where clause operator format.
However, if I do the following to get any entries that lie within any of the given ranges:
An error is returned (trace omitted for brevity):
Is there a way to do complex queries such as this using GraphQL?
Steps to reproduce
Additional info
Beta Was this translation helpful? Give feedback.
All reactions