How to filter and THEN sort, with one-many relationship
See original GitHub issueHi,
I’m new to Hasura, but I feel what I am trying to do should be pretty commonplace…
What I am trying to achieve is to allow a user to apply both a price range filter and a price sort to the results that my site fetches for them. So I want to find all prices within the specified price range, and then sort them by price, either asc or desc.
I have a Hasura Engine running on Heroku Postgres. The DB has two tables, seeds and prices. One row in seeds table, i.e. one seed, can be related to multiple rows in the prices tables, i.e. many prices. They are joined by a foreign key constraint and a one-many relationship, or Object to Array.
I am attempting to query seeds using the following GraphQL query:
{
seeds(
where: {prices: {price: {_gte: "10", _lte: "200"}}},
order_by: {prices_aggregate: {min: {price: asc_nulls_last}}}
) {
product_name
prices {
price
}
}
}
What I want this query to do is to filter out all prices.price that are not within the valid range (10, 200), and then sort them in order of prices.price ascending. However what happens is that the results are sorted by prices.price ascending INCLUDING values that are not within the range, then the results are filtered.
I will give an example to clarify. Consider the following results from the above query:
{
"data": {
"seeds": [
{
"product_name": "Northern Lights Auto Feminised Seeds",
"prices": [
{
"price": 3.48
},
{
"price": 6.79
},
{
"price": 9.58
},
{
"price": 104.5
}
]
},
{
"product_name": "The White OG Feminised Seeds",
"prices": [
{
"price": 3.48
},
{
"price": 6.79
},
{
"price": 15.68
}
]
},
{
"product_name": "Special Kush #1 Feminised Seeds from Royal Queen Seeds",
"prices": [
{
"price": 3.49
},
{
"price": 13.53
},
{
"price": 8.29
}
]
}
]
}
}
The above results are correct, because there are valid values for prices.price within the range specified (104.5, 15.68, 13.53) respectively, however the results are not in the right order. They are instead ordered by the lowest price.prices, regardless of the filter which was specified (10, 200).
The correct order for the results would be:
{
"data": {
"seeds": [
{
"product_name": "Special Kush #1 Feminised Seeds from Royal Queen Seeds",
"prices": [
{
"price": 3.49
},
{
"price": 13.53
},
{
"price": 8.29
}
]
},
{
"product_name": "The White OG Feminised Seeds",
"prices": [
{
"price": 3.48
},
{
"price": 6.79
},
{
"price": 15.68
}
]
},
{
"product_name": "Northern Lights Auto Feminised Seeds",
"prices": [
{
"price": 3.48
},
{
"price": 6.79
},
{
"price": 9.58
},
{
"price": 104.5
}
]
}
]
}
}
Can anyone help me, and explain how I can achieve these correct results? It is worth mentioning that it is not possible to sort the results after the query as there are thousands and the sort will definitely affect which results are returned from the DB.
Thanks, in advance!
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (7 by maintainers)
I started working on another area of my project which is why I left this thread until I was ready. Thanks for the suggestion, I think this will work for my criteria. I will try it shortly and update with the results.
Thanks for helping me, even SO couldn’t solve this one!
P.S I won’t mark as resolved until I have tested and given feedback.
Hi guys, sorry again for the late reply. I have started a new job and unable to find the time to test this right now, but I think it’s safe to say this will work.
You can close it for now, and I will come back when I have time and implement. I will inform if it didn’t work, and we can re-open. If possible I’d like to request this process be simplified in a future update, and supported more easily via Hasura, but I understand it’s a more outside use case.
Thanks again for your time and help everyone, especially in helping me understand the SQL! ❤️