question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to filter and THEN sort, with one-many relationship

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisheseltinecommented, Oct 7, 2019

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.

0reactions
chrisheseltinecommented, Dec 2, 2019

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! ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hasura: How to filter and THEN sort, with one-many relationship
For filtering the price you need to apply the where clause inside prices array relationship. { seeds( where: {prices: {price: {_gte: "10", _lte: ......
Read more >
How to sort while filtering by many-to-one relationship - Help
With a GraphQL schema similar to below, containing a many-to-one relation, I have a resolver UDF that I want to return a page...
Read more >
Lesson 4: Sorting, Filtering, and Creating Relationships
This lesson teaches you how to sort and filter an Microsoft Access table. ... When tables have a one-to-many relationship, the table with...
Read more >
Filtering and sorting (Concepts) - Prisma
Prisma Client supports filtering and sorting with the where and orderBy query ... The one-to-many relation between User and Post allows you to...
Read more >
Sort data in a table - Microsoft Support
Sorting is one of the most common tools for data management. In Excel, you can sort your table by one or more columns,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found