Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order by aggregates with filters #10526

Open
manasag opened this issue Sep 4, 2024 Discussed in #10522 · 1 comment
Open

Order by aggregates with filters #10526

manasag opened this issue Sep 4, 2024 Discussed in #10522 · 1 comment
Assignees
Labels
c/server Related to server c/v2-engine k/enhancement New feature or improve an existing feature v2-server v2

Comments

@manasag
Copy link
Contributor

manasag commented Sep 4, 2024

Discussed in #10522

Originally posted by kevinwasie September 3, 2024
With the below GraphQL, how do I sort the users[] list by the count of 'user_houses' or 'user_jobs'?

I'm using backend pagination and backend fetches for refreshed data on sort requests. The dataset is to large to pass it everytime to the front end to handle this. Is there a way?

query users($limit: Int!, $offset: Int!, $order_by: users_order_by!) {
  users(limit: $limit, offset: $offset, order_by: [$order_by]) {
    id
    full_name
    user_houses: users_to_buildings_aggregate(where: {role: {_eq: "tenant"}}) {
      aggregate {
        count
      }
    }
    user_jobs: users_to_buildings_aggregate(where: {role: {_eq: "administrator"}}) {
      aggregate {
        count
      }
    }
  }

The query produces:

{
  "data": {
    "users": [
      {
        "id": "asdf",
        "full_name": "User 1",
        "user_houses": {
          "aggregate": {
            "count": 2
          }
        },
        "user_jobs": {
          "aggregate": {
            "count": 1
          }
        }
      },
      {
        "id": "asdf",
        "full_name": "User 2",
        "user_houses": {
          "aggregate": {
            "count": 0
          }
        },
        "user_jobs": {
          "aggregate": {
            "count": 4
          }
        }
      }
    ]
  }
}

I've tried the following but neither of them work:

This removes the filters and simply sorts by the total aggregate of users_to_buildings

{
  "limit": 10,
  "offset": 0,
  "order_by": {
    "users_to_buildings_aggregate": {
      "count": "asc"
    }
  }
}

This produces an error

{
  "limit": 10,
  "offset": 0,
  "order_by": {
    "user_houses": {
      "count": "asc"
    }
  }
}
@manasag manasag added k/enhancement New feature or improve an existing feature c/server Related to server labels Sep 4, 2024
@rakeshkky
Copy link
Member

From the GraphQL query POV, both user_houses and user_jobs are aliases backed by the same GraphQL field. However, you cannot use them as arbitrary input fields, as the latter are specified by input object types in GraphQL schema.

For your use-case, I can think of defining views out of buildings table with filters using role column.

create view buildings_tenant as select * from buildings where role = 'tenant'

and

create view buildings_admin as select * from buildings where role = 'administrator'

Track those views and define array relationships from users table.

user_houses: users -> building_tenant
user_jobs: users -> building_admin

You can use the aggregate fields of above relationships in order_by.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/server Related to server c/v2-engine k/enhancement New feature or improve an existing feature v2-server v2
Projects
None yet
Development

No branches or pull requests

3 participants